Join 107,663 C# Programmers for FREE! Ask your question and get quick answers from experts. There are 1,018 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!
You cannot return two values from a method. You can add both values to something like a Hashtable or Dictionary, but both cannot be returned like you're looking for. A method can return a single item
You cannot return two values from a method. You can add both values to something like a Hashtable or Dictionary, but both cannot be returned like you're looking for. A method can return a single item
Since it is only two values, concatenate them together into a string, seperated by a delimeter and then split them apart when they are returned to the method caller.
Example:
CODE
public string ObtenerPuntos(int PtsLoc, int PtsVis) . . return (PtsLoc.ToString() + "~" + PtsVis.ToString()) }
Since it is only two values, concatenate them together into a string, seperated by a delimeter and then split them apart when they are returned to the method caller.
Example:
CODE
public string ObtenerPuntos(int PtsLoc, int PtsVis) . . return (PtsLoc.ToString() + "~" + PtsVis.ToString()) }
To pass back multiple values, you'll want an object that can contain such values. One option is an array, but that's not particuarly explicit. Another object is an object to hold and pass the values around, like so:
java
class ObtenerPuntosResults { public int PtsLoc; public int PtsVis; }
public ObtenerPuntosResults ObtenerPuntos(int PtsLoc, int PtsVis) { ObtenerPuntosResults results = new ObtenerPuntosResults(); if (PGolLocal > PGolVisitante) { results.PtsLoc = 3; results.PtsVis = 0; } else if (PGolLocal == PGolVisitante) { results.PtsLoc = 1; results.PtsVis = 1; } else { PtsLoc = 0; PtsVis = 3; } return results; }
Upon consideration, it would seem that this should be rewritten to use the two values that you're applying your logic to. This might be more apropriate:
java
class ObtenerPuntosResults { public int PtsLoc; public int PtsVis; public ObtenerPuntosResults(int PGolLocal, int PGolVisitante) { if (PGolLocal > PGolVisitante) { this.PtsLoc = 3; this.PtsVis = 0; } else if (PGolLocal == PGolVisitante) { this.PtsLoc = 1; this.PtsVis = 1; } else { this.PtsLoc = 0; this.PtsVis = 3; } } }
public ObtenerPuntosResults ObtenerPuntos(int PGolLocal, int PGolVisitante) { return new ObtenerPuntosResults(PGolLocal, PGolVisitante); }
Can't you use the in and out to return more than one value?
public int MethodName(int val1, int val2, out val1, out val2, in val1, in val2){
Would that work?
That really depends on whether he wants to change the value of the original variable or just use its value without changing the original variable. Using the "out" key work will pass the object by reference, which means that any changes made to the value in the method will be reflected in the original.
I've found the need for tuples to be quit common that I have it as part of my library. What I've done in the past is use this class (similar to baavgai solution)
CODE
/// <summary> /// This class mimics the STL pair class /// It basically allows a tuple to be represented /// as an object /// </summary> /// <note>This cass is very similar to the KeyValuePair struct. /// However, that class cannot be inherited from</note> public class Pair<T, U> { public Pair() {}
public Pair(T first, U second) { m_first = first; m_second = second; }
virtual public T First { get { return m_first; } }
virtual public U Second { get { return m_second; } }
#region [private] T m_first; U m_second; #endregion };
It would then be used like this
CODE
public Pair<int, int> ObtenerPuntos(int PtsLoc, int PtsVis) { ... previous code return new Pair<int, int>(PtsLoc, PtsVs); }
// In main or whatever driver is being used Pair<int, int> values = ObtenerPuntos(val1, val2); Console.WriteLine(values.First); Console.WriteLine(values.Second);