Calling methods

J

Jon

I have a class for complex numbers and an exponential function. This
event handler does not reach Expz in debug mode:

z = New Complex(CDbl(Real.Text), CDbl(Imag.Text))

Real.Text = CStr(z.Expz().GetReal())
Imag.Text = CStr(z.Expz().GetImaginary())

But when I do this

z = z.Expz()
Real.Text = CStr(z.GetReal())
Imag.Text = CStr(z.GetImaginary())

Or even this

Dim w As Complex = z.Expz()
Real.Text = CStr(z.Expz().GetReal())
Imag.Text = CStr(z.Expz().GetImaginary())

it executes the calculation and displays it. What's up here?


Jon Cosby

************************************************************
Public Class Complex
Private Shared re As Double
Private Shared im As Double

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

Public Shared Function Expz() As Complex
Dim w As Complex

w = New Complex(Exp(re) * Cos(im), Exp(re) * Sin(im))
Return w
End Function
 
B

Bart Mermuys

Hi,

Jon said:
I have a class for complex numbers and an exponential function. This
event handler does not reach Expz in debug mode:

z = New Complex(CDbl(Real.Text), CDbl(Imag.Text))

Real.Text = CStr(z.Expz().GetReal())
Imag.Text = CStr(z.Expz().GetImaginary())

But when I do this

z = z.Expz()
Real.Text = CStr(z.GetReal())
Imag.Text = CStr(z.GetImaginary())

Or even this

Dim w As Complex = z.Expz()
Real.Text = CStr(z.Expz().GetReal())
Imag.Text = CStr(z.Expz().GetImaginary())

it executes the calculation and displays it. What's up here?


Jon Cosby

************************************************************
Public Class Complex
Private Shared re As Double
Private Shared im As Double

Shared means that *all* Complex objects will use the same re and im, i don't
think that's what you want, each Complex number should have its own re and
im. Remove "Shared".
Public Sub New(ByVal x As Double, ByVal y As Double)
re = x
im = y
End Sub

Public Shared Function Expz() As Complex
Dim w As Complex

Same here, The Expz must be applied to one Complex object. Shared methods
are usually called with the class name like Complex.Expz(). This is not
what you want here, remove "Shared".


HTH,
Greetings
 

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

Top