Trying to Obtain Two (2) Values from Function
-
Hello, In the following function, I am trying to return two (2) values instead of one. Can someone please say how this can be done? I was thinking possibly returning an array from the function, but I do not know if this is possible. Does anyone have an idea of how multiple values can be obtain from the same function? Thanks.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Testing_Functions
{
class Program
{
public static Double Test(Double Val1, Double Val2, Double Val3)
{
Double Test_Val = Val1 + Val2 + Val3;
Double Second_Test = Test_Val % 2;
return Test_Val;
//return Second_Test;//I need to return this value also(two values, NOT ONE.)
}
static void Main(string[] args)
{
Double A = 1, B = 2, C = 3;
Double Result = Test(A,B,C);
//Double Result_2 = Test(A, B, C);
}
}
} -
Hello, In the following function, I am trying to return two (2) values instead of one. Can someone please say how this can be done? I was thinking possibly returning an array from the function, but I do not know if this is possible. Does anyone have an idea of how multiple values can be obtain from the same function? Thanks.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Testing_Functions
{
class Program
{
public static Double Test(Double Val1, Double Val2, Double Val3)
{
Double Test_Val = Val1 + Val2 + Val3;
Double Second_Test = Test_Val % 2;
return Test_Val;
//return Second_Test;//I need to return this value also(two values, NOT ONE.)
}
static void Main(string[] args)
{
Double A = 1, B = 2, C = 3;
Double Result = Test(A,B,C);
//Double Result_2 = Test(A, B, C);
}
}
}There are a few ways of obtaining multiple values. 1. Create an struct/class that holds more than one value 2. Use an array or list (the same as 1 really) 3. Use an out (or ref if needed) parameter. 1:
public class Pair<T>
{
private T first;
private T second;public Pair(T first, T second) { this.first = first; this.second = second; } public T First { get { return first; } } public T Second { get { return second; } }
}
public static Double Test(Double Val1, Double Val2, Double Val3)
{
Double Test_Val = Val1 + Val2 + Val3;
Double Second_Test = Test_Val % 2;
return new Pair<Double>(Test_Val, Second_Test);
}2 should be straight forward. 3:
public static Double Test(Double Val1, Double Val2, Double Val3, out Double secondResult)
{
Double Test_Val = Val1 + Val2 + Val3;
secondResult = Test_Val % 2;
return Test_Val;
}Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
There are a few ways of obtaining multiple values. 1. Create an struct/class that holds more than one value 2. Use an array or list (the same as 1 really) 3. Use an out (or ref if needed) parameter. 1:
public class Pair<T>
{
private T first;
private T second;public Pair(T first, T second) { this.first = first; this.second = second; } public T First { get { return first; } } public T Second { get { return second; } }
}
public static Double Test(Double Val1, Double Val2, Double Val3)
{
Double Test_Val = Val1 + Val2 + Val3;
Double Second_Test = Test_Val % 2;
return new Pair<Double>(Test_Val, Second_Test);
}2 should be straight forward. 3:
public static Double Test(Double Val1, Double Val2, Double Val3, out Double secondResult)
{
Double Test_Val = Val1 + Val2 + Val3;
secondResult = Test_Val % 2;
return Test_Val;
}Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)out and ref parameters are usually considered to be dirty, so stay away from that if you can. Depending on what the function does, you may want to create a wrapper class or struct for the result. That means that you get semantically useful names, instead of First and Second from the Pair class.
-
Hello, In the following function, I am trying to return two (2) values instead of one. Can someone please say how this can be done? I was thinking possibly returning an array from the function, but I do not know if this is possible. Does anyone have an idea of how multiple values can be obtain from the same function? Thanks.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Testing_Functions
{
class Program
{
public static Double Test(Double Val1, Double Val2, Double Val3)
{
Double Test_Val = Val1 + Val2 + Val3;
Double Second_Test = Test_Val % 2;
return Test_Val;
//return Second_Test;//I need to return this value also(two values, NOT ONE.)
}
static void Main(string[] args)
{
Double A = 1, B = 2, C = 3;
Double Result = Test(A,B,C);
//Double Result_2 = Test(A, B, C);
}
}
}Use ref and out keyword to return multiple values from function
return statement returns only single value. -
There are a few ways of obtaining multiple values. 1. Create an struct/class that holds more than one value 2. Use an array or list (the same as 1 really) 3. Use an out (or ref if needed) parameter. 1:
public class Pair<T>
{
private T first;
private T second;public Pair(T first, T second) { this.first = first; this.second = second; } public T First { get { return first; } } public T Second { get { return second; } }
}
public static Double Test(Double Val1, Double Val2, Double Val3)
{
Double Test_Val = Val1 + Val2 + Val3;
Double Second_Test = Test_Val % 2;
return new Pair<Double>(Test_Val, Second_Test);
}2 should be straight forward. 3:
public static Double Test(Double Val1, Double Val2, Double Val3, out Double secondResult)
{
Double Test_Val = Val1 + Val2 + Val3;
secondResult = Test_Val % 2;
return Test_Val;
}Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Also, he could return a
Tuple<_type_,_type_>
but by far the best is solution 1.Sort of a cross between Lawrence of Arabia and Dilbert.[^]
-Or-
A Dead ringer for Kate Winslett[^] -
Hello, In the following function, I am trying to return two (2) values instead of one. Can someone please say how this can be done? I was thinking possibly returning an array from the function, but I do not know if this is possible. Does anyone have an idea of how multiple values can be obtain from the same function? Thanks.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Testing_Functions
{
class Program
{
public static Double Test(Double Val1, Double Val2, Double Val3)
{
Double Test_Val = Val1 + Val2 + Val3;
Double Second_Test = Test_Val % 2;
return Test_Val;
//return Second_Test;//I need to return this value also(two values, NOT ONE.)
}
static void Main(string[] args)
{
Double A = 1, B = 2, C = 3;
Double Result = Test(A,B,C);
//Double Result_2 = Test(A, B, C);
}
}
}the quick and dirty way:
public static Double[] Test(Double Val1, Double Val2, Double Val3)
{Double Test\_Val = Val1 + Val2 + Val3; Double Second\_Test = Test\_Val % 2; double \[\] arr = { Test\_Val, Second\_Test }; return arr; //return Second\_Test;//I need to return this value also(two values, NOT ONE.) }
Just to show you, you can return virtually anything, including Lists, Dictionaries, self made object (like the Pair example)
V.
-
Hello, In the following function, I am trying to return two (2) values instead of one. Can someone please say how this can be done? I was thinking possibly returning an array from the function, but I do not know if this is possible. Does anyone have an idea of how multiple values can be obtain from the same function? Thanks.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Testing_Functions
{
class Program
{
public static Double Test(Double Val1, Double Val2, Double Val3)
{
Double Test_Val = Val1 + Val2 + Val3;
Double Second_Test = Test_Val % 2;
return Test_Val;
//return Second_Test;//I need to return this value also(two values, NOT ONE.)
}
static void Main(string[] args)
{
Double A = 1, B = 2, C = 3;
Double Result = Test(A,B,C);
//Double Result_2 = Test(A, B, C);
}
}
}A Class (object) is the best way. But if you do not have more than two values, you can always use a Dictionary<>, or KeyValuePair etc System.Collections.Generic.Dictionary : http://msdn.microsoft.com/en-us/library/xfhwa508.aspx KeyValuePair : http://msdn.microsoft.com/en-us/library/5tbh8a42.aspx