|
-
Nov 28th, 2002, 01:35 AM
#1
Thread Starter
New Member
Arrays in VB.NET
What I want to do is create an array, of say 5 elements, but instead of doing it the old VB6 way:
Dim myArr(4) as variant
myArr(0) = "1"
myArr(1) = 23
myArr(2) = "2"
myArr(3) = "other"
myArr(4) = 2.2
I want to be able to clean it up and do it on one line, somthing like this:
Dim myArr(4) as VariantType = new Array("1",2,2.2,"4",5)
But I get the error:
'New' cannot be used on a class that is declared 'MustInherit'.
I know I am doing something wrong, anyone know what???
-
Nov 28th, 2002, 02:03 AM
#2
VB Code:
Dim myArr() As VariantType = {"1", 23, "2", "other", 2.2}
-
Nov 28th, 2002, 02:08 AM
#3
Use Object instead of VariantType. Object is pretty much the equvilant of Variant.
VB Code:
Dim myArr() As Object= {"1", 23, "2", "other", 2.2}
-
Nov 29th, 2002, 09:18 AM
#4
Lively Member
there is only one problem, all the variables will use the same object/class, not a new instance of it.
so:
why don't you do it like
dim arr() as object
arr(0)=new object
arr(1)=new object
...
very handy: [vbcode][/vbcode]
VB.NET - VB6 - VBA - ASP - RPG(AS/400) - C++ - java - SQL
look in the help, many probs can be solved that way.
I know, i'm to lazy too.
PLEASE PUT RESOLVED IF RESOLVED!!
-
Nov 29th, 2002, 09:20 AM
#5
Lively Member
this way you can even make triangular arrays
like
1
1 1
1 1 1
...
or even
1
1 1 1
1 1
1 1 1 1
...
very handy: [vbcode][/vbcode]
VB.NET - VB6 - VBA - ASP - RPG(AS/400) - C++ - java - SQL
look in the help, many probs can be solved that way.
I know, i'm to lazy too.
PLEASE PUT RESOLVED IF RESOLVED!!
-
Nov 29th, 2002, 09:40 PM
#6
Actually this:
VB Code:
dim arr() as object
arr(0)=new object
arr(1)=new object
And
VB Code:
Dim myArr() As Object= {New Object,New Object}
Are the samething.
The only difference is you used objects and the previous examples used more common data types.
-
Nov 30th, 2002, 03:22 AM
#7
Member
Remember that you can use a For Each loop with arrays too.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|