Juniorscone


Hi,

This may be a very basic problem but im haveing real trouble with it and its driving me crazy!!!!!!!

I have created a class called Class1

I can create a new object by calling

Dim test2 As New Class1

I can change its attrributes by

test2.summary = True
test2.is_stage = False

etc.....

Now what I want to do is store this class in an array. I have tried the folowing code but it doesnt seem to work

Dim proj_tasks(10000) As Class1

proj_tasks(counter) = test2

This keeps erroring and I dont know why! Can somebody help me out!

Also if your feeling very generous could you please adivise me on how I could make this a dynamic array rather than having to set the array to a huge size in the first place

Thanks in advance!

Chris



Re: Array Help

rkimble


What error are you getting   Is the value of 'counter' between 0 and 10000   Your code snipit should work in principle...

Instead of an array though, you should at least use an ArrayList if you want it to by dynamic and if you're working in .NET 2.0 you should use a System.Collections.Generic.List(Of T) to create a List(Of Class1) collection.






Re: Array Help

Dustin_H

You have to declare it differently.

Dim proj_tasks As Class1()

Redim proj_tasks(1000)


Ofcourse, you could always use an arraylist.

Dim proj_tasks As ArrayList

proj_tasks.add(test2)

Dustin







Re: Array Help

Juniorscone

I like the idea of ArrayList (Mainly because im a java programmer)

However when I use your code I get:

Compliation Error:

User defined type not define

Do i need to import anything If so whats the syntax Im a real novice VB programmer





Re: Array Help

sbogollu

import System.Collections





Re: Array Help

MS Johan Stenberg

And if you like ArrayList and you use Visual Studio 2005/.NetFx 2.0, you may also like the generic list:

Dim myList As System.Collections.Generic.List(Of Class1)

No more cast-from-object-to-the-type-I-added-to-the-list mumbo-jumbo - it is all nice & strongly typed.

Best regards,
Johan Stenberg






Re: Array Help

Juniorscone

This is crazy now!

I am using:

Dim task_array() As Class1

Dim counter As Integer

counter = 0

ReDim taskarray(10000) As Class1

task_array(counter) = New Class1

task_array(counter).summary = True

I get a Run-time error '9":

Subscript out of range

The error seems to come from the code "task_array(counter) = New Class1"

Can anybody help me





Re: Array Help

Juniorscone

Forget my last post I have fixed that issues, however now I have another problem:

Dim task_array() As Class1

Dim counter As Integer

counter = 0

ReDim task_array(10000) As Class1

task_array(counter) = New Class1 < --------------- This throws an error (Runtime error 91: Object variable or With Block not set)

task_array(counter).summary = True

Can anybody see why this would throw an error PLease help!!!!!!!!!!1





Re: Array Help

rkimble

Exactly what version of Visual Basic as you using I doesn't look like it can be 2005 because that redim statement wouldn't be valid...






Re: Array Help

Dustin_H

Code should look like this....

Dim task_array() As Class1

Dim counter As Integer

counter = 0

ReDim task_array(10000)

task_array(counter) = New Class1 '(Make sure Class1 has a Public sub New() event that can be called)

task_array(counter).summary = True

Also, rember to increment Counter.

However, your dimensioning quite alot of space in your array that you may not need. I would setup as follows and grow your array as needed. here's a full example.

Public Class Form1

Dim task_array As Class1()

Private Sub AddClassToArray(ByVal Obj As Class1)

If task_array Is Nothing Then ReDim task_array(0)

Dim pos As Integer = task_array.GetUpperBound(0)

If task_array(pos) IsNot Nothing Then

pos += 1

ReDim Preserve task_array(pos)

End If

task_array(pos) = Obj

End Sub

Public Sub temp()

AddClassToArray(New Class1)

End Sub

End Class