I get messed up between instanciating an object and dimensioning the
object. Can someone point me to a place that lets me know a little
behind the scenes on what the difference is?
Is the New constructor called by default for a string when you use
s="blah". Is this because it has a default constructor? This cannot
be done with all Types right? I am going to list what I thought was
going on in my code below, it would probably be way too long to answer
these questions here?
Thanks for earlier answers, they were all really good. I could
probably try and plug my way through VB and have no idea what I am
doing, maybe that is why VB is popular?
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim s As String 'Not sure exactly what this
does behind the scenes, just know that you have to do it?
It allocates a reference to a string and clears it to Nothing. What
does that mean? Well, a string is a reference type, so it lives on
the heap (the dynamic data store). So, what you are really setting
aside is a variable that can point to a location on the heap that
holds a string. When the string is assigned and the memory is
allocated, then the reference will then contain the address of the
string...
s = "blah" 'Somehow stores
(character array) into a String Type
While string is a reference type, it is treated in some ways like a
value type by the compiler. This is one of those ways. Your
assignment above, is really equivalent to:
s = New String ("blah".ToCharArray())
But, because that would be a pain to type all the time the compiler
gives a break on it's syntax.
Dim t As Type 'Not sure what it does
behind the scenes, but makes a variable that can hold a type?
Yep. You go it that's what it does.
t = s.GetType 'Get what type a string is,
which not sure what that would look like?
Look at the docs for System.Type. It is an object that can be used to
discover a lot of information about the type of an object.
Dim obj As Object 'Dimension a non-specific
object that can store just about anything, not sure how it does it?
It does it by the power of polymorphism. In .Net everything derives
from System.Object. When you don't explicitly inherit from another
object, then you are inheriting from System.Object. Because of that,
you can store any .net object in a variable of type object. Of
course, you functionality is limited to that defined by the
System.Object interface
obj = System.Activator.CreateInstance(t) 'Create
instance of the type stored in t, and set to be obj, is there another
way to just
'dimension with the same type here rather then have to instantiate it?
I'm not sure what you mean? With the string you could have just done:
Dim s2 As String = s
Now, with other reference types you might be suprised to learn that a
change to s would effect s2. But, that isn't the case with string.
It's another one of those places where the runtime treats string a
little more like a value type. Anyway, if you look in the docs about
string interning, then you will get an idea of what I'm talking about.
obj = "hello" 'If obj is now a string
type I should be able to assign a value to it
It's not a string type - obj is type object. You can assign anything
you want to it because everything in .net ultimately derives from
object.
s = DirectCast(obj, String) 'VB somehow switches types
DirectCast will essentially make sure that the object your casting is
really of the type you are wanting to convert it too and if it is make
the assignment. If it isn't you get an exception.
RichTextBox1.Text = s 'assigns a string to a
richtextbox
End Sub
I hope this helps somewhat. There is a lot more that could be said
about this, but I didn't want to go to deep. If you have any
questions, feel free to ask again