Assigning class members

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jon

    #1

    Assigning class members

    This seems strange, but maybe there's some basic concept I'm missing.
    When I assign one class member to another, any methods that are applied
    to one are applied to both variables.I can't get the code below to work
    (display sum, product and quotient) without re-initializing z1 each
    time. What's the problem here?


    Dim z0 As Complex = New Complex(CDbl(Me .TextBox1.Text) ,
    CDbl(Me.TextBox 2.Text))
    Dim z1 As Complex = New Complex(CDbl(Me .TextBox1.Text) ,
    CDbl(Me.TextBox 2.Text))
    Dim z2 As Complex = New Complex(CDbl(Me .TextBox3.Text) ,
    CDbl(Me.TextBox 4.Text))

    z1.Add(z2) ' Add z1 and z2

    z1 = z0 ' Reassign z0 to z1
    z1.Multiply(z2) ' Multiplies z0!

    z1 = z0
    z1.Divide(z2)


    Public Class Complex
    Private re As Double
    Private im As Double

    Public Sub New(ByVal x As Double, ByVal y As Double)
    re = x
    im = y
    End Sub

  • Cor Ligthert [MVP]

    #2
    Re: Assigning class members

    Jon,

    You do not supply the code that can give insight in what you ask.

    What does by instance the method Complex.Add look like?

    Cor

    "Jon" <nospam@jcosby. com> schreef in bericht
    news:1128979671 .078845.308390@ o13g2000cwo.goo glegroups.com.. .[color=blue]
    > This seems strange, but maybe there's some basic concept I'm missing.
    > When I assign one class member to another, any methods that are applied
    > to one are applied to both variables.I can't get the code below to work
    > (display sum, product and quotient) without re-initializing z1 each
    > time. What's the problem here?
    >
    >
    > Dim z0 As Complex = New Complex(CDbl(Me .TextBox1.Text) ,
    > CDbl(Me.TextBox 2.Text))
    > Dim z1 As Complex = New Complex(CDbl(Me .TextBox1.Text) ,
    > CDbl(Me.TextBox 2.Text))
    > Dim z2 As Complex = New Complex(CDbl(Me .TextBox3.Text) ,
    > CDbl(Me.TextBox 4.Text))
    >
    > z1.Add(z2) ' Add z1 and z2
    >
    > z1 = z0 ' Reassign z0 to z1
    > z1.Multiply(z2) ' Multiplies z0!
    >
    > z1 = z0
    > z1.Divide(z2)
    >
    >
    > Public Class Complex
    > Private re As Double
    > Private im As Double
    >
    > Public Sub New(ByVal x As Double, ByVal y As Double)
    > re = x
    > im = y
    > End Sub
    >[/color]


    Comment

    • Carlos J. Quintero [VB MVP]

      #3
      Re: Assigning class members

      I am not sure if you distinguish between reference types (such as classes)
      and value types (such as structs), but it would be better to use structs
      (Structure in VB.NET) for complex numbers. Structs in .NET can also have
      methods, not only fields. Otherwise, using classes, a statement like:

      z1 = z0

      makes the z1 variable to point to the instance of the z0 variable, rather
      than simply copying the values of z0 to z1 which is likely what you want.

      Review the concept of structs and value types vs reference types in the .NET
      docs.

      --

      Best regards,

      Carlos J. Quintero

      MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
      You can code, design and document much faster.
      Free resources for add-in developers:
      MZ-Tools has a single goal: To make your everyday programming life easier. As an add-in to several Integrated Development Environment (IDEs) from Microsoft, MZ-Tools adds new menus and toolbars to them that provide many new productivity features.


      "Jon" <nospam@jcosby. com> escribió en el mensaje
      news:1128979671 .078845.308390@ o13g2000cwo.goo glegroups.com.. .[color=blue]
      > This seems strange, but maybe there's some basic concept I'm missing.
      > When I assign one class member to another, any methods that are applied
      > to one are applied to both variables.I can't get the code below to work
      > (display sum, product and quotient) without re-initializing z1 each
      > time. What's the problem here?
      >
      >
      > Dim z0 As Complex = New Complex(CDbl(Me .TextBox1.Text) ,
      > CDbl(Me.TextBox 2.Text))
      > Dim z1 As Complex = New Complex(CDbl(Me .TextBox1.Text) ,
      > CDbl(Me.TextBox 2.Text))
      > Dim z2 As Complex = New Complex(CDbl(Me .TextBox3.Text) ,
      > CDbl(Me.TextBox 4.Text))
      >
      > z1.Add(z2) ' Add z1 and z2
      >
      > z1 = z0 ' Reassign z0 to z1
      > z1.Multiply(z2) ' Multiplies z0!
      >
      > z1 = z0
      > z1.Divide(z2)
      >
      >
      > Public Class Complex
      > Private re As Double
      > Private im As Double
      >
      > Public Sub New(ByVal x As Double, ByVal y As Double)
      > re = x
      > im = y
      > End Sub
      >[/color]


      Comment

      • Phill.  W

        #4
        Re: Assigning class members

        "Jon" <nospam@jcosby. com> wrote in message
        news:1128979671 .078845.308390@ o13g2000cwo.goo glegroups.com.. .[color=blue]
        > When I assign one class member to another, any methods that are
        > applied to one are applied to both variables.[/color]

        When you assign one Object [Reference] variable to another, you
        are copying the "value" of the variable, which is a "Pointer" to the
        object itself. You are /not/ assigning a copy of the entire object.

        So, when you try to change "each" object through your two
        "different" variables, they are both actually "pointing" at the same
        instance of the object.

        To create a [complete] copy of an object, you have to write your
        own method to do so, copying the object field by field. This is
        frequently done by implementing the IClonable Interface.

        HTH,
        Phill W.


        Comment

        • Roger Rabbit

          #5
          Re: Assigning class members

          > This seems strange, but maybe there's some basic concept I'm missing.

          Do a little reading on the difference between ByRef and ByVal and it will
          become very clear. Sorry i dont have a Url handy buts its in the
          documentation.
          [color=blue]
          > When I assign one class member to another, any methods that are applied
          > to one are applied to both variables.[/color]

          You need to understand that an instance z0,z1,z2 of your Complex class is
          just a pointer to space in memory. When you z1=z0, you are no longer
          pointing z1 at whatever memory space it was previously pointing at but
          instead you are poinitng it at the z0 memory space. Which is why

          z1.Multiply(z2) ' Multiplies z0!

          You have 2 different variables z0 & z1 both pointing at the same memory
          space.

          z0.Multiply(z2) ' Multiplies z1!....as well.

          Maybe this is what you want?

          Public Shared Function Multiply(comple x1 as complex, complex2 as complex) as
          Complex
          dim x,y as double
          x = complex1.X * complex2.X
          y = complex1.Y * complex2.Y
          return New Complex(x,y)
          End Function

          Dim result as Complex
          result = Complex.Multipl y(z1,z2)


          RR



          I can't get the code below to work[color=blue]
          > (display sum, product and quotient) without re-initializing z1 each
          > time. What's the problem here?
          >
          >
          > Dim z0 As Complex = New Complex(CDbl(Me .TextBox1.Text) ,
          > CDbl(Me.TextBox 2.Text))
          > Dim z1 As Complex = New Complex(CDbl(Me .TextBox1.Text) ,
          > CDbl(Me.TextBox 2.Text))
          > Dim z2 As Complex = New Complex(CDbl(Me .TextBox3.Text) ,
          > CDbl(Me.TextBox 4.Text))
          >
          > z1.Add(z2) ' Add z1 and z2
          >
          > z1 = z0 ' Reassign z0 to z1
          > z1.Multiply(z2) ' Multiplies z0!
          >
          > z1 = z0
          > z1.Divide(z2)
          >
          >
          > Public Class Complex
          > Private re As Double
          > Private im As Double
          >
          > Public Sub New(ByVal x As Double, ByVal y As Double)
          > re = x
          > im = y
          > End Sub
          >[/color]


          Comment

          Working...