PC Review


Reply
Thread Tools Rate Thread

2 ways to initialize, what's the difference?

 
 
Zytan
Guest
Posts: n/a
 
      16th Feb 2007
What is the difference between these two lines?

Dim args As Object() = New Object() {strText}
Dim args As Object() = {strText}

args seems usuable from either, say, like so:

Me.Invoke(delegate, args)

Zytan

 
Reply With Quote
 
 
 
 
Tom Shelton
Guest
Posts: n/a
 
      16th Feb 2007
On Feb 16, 2:38 pm, "Zytan" <zytanlith...@yahoo.com> wrote:
> What is the difference between these two lines?
>
> Dim args As Object() = New Object() {strText}
> Dim args As Object() = {strText}
>
> args seems usuable from either, say, like so:
>
> Me.Invoke(delegate, args)
>
> Zytan


There is no difference.

--
Tom Shelton

 
Reply With Quote
 
Stephany Young
Guest
Posts: n/a
 
      16th Feb 2007
Interesting. Another foible where both are valid.

The brackets are used to populate an array so it appears that, for the form
'Dim args As Object() = {strText}', the compiler second guesses you and
assumes you actually mean 'Dim args As Object() = New Object() {strText}'.

If you compile the following and have a look at the result with Lutz
Roeder's Relector you will find that both forms of the statement emit
identical IL.

Public Class Form1

Private strText As String

Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Handles Button1.Click

Dim args As Object() = New Object() {strText}

End Sub

Private Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs)
Handles Button2.Click

Dim args As Object() = {strText}

End Sub

End Class


"Zytan" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> What is the difference between these two lines?
>
> Dim args As Object() = New Object() {strText}
> Dim args As Object() = {strText}
>
> args seems usuable from either, say, like so:
>
> Me.Invoke(delegate, args)
>
> Zytan
>


 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      16th Feb 2007
"Zytan" <(E-Mail Removed)> schrieb:
> What is the difference between these two lines?
>
> Dim args As Object() = New Object() {strText}
> Dim args As Object() = {strText}


No difference (except that I'd write 'Dim Args() As Object = {strText}'
;-)).

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

 
Reply With Quote
 
=?ISO-8859-1?Q?G=F6ran_Andersson?=
Guest
Posts: n/a
 
      17th Feb 2007
Zytan wrote:
> What is the difference between these two lines?
>
> Dim args As Object() = New Object() {strText}
> Dim args As Object() = {strText}
>
> args seems usuable from either, say, like so:
>
> Me.Invoke(delegate, args)
>
> Zytan
>


There is no difference. The second is just a short form for the first
one that is available when you are declaring the variable and assigning
a value to it in the same statement.

If you first declare the variable and then assign a value to it, you
have to specify the type of the value:

Dim args as Object()
args = New Object() { strtext }

' this would produce a compiler error
' args = { strtext }

--
Göran Andersson
_____
http://www.guffa.com
 
Reply With Quote
 
Zytan
Guest
Posts: n/a
 
      17th Feb 2007
> Interesting. Another foible where both are valid.

So the fact that code like this can exist is kind of frowned upon?
Which is the 'better' code?

> The brackets are used to populate an array so it appears that, for the form
> 'Dim args As Object() = {strText}', the compiler second guesses you and
> assumes you actually mean 'Dim args As Object() = New Object() {strText}'.


Aha, so the compiler is putting a "New" in there. You see, my issue
is that one has "New" and the other doesn't. "New" means it is making
an new object. Too many times ive seen in VB that "New" is optional
(with other things), and both ways make the same code. So, im left to
wonder, well, they both cant be the same, since "New" appears only on
one, so is one wasting memory?

I hope you can repsect the perspective of a newcomer.

> If you compile the following and have a look at the result with Lutz
> Roeder's Relector you will find that both forms of the statement emit
> identical IL.


I have no doubt about this, since 4 of you have told me the code is
identical. Thank you all for that.

But i am not satisifed, as i would like to know why it is identical.
you gave your 'guess', so i assume that version of the code is the one
that is not recommended, since it hides what is going on?

Zytan

 
Reply With Quote
 
Zytan
Guest
Posts: n/a
 
      17th Feb 2007
> There is no difference. The second is just a short form for the first
> one that is available when you are declaring the variable and assigning
> a value to it in the same statement.


Is this done to torture newbies?

I just dont follow how an acceptable way of shortening code allows the
removal of the "New" keyword. Am i the only one who is bothered by
this? Is there any other meaningful way i can think of this style of
code that *makes sense*, in that i can think that "New" really is
there? I just can't wrap my mind around it. for example I can see
how this:

Dim x As Control = New Control

it shortened to this:

Dim x As New Control

But, for:

Dim args As Object() = New Object() {strText}

shortened to this:

Dim args As Object() = {strText}

doesn't flow. What am i not seeing?

> If you first declare the variable and then assign a value to it, you
> have to specify the type of the value:
>
> Dim args as Object()
> args = New Object() { strtext }
>
> ' this would produce a compiler error
> ' args = { strtext }


yes, ok. thanks.

And this strengthens my perspecitve in that the shortening doesn't
'flow'. It doesn't seem as though it should work. Do you follow what
i'm saying?

Zytan


 
Reply With Quote
 
Tom Shelton
Guest
Posts: n/a
 
      17th Feb 2007
On 2007-02-17, Zytan <(E-Mail Removed)> wrote:
>> Interesting. Another foible where both are valid.

>
> So the fact that code like this can exist is kind of frowned upon?
> Which is the 'better' code?
>
>> The brackets are used to populate an array so it appears that, for the form
>> 'Dim args As Object() = {strText}', the compiler second guesses you and
>> assumes you actually mean 'Dim args As Object() = New Object() {strText}'.

