Tom, thanks for your indepth reply.
The following (not withstanding my belief that "dim" as a keyword is
outdated (somebody will surely defend it I'm certain)) 1) declares the
scope of the variable, 2) defines it's datatype and 3) assigns a value (the
object reference.)
Dim x as Control = New Control()
Ok. (I also believe Dim is outdated. It's like using LET in older
BASICs, but we were all glad we didnt have to use it, so no one did.)
While the following works it is generally a syntactical shortcut and I
recommend you avoid it. As always my recommendation is to avoid
language-specific items and to embrace language-agnostic solutions (when it
is possible and practical to do so). Those intent on arguing please re-read
the stuff in parens
Dim x As New Control
I prefer to avoid this shortcut, too. But i have been using it to get
used to other people's code.
Part of the reason is that it doesn't follow that you want a "control"
reference just because you instantiate a control object. The following
works as well because an ArrayList is in fact an object. The shortcut
version cannot do this.
Dim obj As Object
obj = New ArrayList()
I see. Since the following makes obj2 an ArrayList, not an Object:
Dim obj2 As New ArrayList()
So now consider how does one assign contant values? The following works:
Dim arg As String
arg = "test"
Note also that you aren't particularly bothered by the lack of a New keyword
in this instance, right? I'll suggest you've grown accustomed to thinking
of strings as some sort of natural computer data type but they aren't.
No. I do think of strings as classes, unlike integers and floating
point types that are natural types. You can make a string (or any
class) without "New"ing it. It just becomes a variable on the stack,
rather than dynamically allocated. So, i don't miss the lack of "New"
here.
I notice that this works:
Dim arg2 As String = "test"
but this doesn't:
Dim arg3 As New String = "test"
This is confusing. Why can't i "New" that class if i want to? I
obviously have no idea how the "New" keyword is used. I was thinking
it would be similar to what it means in C++. (Does C# share these
same issues? No, it looks like you have to use "New" in C#. Maybe I
should use C# instead.)
So what would the syntax be to assign constant array values? An array
requires the New keyword and that means you have to include the class name
and only then can you tack on the values you want. So you end up with
something like the following:
Dim args As String()
args = New String() {"test"}
No, you can use this without the "New" keyword, as well:
Dim arg5 As String() = {"test", "2nd"}
Is this the same issue I was having with Object()? Let's see. Yes,
it is. Ok, so let's ignore Object() for now, since that was confusing
me. Let's deal with just String or Integer, since the above is the
same problem.
Here it is as an array of Integers:
Dim arr As Integer()
arr = New Integer() {1, 2, 3}
And the same can be done without "New", which normally is:
Dim i2 As Integer() = {4, 5, 6}
So what's left is the example you saw originally, the declaration and
assignment of an array of Objects.
Dim args as Object()
args = New Object() { strtext }
And that's why we have that syntax. What else could the syntax be and do
you think another one would be more clear?
The above is exactly what I expect. args is an array of objects.
Then we 'new' an array of Objects initialized with { strtext}, and set
args to be that. The shortcut is what I didn't understand.
Ok, i think i am more clear about all of this. I was used to:
Dim i2 As Integer() = {4, 5, 6}
when this is the same as:
Dim i1 As Integer()
i1 = New Integer() {1, 2, 3}
so, you really are using "New" on each array, but its normally
hidden. And here i thought these variables would just be local stack
variables, not dynamically allocated. I think this was the source of
confusion. Nobody wanted to type "New" all the time, so they made a
shortcut that looks like it isnt being dynamically allocated, which
screws up some C++ programmers like me.
Thanks for all of your input
Zytan