CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 1999
    Location
    Trivandrum, Kerala, INDIA.
    Posts
    32

    Passing Classes as arguments to other Classes

    I am not able to pass classes to other classes.

    In Class1, put the code



    public A as Byte





    In Form1, put the code




    private Sub Form_Load()

    Dim X as new Class1
    Dim Coll as new Collection

    X.A=25

    Coll.Add X

    'BookMark1

    X.A=6

    Coll.Add X

    End Sub





    When I make "X.A=6", the value of the first item in the collection also becomes "6". So, at BookMark1, I added the statement "Set X=Nothing". Is that necessary, or have I made a mistake somewhere? I have the same problem whenever I pass Objects to another Object.

    Deepak


  2. #2
    Join Date
    May 1999
    Posts
    3,332

    Re: Passing Classes as arguments to other Classes

    you have been using the same reference. Of course, the value changed in X.
    code like this:

    dim x as class1
    set x = new class1
    x.a = 25
    col1.add x
    set x = new class1
    x.a = 6
    col1.add x





  3. #3
    Join Date
    Sep 1999
    Location
    Trivandrum, Kerala, INDIA.
    Posts
    32

    Re: Passing Classes as arguments to other Classes

    Suppose I have a sub like



    private Sub A( byval B as Class1)

    'Code Here
    'More Code

    End Sub




    Do I have to "Set B=Nothing" before I end the Sub? Or will this be done automatically?

    Deepak



  4. #4
    Join Date
    May 1999
    Posts
    3,332

    Re: Passing Classes as arguments to other Classes

    IMHO you should not do it


  5. #5
    Join Date
    Sep 1999
    Location
    Trivandrum, Kerala, INDIA.
    Posts
    32

    Re: Passing Classes as arguments to other Classes

    What's IMHO?


  6. #6
    Join Date
    May 1999
    Posts
    3,332

    Re: Passing Classes as arguments to other Classes

    common acronym in Email:
    In my humble opinion


  7. #7
    Join Date
    Sep 1999
    Posts
    202

    Re: Passing Classes as arguments to other Classes

    I hope you understand difference between passing arguments ByVal and ByRef.
    (If you pass variable ByVal, your sub cannot change original value of variable.)

    For OBJECT variables, behaviour is different.
    If you pass OBJECT variable ByVal, you CAN modify original Object properties, but you cannot modify its address (i.e. you cannot Set it to another object or destroy it).
    See: http://www.**************/Item.asp?Pa...takeBank&ID=11

    Since you are passing your object ByVal: Sub A(byval B as Class1) I thought you should have this info.


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