473,513 Members | 2,377 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Assigning class members

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

Nov 21 '05 #1
4 1241
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" <no****@jcosby.com> schreef in bericht
news:11**********************@o13g2000cwo.googlegr oups.com...
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

Nov 21 '05 #2
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

"Jon" <no****@jcosby.com> escribió en el mensaje
news:11**********************@o13g2000cwo.googlegr oups.com...
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

Nov 21 '05 #3
"Jon" <no****@jcosby.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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.
Nov 21 '05 #4
> 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 (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

Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
1800
by: Matthew Sims | last post by:
Python Newbie here. This is my first time learning object-oriented programming and trying to break out of the usual Korn/Perl/PHP style of programming. Having some difficulty understand some items....
0
2053
by: Carlos Ribeiro | last post by:
I thought about this problem over the weekend, after long hours of hacking some metaclasses to allow me to express some real case data structures as Python classes. I think that this is something...
8
3933
by: CoolPint | last post by:
I read in books that nested class cannot access private members of nesting class and vice versa unless they are made friends. Somehow, my compiler is letting my nested class member functions access...
22
465
by: ajay | last post by:
Why would a new of object be created without assigning it to any of variable? new A; ??? tx
14
74768
by: Eric Bantock | last post by:
Very basic question I'm afraid. Once an array has been declared, is there a less tedious way of assigning values to its members than the following: myarray=8; myarray=3; myarray=4; myarray=0;...
5
8722
by: Chris | last post by:
Hi, I don't get the difference between a struct and a class ! ok, I know that a struct is a value type, the other a reference type, I understand the technical differences between both, but...
2
4558
by: Stephan Hoffmann | last post by:
Hi, I'm new to std::auto_ptr<> and wanted to use it with a base class and several derived classes. I'm using gcc 3.3.5 and get a compile error I don't know how to resolve when compiling the...
17
2583
by: Calle Pettersson | last post by:
Coming from writing mostly in Java, I have trouble understanding how to declare a member without initializing it, and do that later... In Java, I would write something like public static void...
8
4534
by: =?Utf-8?B?VHJlY2l1cw==?= | last post by:
Hello, Newsgroupians: I have a large class with a lot of member variables. I also have a function in the class that I would like to change ALL Of the member variables. I am trying to assign...
0
7269
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7177
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7123
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5100
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4756
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3248
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3237
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1611
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
811
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.