By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,972 Members | 1,115 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,972 IT Pros & Developers. It's quick & easy.

"Basic Question"

P: n/a
What is the difference between these declarations:
Dim strMyData() as string

and

Dim strMyData as string()

-Peter

Nov 21 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
I believe they are equivalent. I think the second has been deprecated.
Although, it works fine in VB 2005 beta 1.

Dim strMyData() As String = {"a", "b", "c"}
Dim strMyData As String() = {"a", "b", "c"}

Greg
"pmclinn" <pe***@mclinn.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
What is the difference between these declarations:
Dim strMyData() as string

and

Dim strMyData as string()

-Peter

Nov 21 '05 #2

P: n/a
Peter,

As Greg said they have equal results,

However
dim str(10) as String goes and
dim str() as String(10) does not go

A little bit confusing is that in a function or other places where you need
the type you will write:
private function myfunction (byval str as String()) As string()
Ctype(mytype, String())

I hope this helps?

Cor

"pmclinn" <pe***@mclinn.com>
What is the difference between these declarations:
Dim strMyData() as string

and

Dim strMyData as string()

-Peter

Nov 21 '05 #3

P: n/a
"Cor Ligthert" <no************@planet.nl> schrieb:
As Greg said they have equal results,

However
dim str(10) as String goes and
dim str() as String(10) does not go


But 'Dim astr As String(10) {}' will work.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #4

P: n/a
Herfried,
But 'Dim astr As String(10) {}' will work.


Thanks I did not know that.
(And I will never use that)

:-)

Cor
Nov 21 '05 #5

P: n/a
Don't write that one off to quickly Cor.

Although it's not quite the same, the following can be very useful.

Dim astr() As String
Nov 21 '05 #6

P: n/a
"Stephany Young" <noone@localhost> schrieb:
Although it's not quite the same, the following can be very useful.

Dim astr() As String
.
.
.
astr = New String(10) {}


You could use 'ReDim astr(10)' instead of the line above, which is IMO the
preferred method because it uses VB.NET's own array syntax.

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

Nov 21 '05 #7

P: n/a
Stephany,

I have the opinion that for a fixed array the array is the best
For a dynamimc array every Ilist or Icollection implementing
array/collection, where the arraylist is my favorit.

Cor
Nov 21 '05 #8

P: n/a
Agreed Herfied. I was pointing out a valid alternative rather than
advocating it's use.

That said, the alternative does have one useful feature and that is to
populate the array at the same time as declaring and/or 'ReDim'ing it:

Dim astr() As String = New String(10) {"1", "4", "7" ... "A", "D"}
Nov 21 '05 #9

P: n/a
Dim strMyData() as string

means that you are initializing an array of strings

Dim strMyData as string()

is simply wrong syntax. The open and close parentheses at the end of the
statement signifies that you are providing reference to the class
through the default constructor of the class but since you are not using
it with the New keyword it is wrong syntax.

You can use Dim strMyData as New SomeClass(), which means that you are
creating an instance called strMyData and initializing the object
through the default constructor of the class.

with regards,
J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #10

P: n/a
> Dim strMyData as string() is simply wrong syntax. is simply wrong syntax.

Don't be so sure. I still think it is just depracted syntax.

Try this:

Dim s as string
Intellisense says for s = "Dim s as string"

Dim s() as string
Intellisense says for s = "Dim s() as string"

Dim s as string()
Intellisense says for s = "Dim s() as string"

Also this will compile:
Dim s as string()
Redim s(10)

And this won't:
Dim s a string
Redim s(10)

Greg

"Ravichandran J.V." <jv************@yahoo.com> wrote in message
news:O$**************@TK2MSFTNGP15.phx.gbl...
Dim strMyData() as string

means that you are initializing an array of strings

Dim strMyData as string()

is simply wrong syntax. The open and close parentheses at the end of the
statement signifies that you are providing reference to the class
through the default constructor of the class but since you are not using
it with the New keyword it is wrong syntax.

You can use Dim strMyData as New SomeClass(), which means that you are
creating an instance called strMyData and initializing the object
through the default constructor of the class.

with regards,
J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 21 '05 #11

P: n/a
Greg,

"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> schrieb_
Dim strMyData as string() is simply wrong syntax. is simply wrong syntax.


Don't be so sure. I still think it is just depracted syntax.


I don't think it's deprecated. It's only an alternative that works and that
will work for forseeable future. VB6 only supported the 'Dim X() As Y'
syntax.

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

Nov 21 '05 #12

P: n/a

"Ravichandran J.V." <jv************@yahoo.com> wrote
Dim strMyData() as string

means that you are initializing an array of strings

Dim strMyData as string()

is simply wrong syntax. The open and close parentheses at the end of the
statement signifies that you are providing reference to the class
through the default constructor of the class but since you are not using
it with the New keyword it is wrong syntax.


You might want to inform MSFT that syntax is wrong since they
use that syntax in the documentation to explain creating an array!

(See 'New keyword' in VB.Net help)

There is no parameterless constructor for the String class:

strMyData = New String()

That syntax is wrong, which indicates that there is no 'default constructor'
for the String class.

Following primative types with parentheses is another valid data type as
evidensed by:

Dim a As String() = {"X", "Y"}
Dim b As Integer() = {1, 2}

The parentheses indicate the Array class is used to build a strongly typed
array. (Note the array class also has no 'default constructor' and the
desired type is a requirement of the shared CreateInstance method.)

HTH
LFS

Nov 21 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.