"Basic Question"

  • Thread starter Thread starter pmclinn
  • Start date Start date
P

pmclinn

What is the difference between these declarations:
Dim strMyData() as string

and

Dim strMyData as string()

-Peter
 
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
 
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
 
Cor Ligthert said:
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.
 
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
 
Stephany Young said:
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.
 
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
 
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"}
 
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
 
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
 
Greg,

Greg Burns said:
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.
 
Ravichandran J.V. said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top