Results 1 to 14 of 14

Thread: pass by ref question

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 1999
    Location
    Australia
    Posts
    115
    In Class1, there's a public variable Flag of type boolean.
    The defined class is used in most forms, so a variable is defined in the class instead declaring the same variables on all required forms.

    When I call a function, passing this calss variable as parameter (by reference) to change the value of Flag, the value does not change. Why?

    Code:
    ' code declared in a form
    private sub Command1_Click()
      dim c as new class1
      c.Flag = False
      sub1 c.flag
      msgbox c.Flag     ' The value is still false afterward
    end sub
    
    ' code declared in main module
    private sub sub1(Value as boolean)
       Value = True
    end sub
    I'm using VB5 SP3.

  2. #2
    Addicted Member
    Join Date
    Jun 1999
    Location
    Los Angeles
    Posts
    186
    Hi Carolyn,
    Try putting the c.Flag into a variable and then work with the variable like:

    ' code declared in a form
    Private Sub Command1_Click()
    Dim Value As Boolean
    Dim c As New Class1
    c.Flag = False
    Value = c.Flag 'Put c.Flag into Value
    sub1 Value
    MsgBox Value ' Now Value assumes the value TRUE
    End Sub

    ' code declared in main module
    Private Sub sub1(Value As Boolean)
    Value = True
    End Sub

    André

  3. #3
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845
    tht's not the reason FantastichenEin it's nowt to do with scope because c.flag is not being refernced within the sub1

    Mark
    -------------------

  4. #4
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658
    I have to agree with Mark on this one. What you should be looking at is a function.

    Code:
    ' code declared in a form
    private sub Command1_Click()
      dim c as new class1
      c.Flag = False
      c.flag = sub1
      msgbox c.Flag     ' The value is still false afterward
    end sub
    
    ' code declared in main module
    private Function sub1() As Boolean
       sub1 = True
    end sub
    Iain, thats with an i by the way!

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Why are you guys making things so complicated when they aren't?
    private Function sub1() As Boolean
    sub1 = True
    end sub
    Replace that function with a single keyword: True

    And for the class1, are you sure you have a public variable named flag that is boolean? Or is it a property get/let? You may have a bug in that.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Oct 1999
    Location
    Australia
    Posts
    115
    Thank you for all your replies.

    'Flag' in the class is declared as a public variable, not property.

    I have serveral functions that need to do something, depending on the Flag, and some set the flag to T or F after doing something.

    I've done serveral tests. The value of 'Flag' just refused to change when passed as parameter.

    Isn't passing the variable by reference means passing the address, and the function which sets this variable access the address directly?


  7. #7
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Boulder, Colorado, USA
    Posts
    325
    Passing Class data Members by reference to functions does not work in VB.

    Classes just plan suck in VB, although they are getting better. Go Java & C++!

    Just my $0.03

    -Shickadance

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Oct 1999
    Location
    Australia
    Posts
    115
    MrShickadance

    Thank you for confirming that class meembers cannot be passed by reference.

    I agre that VB is not a good development tool. It doesn't have the OO that other tools (SQL*Windows and PowerBuilder) provide. I've had various problems with it. Unfortunately, I'm stuck at the moment. I'll eventually move to C++ or Delphi.

  9. #9
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Japan
    Posts
    840
    Public Vars in Classes seems to go against the whole point of OO though if you ask me. This works though...

    Code:
    'class mod
    
    Private bFlag As Boolean
    
    Public Property Get Flag() As Boolean
         Flag = bFlag
    End Property
    
    
    Public Property Let Flag(Value1 As Boolean)
        bFlag = Value1
    End Property
    
    ' form code
    
    
    Private Sub Command1_Click()
      Dim C As Class1
      Set C = New Class1
      C.Flag = False
      Call sub1(C)
      MsgBox C.Flag     ' The value is Now True !!
    
    End Sub
    
    Public Sub sub1(ByRef Value As Class1)
       Value.Flag = True
    End Sub
    If you're using public vars, just stick to modules, every thing is simpler.

    This (OO) way is more code but OO always is as the extra property code is what makes it reusable

    My 4c worth

    Cheers

    Paul Dwyer
    Network Engineer
    Aussie In Tokyo

    Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)

  10. #10
    Hi guys

    You can not pass the class memebers by ref to a function or sub procedure. But you can pass the entire class as by ref.

    The following code works fine.

    Code:
    '<<<CLASS MODULE (Class1.cls)>>>
    
    Public Flag As Boolean
    
    
    '<<<FORM MODULE  (Form1.frm)>>>
    
    
    Public Sub Sub1(ByRef a As Object)
      a.flag = True
    End Sub
    
    Private Sub Command1_Click()
      dim c as new class1
      c.Flag = False
      Sub1 c
      MsgBox c.Flag
    End Sub
    Madhusudana Gorthi [email protected]
    Senior Software Engineer
    www.stgil.com

  11. #11
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Japan
    Posts
    840
    Isn't that what I did?

    (just without the Public Var)

    Is it worse to misunderstood than ignored???


    See Ya

    Paul Dwyer
    Network Engineer
    Aussie In Tokyo

    Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)

  12. #12
    New Member
    Join Date
    May 2000
    Location
    RTP
    Posts
    10
    I should really get some work done, but...

    Yes, Carolyn, VB's OO abilities (inabilities?) are questionable at best.

    To everyone who suggested band-aiding the problem with a return instead of a pass-by-reference. For shame, you people need to get out and learn a language that supports classes and pass-by-reference/value correctly.

    I have a found something odd in using API calls to Registry queries. I pass in a buffer, and have to explicitly say "ByVal" in the function CALL. The function sets the queried value to the buffer I pass, and returns a pass/fail code.

    Troubles me.

    -Travis
    Travis, BS in CSC
    MQ/Series Administration
    VB6E on NT4sp4

  13. #13
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    I agree, don't mix up objects with vars.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  14. #14
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Japan
    Posts
    840
    I'm not going to defend VB's OO implimentation but it's all we have till the next version so...

    Use what works, the language will improve (although no faster for whinging avout it)

    Or go buy BCB
    Paul Dwyer
    Network Engineer
    Aussie In Tokyo

    Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)

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