return more then one value from a class

  • Thread starter Thread starter ericvdb
  • Start date Start date
E

ericvdb

Hi all,

supose i send 3 values to a function which resides in a class.
I want to work with those 3 numbers and return for ex 2 values.

i send value A, B, C to my function and it has to return Y=A-B, Z=C+Y

I know how to return 1 value.

How can I accomplish this?

Eric
 
A function can return a class, an array, a Collection - any object.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

Suresh said:
A function can only return one entity.

Pass the parameters in by reference or use out parameters.

You can also have your function return a struct. Structs are like
classes(light weight) and Y and Z would be it's properties.
 
Hi again,

I have tried the following but failed

I created a new class and put the following code in it:
Public Class ReqProp
Public valA As Integer
Public valB As Integer

Public Function Proposal(ByVal myX As Integer, ByVal myY As Integer,
ByVal myZ As Integer) As ReqProp
valA = myX + myY
valB = valA - myZ
End Function
End Class

In my webform I have 3 textboxes and 2 labels, the code behind:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim NewProposal As ReqProp
NewProposal = Proposal(TextBox1.Text, TextBox2.Text, TextBox3.Text)
Label1.Text = NewProposal.valA
Label2.Text = NewProposal.valB
End Sub

Can someone tell me what i'm doing wrong?
I can not even run this because VS.NET complains that "Name 'Proposal' is
not declared.

Eric
 
I can see Object-oriented programming is new to you. Let me see if I can
help you understand better. There are several things wrong with your code.

1. You defined a class ("ReqProp") with one Method ("Proposal") which is
supposed to return an instance of that class. However, it returns nothing at
all. There is no return statement in it.
2. In your Event Handler, you attempt to create an instance of that class by
calling the "Proposal" Method. However, you didn't create an instance of the
class and refer to that instance to create it. In other words, you can't
simply call "Proposal(...)" from inside a Page class without identifying the
class that the Method is a member of. It certainly is NOT a member of the
Page class. And as the Function "Proposal" is not static (Shared), an
instance of the "ReqProp" class would have to be instantiated in order to
call that Method in it.
3. Your Method definition takes 3 integers. In your Event Handler you are
passing it 3 strings. Do yourself a big favor and turn Option Strict ON!
Coming from a VBScript or VB background, you may not be familiar with data
types. In .Net, it is very important to be aware of them.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
Kevin,

I really appreciate the feedback you gave, however, demonstrating your
feedback with some code should make it more clear to me.
If you have on the other hand some good references regarding OOP, I would
for sure take the time to study them.

Thanks,

Eric
 
Hi Eric,

Well, I could only guess as to how you should write the code. Here's one
variation:

Public Class ReqProp
' These are public fields
Public valA As Integer
Public valB As Integer

' This is a constructor Method
Public Sub New(ByVal myX As Integer, ByVal myY As Integer, ByVal myZ As
Integer)
valA = myX + myY
valB = valA - myZ
End Function
End Class

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim NewProposal As ReqProp
NewProposal = New ReqProp(CInt(TextBox1.Text), CInt(TextBox2.Text),
CInt(TextBox3.Text))
Label1.Text = NewProposal.valA.ToString()
Label2.Text = NewProposal.valB.ToString()
End Sub

There are some inherent problems with this code as it stands. For example,
it doesn't check to make sure that the values from the textboxes are indeed
numbers. Somehow that would have to be handled, as well as any other
potential show-stoppers. But this is a basic idea.

As for references on OOP, I haven't read one in years. But all OOP uses the
same basic rules. Perhaps someone else here can make a recommendation.
 
Back
Top