VB.net/VB 2005 Using multiple types in an array

Hi

I am working in Visual Studio 2005.
Is it possible to use mutliple types with the same array

eg Dimm arr(10) as Object

arr(0) = "Hello"
arr(1) = False
arr(2) = 23
etc
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?
 
Jens FiedererTest Developer/ValidatorCommented:
yes.   you want to use just one "m" in a dim.
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks for that!
The dimm was a typo.
Appreciate the quick answer!
0
 
Jens FiedererTest Developer/ValidatorCommented:
Note, though, that this is really a case of using subclasses, and everything being a subclass of Object.

If you declared

dim arr(10) as Shape

And Circle, Square, and Triangle all interited from Shape, you could put
Circles Squares and Triangles in arr ... but not, for example, an arraylist.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
I think I understand your last comment.
I just want to be able to eg do the following
Dim A(2) As Object
A(0) = "Tree"
A(1) = xlYesNoGuess.xlYes
A(2) = 3456376

I am assuming that this is okay.

Thanks
0
 
Jens FiedererTest Developer/ValidatorCommented:
That will WORK, but it is dangerous.

Later, when you reference A(2)...and maybe expect to find a string rather than a number...you might blow up.

It is generally good programming practice to make the type of the Array as specific as possible, to minimize the chance of a type mismatch in the future.
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.