Click to See Complete Forum and Search --> : Passing array's in classes....
advocation
February 28th, 2006, 01:33 PM
Ok, here goes: what i'm trying to do is create my first class, then in my second class create an array of that instance. Here is my code:
Class transaction
Private m_name, m_todayDate As String
Private m_amount, m_change As Double
Public Property name() As String
Get
Return m_name
End Get
Set(ByVal Value1 As String)
m_name = Value1
End Set
End Property
Public Property amount() As Double
Get
Return m_amount
End Get
Set(ByVal Value2 As Double)
m_amount = Value2
End Set
End Property
Public Property todayDate() As String
Get
Return m_todayDate
End Get
Set(ByVal Value3 As String)
m_todayDate = Value3
End Set
End Property
Public Property change() As Double
Get
Return m_change
End Get
Set(ByVal Value4 As Double)
m_change = Value4
End Set
End Property
End Class
Public Class account
Public checking As transaction()
Public savings As transaction()
Public saveBal As Double
Public checkBal As Double
End Class
then...
check = New account
check.checking(1).name = "Hello"
lstTrans.Items.Add(check.checking(1).name)
this will give me an Object reference not set to an instance of an object error. Any ideas???? Thanks!
hspc
February 28th, 2006, 01:43 PM
this is not the way to declare arrays
you'll need to initialize values
consider this piece of code:
Public Class account
Public checking(10) As transaction
private sub New()
checking(0)=new transaction()
checking(1)=new transaction()
.
.
.
End Sub
End Class
advocation
February 28th, 2006, 05:47 PM
Well, i tried this...
dim check as account
Public Class account
Dim checking() As transaction
Dim savings() As transaction
Dim saveBal As Double
Dim checkBal As Double
Sub whatever()
checking(1).name = "hello"
MsgBox(checking(1).name)
End Sub
End Class
and then call in a button sub.
sub button(...)
check = new account
check.whatever()
end sub
And I still get the Object reference not set to an instance of an object error. on checking(1).name = hello.
I have never in the past had to declare an array as a new transaction for everytime that it is being used, or to declare each individual object in the array...
also, it tells me that we cannot do checking(0)=new transaction() because it cannot convert into a 1-dimensional array.
jmcilhinney
February 28th, 2006, 06:45 PM
Think of your array as an egg carton, and the objects as the eggs. Just because you have an egg carton doesn't mean you have any eggs. You just have 12 places where eggs can go. You still have to put eggs in the cups.
This code:Dim checking() As transactiondoes NOT create an array, therefore checking(0) doesn't exist. All that code does is declare a variable that can hold a reference to an array. This is akin to saying here is a place I can put an egg carton. To create an array object you MUST specify the size of the array. This makes sense because how can you have an array if you don't know the size. The following code creates an array with 12 elements:Dim checking(11) As transactionas does this:Dim checking() As transaction
ReDim checking(11)Now you've got an egg carton. but you still don't have any eggs. You can refer to each element of the array by index now though, so you can create new objects and assign them to each element, or assign existing objects. Don't make me talk about laying eggs... ;)
jmcilhinney
February 28th, 2006, 06:51 PM
I strongly suggest that you read up about the difference between reference types and value types. An array itself is a reference type, which is why declaring a variable of an array type doesn't actually create an array. The elements of an array can be reference types or value types, depending on the type of the array. Once you've create an array object, each element behaves exactly like a stand-alone variable of the same type. This means that if you create an Integer array then every element behaves just like an Integer variable. Integer is a value type so you don't have to explicitly create each element, just as you don't have to explicitly create an Integer object. It just contains zero by default. Your transaction class is a reference type, so each element of the array behaves just like a transaction variable. you know that this:Dim trn As transactiondoesn't actually create a transaction object, so an array element should be no different.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.