Assigning class members

J

Jon

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.TextBox2.Text))
Dim z1 As Complex = New Complex(CDbl(Me.TextBox1.Text),
CDbl(Me.TextBox2.Text))
Dim z2 As Complex = New Complex(CDbl(Me.TextBox3.Text),
CDbl(Me.TextBox4.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
 
C

Cor Ligthert [MVP]

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
 
C

Carlos J. Quintero [VB MVP]

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:
http://www.mztools.com
 
P

Phill. W

Jon said:
When I assign one class member to another, any methods that are
applied to one are applied to both variables.

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.
 
R

Roger Rabbit

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.
When I assign one class member to another, any methods that are applied
to one are applied to both variables.

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(complex1 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.Multiply(z1,z2)


RR



I can't get the code below to work
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Calling methods 1
Deserializing a class 1
how can I make this code run faster 8
Classes 1
CLASSES 2
Need a palette expert here 17
some logic for constructor 2
Win API can't retrieve palette from clipboard 8

Top