Roey Posted February 3, 2005 Posted February 3, 2005 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..... Quote
Administrators PlausiblyDamp Posted February 3, 2005 Administrators Posted February 3, 2005 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
twistedm1nd Posted February 3, 2005 Posted February 3, 2005 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 Quote
Leaders Iceplug Posted February 3, 2005 Leaders Posted February 3, 2005 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. :-\ Quote Iceplug, USN One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(
Roey Posted February 3, 2005 Author Posted February 3, 2005 Thanks a lot for the advice I went for passing an array as I was using two decimal values. Quote
VBAHole22 Posted February 14, 2005 Posted February 14, 2005 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. Quote Wanna-Be C# Superstar
Roey Posted February 14, 2005 Author Posted February 14, 2005 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 Quote
Administrators PlausiblyDamp Posted February 14, 2005 Administrators Posted February 14, 2005 You might want to post the relevant code snippet - it's a lot easier to give advice when you know the context it will be applied in. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Roey Posted February 14, 2005 Author Posted February 14, 2005 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. Quote
VBAHole22 Posted February 14, 2005 Posted February 14, 2005 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? Quote Wanna-Be C# Superstar
Roey Posted February 14, 2005 Author Posted February 14, 2005 That does make sense I will give it a try and let you know how it goes. Thanks for taking the time to have a look at this post. Quote
donnacha Posted February 16, 2005 Posted February 16, 2005 Have you considered using a collection as the output type, this gives you great flexibility and ease of access to the results Quote Hamlet
John_0025 Posted February 16, 2005 Posted February 16, 2005 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 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.