1

I need some help in VB.NET: I have six arrays who all have the same length, but the elements of them have different datatypes (string and long). I need to create a multidimensional array or a jagged array which later will be copied to an Excel range at once.

Is this possible or can I only have elements of the same type in an array? Are there any other ways which I can use alternatively?

1 Answer 1

1

You can't have a multidimensional array containing two separate types. The solution is to create a class which contains the two variables you need.

For example,

Public Class MyObject
    Public Property MyString As String
    Public Property MyInteger As Integer
End Class

The following step is in the class where you want to create an array. You can define the following:

//...
Dim myFancyObjects as MyObject(3)
//...

You can access your properties by doing the following:

Integer testNumber = myFancyObjects(0).myInteger
String testString = myFancyObjects(0).myString
Sign up to request clarification or add additional context in comments.

6 Comments

If I use a separate class, can I copy the whole class with all emelend (e.g. 10 columns with 200 elements each) into a 10x200 Excel Range? If I need a loop to get back the stored values than this way I can't use. How I can copy the values to the Excel Range?
You put this 'MyObject' class in an array. This array contains an object with 2 properties, it's an alternative than accessing a 2 dimensional array. But the 2 dimensions are replaced by 2 properties. So yes it's possible.
What do you mean with "copy it into an array"? I created the class, the Properties with the Getter/Setter and made a variable in my main class of the type of the new class. How I can store values in each of the Properties and then copy them back to Excel?
You store them like: myFancyObjects(0).myInteger = 3 myFancyObjects(0).myString = "Hello world"
This is my example code now: <code> 'Class to store the values Public Class HTClass Private Dim m_Arr_1() As Long Public Property Arr_1() As Long() Get Return m_Arr_1 End Get Set m_Arr_1 = value End Set End Property End Class 'Main Class '... Dim HT As New HTClass For i as Integer = 1 to lastRow HT.Arr_1(i - 1 - CLng(offset)) = i Next For i As Long = 1 To 10 MsgBox(HT.Arr_1(i)) Next </code> But I get an error when I try to access the Property value in the last For-Loop. Declaring HTClass(3) as you suggested did not work and I got an compiler error.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.