Mixed types in multidimensional arrays

A

Anil Gupte

How can I dimension an array like this:

dim info(a as integer, b as string) so I can store somthing like
{{"John", 10}{"Dan", 20}{"Jane", 30}}

I want to be able to sort this array too, but I think I can swing that if I
know how to create this array or whatever else method is needed.
 
T

Theo Verweij

Public struct Storage
Dim a as Integer
Dim b as String
End Struct

Dim info() as Storage
 
T

Theo Verweij

Sorry, it must be:

Public structure Storage
Dim a As Integer
Dim b As String
End Structure

Dim Info() as storage
 
A

Anil Gupte

In that case, I may as well as create a class, right? I understand
Structures are "passe" in OOP?

Only problem is, creating a class that is an array sounds daunting....
 
H

Herfried K. Wagner [MVP]

Anil Gupte said:
How can I dimension an array like this:

dim info(a as integer, b as string) so I can store somthing like
{{"John", 10}{"Dan", 20}{"Jane", 30}}

I want to be able to sort this array too, but I think I can swing that if
I know how to create this array or whatever else method is needed.

Check out the chapter about "jagged arrays" in the documentation.
 
J

Jay B. Harlow

Anil,
Structures are "passe" in OOP?
No! Why would you think that?

Structures are used when you need to define "Value Types" within .NET

http://msdn.microsoft.com/library/d...genref/html/cpconValueTypeUsageGuidelines.asp

Basically when you need a type that:
- acts like primitive types (acts like an integer)
- Have an instance size under 16 bytes
- Are immutable
- Value semantics are desirable.

Although Classes are more commonly used in .NET, Structures still offer all
the major tenets of OO (Encapsulation, Abstraction, Polymorphism)...

Considering there is significantly more to inheritance then the common
Implementation Inheritance found with Classes...
Only problem is, creating a class that is an array sounds daunting....
You wouldn't create a class that is an array, you would create an array of a
specific type. That specific type can be either a reference type (Class) or
a value type (Structure)

In the example you gave, an array of Structures might be easier to use then
an array of Classes...
 
T

Tim Patrick

To provide a custom sorting algorithm, implement the IComparable interface
in the class, and then craft the CompareTo override. If you look up "IComparable
interface, about IComparable interface" in the on-line help, it will provide
an example.
 
A

Anil Gupte

Thanx yes,that does help. I have read about Enums, now I will go read up on
structures. I was given the impression by some programmer that real OOP
does not include Structs.

Anyway thanx,
 
A

Anil Gupte

I think jagged arrays are only avaialble in C#, not VB. I looked in the
dynamc help and also in the books for VB that I have, but there is no
mention of it.

Thanx,
 
H

Herfried K. Wagner [MVP]

Anil Gupte said:
I think jagged arrays are only avaialble in C#, not VB. I looked in the
dynamc help and also in the books for VB that I have, but there is no
mention of it.

Jagged arrays are available in VB too ('Dim x()() As String'). However, as
I missed in my previous post, it's better not to use them because you store
values of different types in the elements.
 
M

Miro

Anil,

As the other posts said as well...

I tried to do the same thing you did as well...
In the end I used a Class.

I didnt know about Struct, but im pretty happy with the way the class
handled it for me.

M.
 

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

Top