Murray Brown

asked on

# 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

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

ASKER CERTIFIED SOLUTION

membership

Create a free account to see this answer

Signing up is free and takes 30 seconds.

**No credit card required.**
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.

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.

ASKER

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

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

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.

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.

ASKER

Thanks

ASKER

The dimm was a typo.

Appreciate the quick answer!