Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

How do you create a function to return two decimal values. I have done a bit of research and found basically three options.

 

1. Pass the values ByRef

2. Return an object

3. Return an array

 

Which one of these three would be the best way.

 

Any examples would be appreciated.....

  • Administrators
Posted

Depends on the type of information being returned really. If it is several 'results' to a function call where each value is the same data type and there is no relationship between the values then an array may be best. e.g. String.Split(...)

If the two values have an intrinsic relationship (StartTime / StopTime, InitialBalance / NewBalance) then I would tend to create a class / struct to contain the values e.g. Point, Size etc.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

A function can return multiple values by tagging the input parameter/s with the out keyword

 

decimal FuncDoSomeThing(out decimal d2,out string msg);

 

 

input parameters passed using out needn't be initialized before calling the method .however after a call the out parameter must be initialized

  • Leaders
Posted

I agree with Damp: go for an array or a structure rather than ByRef/out

 

It gets a little complicated keeping track of procedures that modify inputs, although I suppose you get used to it if you do it a lot. :-\

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

  • 2 weeks later...
Posted

Roey,

 

Just a suggestion.

I ran into the same problem as you, returning 2 values from a function, quite some time back and I posted it here and got some great responses.

What happened after that for me was that I cast aside my procedural programming ways and got on the OOP freight train.

 

If you are looking for a function that will modify two or more things then you can create a class and make properties for all of the things you want to track. Your functio ncan then modify all of these without needing to return anything. You can then access the values of these properties.

 

In fact, since I started getting into the OOP swing of things I don't really find occasion to write functions at all anymore. I realize they are still needed but I use them now more in a 'helper' kind of way. Like if you had a number and you wanted some computation on it.

 

Just my 2 cents on the subject.

Wanna-Be C# Superstar
Posted

That function is inside its own class. I am instantiating the class in order to call the function, which will return two values. Should I be doing this differently ?????

 

Thanks

Posted

Its some code to return the labor and material cost of a manufactured item from a mousedown on a treeview.

 


Dim objRequirements As New elantis_BUS.BusRequirements()
Dim decResult() As Decimal

decResult= objRequirements.BOM_Explosion_Costing_Level_One(CType(Me.tvwBillOfMaterial.SelectedNode.Tag, String), CType(Quantity_From_Node_Description.Remove(0, 1), Decimal), 2)  

Dim decMat As Decimal
Dim decLab As Decimal

decMat = decResult(0)
decLab = decResult(1)

 

The function is a recursion through the treeview that returns the two values in a decimal format.

Posted

I'm still learning this OOP stuff, mind you.

But what I would do is make decMat and decLab properties of the class, and you can have private variables for them if you wish.

 

Then inside your function (which can then be a sub) set the values of these two proerties just like you are already and then in your calling code access the value of the public properties that you have just set.

 

Does that make any sense?

Wanna-Be C# Superstar
Posted

You could use a structure to hold the values.

 

   Public Function myFunction() As Costs


   Dim objRequirements As New elantis_BUS.BusRequirements
   Dim decResult() As Decimal

       decResult = objRequirements.BOM_Explosion_Costing_Level_One(CType(Me.tvwBillOfMaterial.SelectedNode.Tag, String), CType(Quantity_From_Node_Description.Remove(0, 1), Decimal), 2)

       Dim costs As Costs

       costs.decMat = decResult(0)
       costs.decLab = decResult(1)

       Return costs
   End Function

   Public Structure Costs
       Public decMat As Decimal
       Public decLab As Decimal
   End Structure

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...