We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

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

Murray Brown
Murray Brown asked
on
Medium Priority
267 Views
Last Modified: 2012-05-05
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
Comment
Watch Question

Test Developer/Validator
Commented:
yes.   you want to use just one "m" in a dim.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Murray BrownASP.net/VBA/VSTO Developer

Author

Commented:
Thanks for that!
The dimm was a typo.
Appreciate the quick answer!
Jens FiedererTest Developer/Validator

Commented:
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.
Murray BrownASP.net/VBA/VSTO Developer

Author

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
Jens FiedererTest Developer/Validator

Commented:
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.
Murray BrownASP.net/VBA/VSTO Developer

Author

Commented:
Thanks
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.