Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. ByReference help

ByReference help

Scheduled Pinned Locked Moved Visual Basic
helpquestion
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anonymous
    wrote on last edited by
    #1

    I have a class defined, along these lines: class theClass dim a as Integer public sub new( ByRef inA ) MyBase.New a = inA end sub public sub doSomething() a = 5 end sub end class Now, I call this as so: Dim i as Integer dim b as theClass = new theClass(i) b.doSomething() // print i When I print i, I expected it to be 5, but it shows up as 0. I pass it into the class by reference, so I expected that any change I make to it would show up. Can someone explain why this is behaving the way it is? Thanks...

    D 1 Reply Last reply
    0
    • A Anonymous

      I have a class defined, along these lines: class theClass dim a as Integer public sub new( ByRef inA ) MyBase.New a = inA end sub public sub doSomething() a = 5 end sub end class Now, I call this as so: Dim i as Integer dim b as theClass = new theClass(i) b.doSomething() // print i When I print i, I expected it to be 5, but it shows up as 0. I pass it into the class by reference, so I expected that any change I make to it would show up. Can someone explain why this is behaving the way it is? Thanks...

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      It's returning 0 because you never assigned any value to 'i' anywhere in your code. I think your trying to do something like this:

      Class theClass
      Private a As Integer

      Public Sub New( ByVal inA As Integer )
          a = inA
      End Sub
      
      Public Sub doSomething( ByRef returnValue As Integer )
          returnValue = a
      End Sub
      

      End Class

      Now, to demonstrate ByRef passing:

      Dim b As New theClass( 25 ) ' Passed to the contructor ByVal
      Dim i As Integer = 0
      b.doSomething( i ) ' 'i' passed to doSomething ByRef
      Debug.WriteLine( i.ToString() )

      RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

      A N 2 Replies Last reply
      0
      • D Dave Kreskowiak

        It's returning 0 because you never assigned any value to 'i' anywhere in your code. I think your trying to do something like this:

        Class theClass
        Private a As Integer

        Public Sub New( ByVal inA As Integer )
            a = inA
        End Sub
        
        Public Sub doSomething( ByRef returnValue As Integer )
            returnValue = a
        End Sub
        

        End Class

        Now, to demonstrate ByRef passing:

        Dim b As New theClass( 25 ) ' Passed to the contructor ByVal
        Dim i As Integer = 0
        b.doSomething( i ) ' 'i' passed to doSomething ByRef
        Debug.WriteLine( i.ToString() )

        RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

        A Offline
        A Offline
        Anonymous
        wrote on last edited by
        #3

        No, I really would like the parameter passed into the constructor to be passed by reference. I tried to make the example simple, but some background might be required to see why I wish to do it this way. Basically, the class is a dialog box, and I want to construct it, binding some of its values to some variables, then show the dialog, and have the variables that are bound be updated automatically, without the calling class having to pull each one out individually. At first I thought maybe I needed to assign a value to the original integer, as you suggested, but the following still yields a 1, not a 5, which obviously isnt the desired result. So is there a way to set a member variable within a class to a reference to a variable that has been passed in via the constructor? Class theClass Private a As Integer Public Sub New( ByRef inA As Integer ) a = inA End Sub Public Sub doSomething() a = 5 End Sub End Class Dim i as Integer = 1 Dim b As New theClass( i ) ' Passed to the contructor as reference b.ShowDialog() ' User modifies value...should be 5 now... Debug.WriteLine( i.ToString() ) ' Expect to see 5, instead see 1. So, I understand using ByRef in a method, but am really looking to bind the class member to a variable reference, if that is possible. From what I'm seeing here, I'm not sure it is... Thanks for your help...

        D 2 Replies Last reply
        0
        • A Anonymous

          No, I really would like the parameter passed into the constructor to be passed by reference. I tried to make the example simple, but some background might be required to see why I wish to do it this way. Basically, the class is a dialog box, and I want to construct it, binding some of its values to some variables, then show the dialog, and have the variables that are bound be updated automatically, without the calling class having to pull each one out individually. At first I thought maybe I needed to assign a value to the original integer, as you suggested, but the following still yields a 1, not a 5, which obviously isnt the desired result. So is there a way to set a member variable within a class to a reference to a variable that has been passed in via the constructor? Class theClass Private a As Integer Public Sub New( ByRef inA As Integer ) a = inA End Sub Public Sub doSomething() a = 5 End Sub End Class Dim i as Integer = 1 Dim b As New theClass( i ) ' Passed to the contructor as reference b.ShowDialog() ' User modifies value...should be 5 now... Debug.WriteLine( i.ToString() ) ' Expect to see 5, instead see 1. So, I understand using ByRef in a method, but am really looking to bind the class member to a variable reference, if that is possible. From what I'm seeing here, I'm not sure it is... Thanks for your help...

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          Anonymous wrote: I really would like the parameter passed into the constructor to be passed by reference. I tried to make the example simple, but some background might be required to see why I wish to do it this way. Basically, the class is a dialog box, and I want to construct it, binding some of its values to some variables, then show the dialog, Can't be done using ByRef in the constructor. When the constructor sub returns the variable references are popped off the stack and lost. The references only exist so long as the function that was called is still executing. The moment the function returns, the references are lost. What you want to do, or I should say how you want to do it, would require FAR more code than it's worth. Even using databinding, you would have to bind each of your variables to their respective fields on the dialog form. The best method is to do exactly what you don't want to do ... pull each variable off individually. You'll be doing it no matter what, either way you go... RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

          1 Reply Last reply
          0
          • A Anonymous

            No, I really would like the parameter passed into the constructor to be passed by reference. I tried to make the example simple, but some background might be required to see why I wish to do it this way. Basically, the class is a dialog box, and I want to construct it, binding some of its values to some variables, then show the dialog, and have the variables that are bound be updated automatically, without the calling class having to pull each one out individually. At first I thought maybe I needed to assign a value to the original integer, as you suggested, but the following still yields a 1, not a 5, which obviously isnt the desired result. So is there a way to set a member variable within a class to a reference to a variable that has been passed in via the constructor? Class theClass Private a As Integer Public Sub New( ByRef inA As Integer ) a = inA End Sub Public Sub doSomething() a = 5 End Sub End Class Dim i as Integer = 1 Dim b As New theClass( i ) ' Passed to the contructor as reference b.ShowDialog() ' User modifies value...should be 5 now... Debug.WriteLine( i.ToString() ) ' Expect to see 5, instead see 1. So, I understand using ByRef in a method, but am really looking to bind the class member to a variable reference, if that is possible. From what I'm seeing here, I'm not sure it is... Thanks for your help...

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            Actually, there is a method to do it using ByRef, but it's not flexible. You must pass all of your variables in AND use the same number of parameters. But... In your dialog class, you would create a Sub that takes parameters for all of the fields you want to return from your form. This Sub would call ShowDialog on itself. When the user clicks OK, the Sub would then pull off all the fields and assign them, one by one, to the parameters passed in. So if you want to pull off the values of 15 fields when the user clicks OK, your function would look something like this:.

            Public Sub MyShowDialog(ByRef field1 As String, ByRef field2 As String, ByRef field3 As Integer, ... )
            If Me.ShowDialog() = DialogResult.OK Then ' This is a blocking call so nothing will execute until this returns
            field1 = WhateverControl.Text()
            field2 = WhateverOtherControl1.Text()
            field3 = WhateverOtherControl2.Text()
            ...
            ...
            End If
            End Sub

            Now, when you call the MyShowDialog from your other form:

            Dim myData1 As String
            Dim myData2 As String
            Dim myData3 As Integer
            ...
            ...
            

            myDialog.MyShowDialog( myData1, myData2, myData3, ... )
            ' Now all your variable have their values...

            Remember this though, I said it's possible, to do it this way. It is NOT the best way to pull values off of a form. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

            A 1 Reply Last reply
            0
            • D Dave Kreskowiak

              Actually, there is a method to do it using ByRef, but it's not flexible. You must pass all of your variables in AND use the same number of parameters. But... In your dialog class, you would create a Sub that takes parameters for all of the fields you want to return from your form. This Sub would call ShowDialog on itself. When the user clicks OK, the Sub would then pull off all the fields and assign them, one by one, to the parameters passed in. So if you want to pull off the values of 15 fields when the user clicks OK, your function would look something like this:.

              Public Sub MyShowDialog(ByRef field1 As String, ByRef field2 As String, ByRef field3 As Integer, ... )
              If Me.ShowDialog() = DialogResult.OK Then ' This is a blocking call so nothing will execute until this returns
              field1 = WhateverControl.Text()
              field2 = WhateverOtherControl1.Text()
              field3 = WhateverOtherControl2.Text()
              ...
              ...
              End If
              End Sub

              Now, when you call the MyShowDialog from your other form:

              Dim myData1 As String
              Dim myData2 As String
              Dim myData3 As Integer
              ...
              ...
              

              myDialog.MyShowDialog( myData1, myData2, myData3, ... )
              ' Now all your variable have their values...

              Remember this though, I said it's possible, to do it this way. It is NOT the best way to pull values off of a form. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

              A Offline
              A Offline
              Anonymous
              wrote on last edited by
              #6

              Thank you! Between your last reply and this, I have a much better understanding of what is going on than before, which is what I was really going for. I had already went ahead and resorted to pulling the values out of the form one by one prior to posting, in order to get it to work correctly, but I was confused as to why it hadn't worked as expected. Knowing that the reference goes out of scope when the called method ends is good to know, and will most likely save me some headaches later, so again, many thanks. For curiosity, why do you discourage the work around you provided in this last post, in favor of the other method? This seems to encapsulate the functionality pretty neatly, assuming you are posting a dialog to retrieve relatively few parameter values and the number of those parameters isn't changing. I'm learning all the time, thanks for helping me along the way.

              D 1 Reply Last reply
              0
              • A Anonymous

                Thank you! Between your last reply and this, I have a much better understanding of what is going on than before, which is what I was really going for. I had already went ahead and resorted to pulling the values out of the form one by one prior to posting, in order to get it to work correctly, but I was confused as to why it hadn't worked as expected. Knowing that the reference goes out of scope when the called method ends is good to know, and will most likely save me some headaches later, so again, many thanks. For curiosity, why do you discourage the work around you provided in this last post, in favor of the other method? This seems to encapsulate the functionality pretty neatly, assuming you are posting a dialog to retrieve relatively few parameter values and the number of those parameters isn't changing. I'm learning all the time, thanks for helping me along the way.

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #7

                Most of the time, you're not pulling just a few parameters. It would work for something like a login dialog, but not for an Options/Settings dialog where there could be MANY options. It's also not flexible. If you put up the dialog for two different operations, say one situation where you needed 15 fields returned, and the other where you needed, say 10. You'd have to write two different MyShowDialog functions with the different parameter lists. I'm all for overloading functions, but that makes the dialog less useful because the dialog class now has to know everything about the situations in which it is used and that's just not good practice in an Object Oriented world. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                1 Reply Last reply
                0
                • D Dave Kreskowiak

                  It's returning 0 because you never assigned any value to 'i' anywhere in your code. I think your trying to do something like this:

                  Class theClass
                  Private a As Integer

                  Public Sub New( ByVal inA As Integer )
                      a = inA
                  End Sub
                  
                  Public Sub doSomething( ByRef returnValue As Integer )
                      returnValue = a
                  End Sub
                  

                  End Class

                  Now, to demonstrate ByRef passing:

                  Dim b As New theClass( 25 ) ' Passed to the contructor ByVal
                  Dim i As Integer = 0
                  b.doSomething( i ) ' 'i' passed to doSomething ByRef
                  Debug.WriteLine( i.ToString() )

                  RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                  N Offline
                  N Offline
                  normanordas
                  wrote on last edited by
                  #8

                  This is somewhat tricky so you be watchful with any changes that could occur.You might expect that since this is passed by reference any changes you made to the passed variable would reflect in the instanciated class.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups