473,598 Members | 3,266 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

2 ways to initialize, what's the difference?

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(deleg ate, args)

Zytan

Feb 16 '07 #1
38 2298
On Feb 16, 2:38 pm, "Zytan" <zytanlith...@y ahoo.comwrote:
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(deleg ate, args)

Zytan
There is no difference.

--
Tom Shelton

Feb 16 '07 #2
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(B yVal sender As Object, ByVal e As EventArgs)
Handles Button1.Click

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

End Sub

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

Dim args As Object() = {strText}

End Sub

End Class
"Zytan" <zy**********@y ahoo.comwrote in message
news:11******** **************@ a75g2000cwd.goo glegroups.com.. .
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(deleg ate, args)

Zytan
Feb 16 '07 #3
"Zytan" <zy**********@y ahoo.comschrieb :
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/>

Feb 16 '07 #4
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(deleg ate, 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
Feb 17 '07 #5
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

Feb 17 '07 #6
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
Feb 17 '07 #7
On 2007-02-17, Zytan <zy**********@y ahoo.comwrote:
>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
Feb 17 '07 #8
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" <zy**********@y ahoo.comwrote in message
news:11******** **************@ j27g2000cwj.goo glegroups.com.. .
>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
Feb 17 '07 #9
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" <zy**********@y ahoo.comwrote in message
news:11******** *************@t 69g2000cwt.goog legroups.com...
>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


Feb 17 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1376
by: KevinRug | last post by:
in one page, i am able to declare and initialize a variable like this, in one line. cmdPersonalInfo = new SqlCommand("getPersonalInfo", myG.sqlConnection); In another page it gives me an error when doing as above and I must first declare then initialize, like so:
10
2095
by: Andrew | last post by:
Hi, friends, In Page_Load() events, I want to initialize this web page with values I retrieved from DB. For example, for the element <INPUT type="text" id="firstName" .../>, I want to make its value equal to "John". So, in C#, I did: this.firstName = dbrow.ToString();
1
1465
by: dennis.sprengers | last post by:
I've compared some open-source javascript editors, and found different techniques for constructing the code. Two examples: CodePress.run = function() { new CodePress(t); } CodePress = function(obj) { self.initialize = function() {} self.toggleLineNumbers = function() {}
0
7987
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8392
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8264
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6718
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
3897
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3939
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2412
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1504
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1250
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.