Passing integer value between two classes as ByRef 
Author Message
 Passing integer value between two classes as ByRef

Hello,

I have created the following two form classes as below. On each form, you
have one button to release some events in order to update values / results.

Even if ByRef is used in order to update variable a_form1 each time if
a_form2 will be changed, the "call by reference" is not really done, it is
call by value. Therefore, variable a_form1 will be kept unchanged.

Can someone explain me the background?

I can only get an update of a_form1 if I use an array of integer instead,
inside form1 and form2. Only then, the value of form2 will be updated to
form1 variable.

Public Class Form1:
Dim a_form1 as integer
 ....
   public sub Button1_click ()
   'It is used to create an instance of form2 only.
      dim frm as Windows.Forms.Form

      frm = New Form2 (a_form1)
   end sub

   public sub Button2_click()
   'It is used to display current value of a_form1.
      textbox1.text = a_form1.tostring
   end sub.
end class.

Public Class Form2
Dim a_form2 As Integer
...
   Public Sub New(ByRef a As Integer)

     ' This call is required by the Windows Form Designer.
     InitializeComponent()

     ' Add any initialization after the InitializeComponent() call.
     a_form2 = a
  End Sub

   Private Sub Button3_Click(ByVal sender As Object, _
                                       ByVal e As System.EventArgs)    
Handles Button3.Click
   'It is used to change value of a_form2 only.
   'Because of ByRef, an update of a_form1 is expected.
         a_form2 += 1
   End Sub
End Class



Mon, 10 Jan 2011 06:20:00 GMT  
 Passing integer value between two classes as ByRef

Quote:
> Hello,

> I have created the following two form classes as below. On each form, you
> have one button to release some events in order to update values / results.

> Even if ByRef is used in order to update variable a_form1 each time if
> a_form2 will be changed, the "call by reference" is not really done, it is
> call by value. Therefore, variable a_form1 will be kept unchanged.

> Can someone explain me the background?

> I can only get an update of a_form1 if I use an array of integer instead,
> inside form1 and form2. Only then, the value of form2 will be updated to
> form1 variable.

> Public Class Form1:
> Dim a_form1 as integer
>  ....
>    public sub Button1_click ()
>    'It is used to create an instance of form2 only.
>       dim frm as Windows.Forms.Form

>       frm = New Form2 (a_form1)
>    end sub

>    public sub Button2_click()
>    'It is used to display current value of a_form1.
>       textbox1.text = a_form1.tostring
>    end sub.
> end class.

> Public Class Form2
> Dim a_form2 As Integer
> ...
>    Public Sub New(ByRef a As Integer)

>      ' This call is required by the Windows Form Designer.
>      InitializeComponent()

>      ' Add any initialization after the InitializeComponent() call.
>      a_form2 = a
>   End Sub

>    Private Sub Button3_Click(ByVal sender As Object, _
>                                        ByVal e As System.EventArgs)    
> Handles Button3.Click
>    'It is used to change value of a_form2 only.
>    'Because of ByRef, an update of a_form1 is expected.
>     a_form2 += 1
>    End Sub
> End Class

This is the wrong place to ask this, since your in the VB.CLASSIC group...
You want to move over to microsoft.public.dotnet.languages.vb for future
posts.

But the simple anser is that an Integer is a value type.  When you assign a to
a_form2 you make a copy of the value.  You'll find that you can't do what your
asking with anything that inherits from System.ValueType.  It works with
arrays, because arrays are reference types.  Reference types are essentially
pointers to the actuall data, so when you do assignment - all that is copied
is a pointer or the address of the data.  

You might want to spend some time researching value types vs. reference types if
the above is unclear...

--
Tom Shelton



Mon, 10 Jan 2011 06:36:07 GMT  
 Passing integer value between two classes as ByRef
Hello Tom,
thanks for the quick response. I coudln't find in the online help this kind
of explanation if I search for "ByRef" or parameters in sub/functions.
But it is clear now, thanks.

Stephan.

Quote:


> > Hello,

> > I have created the following two form classes as below. On each form, you
> > have one button to release some events in order to update values / results.

> > Even if ByRef is used in order to update variable a_form1 each time if
> > a_form2 will be changed, the "call by reference" is not really done, it is
> > call by value. Therefore, variable a_form1 will be kept unchanged.

> > Can someone explain me the background?

> > I can only get an update of a_form1 if I use an array of integer instead,
> > inside form1 and form2. Only then, the value of form2 will be updated to
> > form1 variable.

> > Public Class Form1:
> > Dim a_form1 as integer
> >  ....
> >    public sub Button1_click ()
> >    'It is used to create an instance of form2 only.
> >       dim frm as Windows.Forms.Form

> >       frm = New Form2 (a_form1)
> >    end sub

> >    public sub Button2_click()
> >    'It is used to display current value of a_form1.
> >       textbox1.text = a_form1.tostring
> >    end sub.
> > end class.

> > Public Class Form2
> > Dim a_form2 As Integer
> > ...
> >    Public Sub New(ByRef a As Integer)

> >      ' This call is required by the Windows Form Designer.
> >      InitializeComponent()

> >      ' Add any initialization after the InitializeComponent() call.
> >      a_form2 = a
> >   End Sub

> >    Private Sub Button3_Click(ByVal sender As Object, _
> >                                        ByVal e As System.EventArgs)    
> > Handles Button3.Click
> >    'It is used to change value of a_form2 only.
> >    'Because of ByRef, an update of a_form1 is expected.
> >        a_form2 += 1
> >    End Sub
> > End Class

> This is the wrong place to ask this, since your in the VB.CLASSIC group...
> You want to move over to microsoft.public.dotnet.languages.vb for future
> posts.

> But the simple anser is that an Integer is a value type.  When you assign a to
> a_form2 you make a copy of the value.  You'll find that you can't do what your
> asking with anything that inherits from System.ValueType.  It works with
> arrays, because arrays are reference types.  Reference types are essentially
> pointers to the actuall data, so when you do assignment - all that is copied
> is a pointer or the address of the data.  

> You might want to spend some time researching value types vs. reference types if
> the above is unclear...

> --
> Tom Shelton



Mon, 10 Jan 2011 06:45:02 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Pass integer Value from forms Class mod to report class mod

2. Calling a DLL function from VB with integers passed byref

3. Passing arguments byref to a class

4. comparing two integer values don't work

5. Problem : Unable to pass an integer value from Visual Basic to Fortran DLL

6. Passing value to Integer

7. Public Method in a class calling a private method in same class yields ByRef error

8. Passing value to class between postbacks

9. passing values from one class to another

10. Passing a collection class as a return value

11. Charts with two values and two reaction

12. Swap two integers

 

 
Powered by phpBB® Forum Software