>
> Aha, so the compiler is putting a "New" in there. You see, my issue
> is that one has "New" and the other doesn't. "New" means it is making
> an new object. Too many times ive seen in VB that "New" is optional
> (with other things), and both ways make the same code. So, im left to
> wonder, well, they both cant be the same, since "New" appears only on
> one, so is one wasting memory?
>
> I hope you can repsect the perspective of a newcomer.
>
>> If you compile the following and have a look at the result with Lutz
>> Roeder's Relector you will find that both forms of the statement emit
>> identical IL.

>
> I have no doubt about this, since 4 of you have told me the code is
> identical. Thank you all for that.
>
> But i am not satisifed, as i would like to know why it is identical.
> you gave your 'guess', so i assume that version of the code is the one
> that is not recommended, since it hides what is going on?
>
> Zytan
>


Zytan,

They are the same because the compiler infers what you want to do, and inserts
the new for you. I personally think it is bad form not to specify the new,
but there is a certain amount of BASIC tradition that spills over into VB.NET


Anyway, what is recommended is what you feel is best or that your boss tells
you is best

--
Tom Shelton
 
Reply With Quote
 
Stephany Young
Guest
Posts: n/a
 
      17th Feb 2007
As you are finding, VB.NET has a number of foibles, that don't, at face
value, seem logical. Mostly, it is a matter of choice/preference/style,
(call it what you will), as to which form of the syntax you use.

When one first comes across a foible it tends to be a bit annoying but once
one figures out what is actually happening then using the syntax of choice
tends to become habit.

You will find 'purists' who will argue till the cows come home that one form
is correct and the other isn't or that one form is is better and the other
isn't but if both forms compile to identical IL then the argument is purely
acedemic and it is up to you to use the form that you feel most comfortable
with.

If the compiled IL is different and/or the execution of one form is
implemented differently then that is a completely different matter. (The use
of Control.Select() over Control.Focus() fits into this category.)


"Zytan" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>> Interesting. Another foible where both are valid.

>
> So the fact that code like this can exist is kind of frowned upon?
> Which is the 'better' code?
>
>> The brackets are used to populate an array so it appears that, for the
>> form
>> 'Dim args As Object() = {strText}', the compiler second guesses you and
>> assumes you actually mean 'Dim args As Object() = New Object()
>> {strText}'.

>
> Aha, so the compiler is putting a "New" in there. You see, my issue
> is that one has "New" and the other doesn't. "New" means it is making
> an new object. Too many times ive seen in VB that "New" is optional
> (with other things), and both ways make the same code. So, im left to
> wonder, well, they both cant be the same, since "New" appears only on
> one, so is one wasting memory?
>
> I hope you can repsect the perspective of a newcomer.
>
>> If you compile the following and have a look at the result with Lutz
>> Roeder's Relector you will find that both forms of the statement emit
>> identical IL.

>
> I have no doubt about this, since 4 of you have told me the code is
> identical. Thank you all for that.
>
> But i am not satisifed, as i would like to know why it is identical.
> you gave your 'guess', so i assume that version of the code is the one
> that is not recommended, since it hides what is going on?
>
> Zytan
>


 
Reply With Quote
 
Tom Leylan
Guest
Posts: n/a
 
      17th Feb 2007
It isn't intended to torture but rather it is the result of having to
conform new language features into a system not originally designed to
accomodate such things. There is a bit of "VB-ism" concern but consider the
challenge.

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()

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

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()

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. :-)

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"}

Here it is as an array of Integers:

Dim arr As Integer()
arr = New Integer() {1, 2, 3}

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?

Tom


"Zytan" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>> There is no difference. The second is just a short form for the first
>> one that is available when you are declaring the variable and assigning
>> a value to it in the same statement.

>
> Is this done to torture newbies?
>
> I just dont follow how an acceptable way of shortening code allows the
> removal of the "New" keyword. Am i the only one who is bothered by
> this? Is there any other meaningful way i can think of this style of
> code that *makes sense*, in that i can think that "New" really is
> there? I just can't wrap my mind around it. for example I can see
> how this:
>
> Dim x As Control = New Control
>
> it shortened to this:
>
> Dim x As New Control
>
> But, for:
>
> Dim args As Object() = New Object() {strText}
>
> shortened to this:
>
> Dim args As Object() = {strText}
>
> doesn't flow. What am i not seeing?
>
>> If you first declare the variable and then assign a value to it, you
>> have to specify the type of the value:
>>
>> Dim args as Object()
>> args = New Object() { strtext }
>>
>> ' this would produce a compiler error
>> ' args = { strtext }

>
> yes, ok. thanks.
>
> And this strengthens my perspecitve in that the shortening doesn't
> 'flow'. It doesn't seem as though it should work. Do you follow what
> i'm saying?
>
> Zytan
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between two ways to set Text in a TextBox ? Moe Sisko Microsoft ASP .NET 0 31st Mar 2008 06:59 AM
What's the difference between these two ways of adding delegates to a Generic List object? Deckarep Microsoft C# .NET 1 21st Jan 2007 04:08 AM
Difference between ways of Object creation =?Utf-8?B?VG9t?= Microsoft Dot NET Framework 2 31st Oct 2005 06:19 AM
Calculating Date difference in 2 ways Hari Microsoft Excel Misc 5 15th Jan 2005 10:25 PM
Is there a difference of this two ways? =?Utf-8?B?Q2h1YSBXZW4gQ2hpbmc=?= Microsoft C# .NET 2 1st Jul 2004 10:57 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:17 PM.