i write a class that stores data simple. the class was in the module and i import that module in the main form class.
Name of Class>> DataSource
Name of Object>> DS( )
i create an object array in the module by the following code
Public DD() As DataSource |
and then i add the following line to initialize the object array in button click event.
DS(Index + 1) = New DataSource |
Here index+1 show how many elements i want to be in object array.
but when i run the project it succesfull but clicking the button[that contains the code] gives program a break and show the following error.
An unhandled exception of type 'System.NullReferenceException' occurred in abc.exe
Additional information: Object reference not set to an instance of an object.
i initialize the object but it give the above error what i want is to create an object array containing index+1 number of elements. the class is public declare in module file and the object is also public declare in module file and initialization of object occurs in button click event when i know the total number of elements.

Object array and refference
AJ_101
Index is an integer which calculated after some lopping. where i get the how many objects i need to initialize.
and i am using .net 2003 framework 1.1 and in system collection and i can't find generic's may be it was in 2005 edition of framework 2.0 i find the list array class.
Kiku
his is why the arraylist or list item is nicer than the array as you dont have to be concerned about how many elements it is - you can just add elements as you go.
ocanon
You could even use the generics list type to create a list which you simply add and removed items out of. As its using generics (new feature with VB 2005) it is type safe and will only allow storage of specific type rather than any object.
All you need to do to add an element is use the AList.add method and to remove an element is use AList.Remove
Public
Class fred End ClassModule
Module1 Dim AList As New System.Collections.Generic.List(Of fred) Sub Main()AList.Add(New fred)
AList.Add(New fred)
End Sub
End
ModulecarzyR
Public Class Form1
Private DataCollection As ArrayList
Public Sub Main()
'Make new connection, and get it's position in the array.
Dim position As Integer = DataCollection.Add(New Data.OleDb.OleDbConnection)
'Modify the settings for the elodbconnection.
With CType(DataCollection(position), Data.OleDb.OleDbCommand)
.CommandText = "SELECT * FROM Something"
.CommandTimeout = 30
End With
'All done work, remove the item from the array list.
DataCollection.RemoveAt(position)
End Sub
End Class
As you see, easier to manage.
Hope it helps!
Dustin.
joerage
From your code sample... do this..
Public DD() As DataSource
private SizeOfArray As Integer =100
Redim DD(SizeOfArray)
OR.....
You can dynamically increase the array as you go....
Public DD() As DataSource
dim SizeOfArray As Integer =-1
SizeOfArray += 1 'Increase the array Size.
Redim Preserve DD(SizeOfArray) 'Resize array to the new size.
DD(SizeOfArray) = New DataSource 'Create new DataSource.
Dustin.
dcritch
1st - you dimension with variable DD and use DS in the call.
Public DD() As DataSource
DS(Index + 1) = New DataSource
2nd - you havent defined how many elements the array is going to hold. You have just defined it is an array. You could use something like Public DD(10) as datasource to define that the array will have 11 elements ( 0 - 10)
3rd - I dont see the code as to how Index is being established to define what values this will hold. So I have no idea of what values this is referring to.
This is why the arraylist or generics list collection is a nice approach as you dont have to be concerned about how many elements you need to establish - you are just adding elements to the collection.
RSatter
Heres an example that works.... When you want to add the additional element then you redim the array to accomodate a new element.
Public Class Fred
End Class
Public Class Form1
Dim NormalArray() As fred
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim INDEX As Integer
If NormalArray Is Nothing Then
INDEX = 0
Else
INDEX = NormalArray.GetUpperBound(0) + 1
End If
'//Redimension the array to add an addition element
ReDim Preserve NormalArray(INDEX)
NormalArray(INDEX) = New fred
End Sub
End Class
Or you could use an generic list of a specific type of class and just add the items to the list.
Freek Bos
I give it a try for lists thanks again.