CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2005
    Posts
    48

    Passing array's in classes....

    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:
    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!

  2. #2
    Join Date
    Apr 2002
    Location
    Egypt
    Posts
    2,210

    Re: Passing array's in classes....

    1. this is not the way to declare arrays
    2. you'll need to initialize values
    consider this piece of code:
    Code:
       Public Class account
    
    	Public checking(10) As transaction
    
    	private sub New()
    		checking(0)=new transaction()
    		checking(1)=new transaction()
    .
    .
    .
    	
    	End Sub
    
    
    
    	End Class
    Hesham A. Amin
    My blog , Articles


    <a rel=https://twitter.com/HeshamAmin" border="0" /> @HeshamAmin

  3. #3
    Join Date
    Mar 2005
    Posts
    48

    Re: Passing array's in classes....

    Well, i tried this...
    Code:
        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.
    Last edited by advocation; February 28th, 2006 at 06:59 PM.

  4. #4
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    1,080

    Re: Passing array's in classes....

    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:
    Code:
    Dim checking() As transaction
    does 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:
    Code:
    Dim checking(11) As transaction
    as does this:
    Code:
    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...
    Tutorials: Home & Learn | Start VB.NET | Learn VB.NET | C# Station | GotDotNet | Games in VB.NET 101 Samples: 2002 | 2003 | 2005 | More .NET 2.0 (VB.NET, C#) Articles: VB.NET | C# | ASP.NET | MoreFree Components: WFC | XPCC | ElementsEx | VBPP | Mentalis | ADO.NET/MySQL | VisualStyles | Charting (NPlot, ZedGraph) | iTextSharp (PDF) | SDF (CF) ● Free Literature: VB 2005 (eBook) | VB6 to VB.NET (eBook) | MSDN Magazine (CHM format) ● Bookmarks: MSDN | WinForms .NET | ASP.NET | WinForms FAQ | WebForms FAQ | GotDotNet | Code Project | DevBuzz (CF) ● Code Converter: C#/VB.NET | VB.NET/C# | VS 2005 add-in

  5. #5
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    1,080

    Re: Passing array's in classes....

    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:
    Code:
    Dim trn As transaction
    doesn't actually create a transaction object, so an array element should be no different.
    Tutorials: Home & Learn | Start VB.NET | Learn VB.NET | C# Station | GotDotNet | Games in VB.NET 101 Samples: 2002 | 2003 | 2005 | More .NET 2.0 (VB.NET, C#) Articles: VB.NET | C# | ASP.NET | MoreFree Components: WFC | XPCC | ElementsEx | VBPP | Mentalis | ADO.NET/MySQL | VisualStyles | Charting (NPlot, ZedGraph) | iTextSharp (PDF) | SDF (CF) ● Free Literature: VB 2005 (eBook) | VB6 to VB.NET (eBook) | MSDN Magazine (CHM format) ● Bookmarks: MSDN | WinForms .NET | ASP.NET | WinForms FAQ | WebForms FAQ | GotDotNet | Code Project | DevBuzz (CF) ● Code Converter: C#/VB.NET | VB.NET/C# | VS 2005 add-in

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured