12 Replies - 18828 Views - Last Post: 01 July 2008 - 02:46 PM Rate Topic: -----

#1 papuccino1   User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

How to return two int values from a method!

Post icon  Posted 20 June 2008 - 02:37 PM

I want that at the end of all the if statements, that values PtsLoc and PtsVis be returned to whatever other method calls this method.

HELP PLEASE! this is the final key to my puzzle and homework! What's the syntax?

public int ObtenerPuntos(int PtsLoc, int PtsVis)
		{
			if (PGolLocal > PGolVisitante)
			{
				PtsLoc = 3;
				PtsVis = 0;
			}
			else
				if (PGolLocal == PGolVisitante)
				{
					PtsLoc = 1;
					PtsVis = 1;
				}
				else
				{
					PtsLoc = 0;
					PtsVis = 3;
				}
			

		}

This post has been edited by papuccino1: 20 June 2008 - 02:38 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How to return two int values from a method!

#2 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1663
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: How to return two int values from a method!

Posted 20 June 2008 - 06:48 PM

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
Was This Post Helpful? 0
  • +
  • -

#3 rgfirefly24   User is offline

  • D.I.C Lover
  • member icon


Reputation: 473
  • View blog
  • Posts: 2,221
  • Joined: 07-April 08

Re: How to return two int values from a method!

Posted 20 June 2008 - 07:18 PM

View PostPsychoCoder, on 20 Jun, 2008 - 06:48 PM, said:

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 can return an Array of ints also cant you?
Was This Post Helpful? 0
  • +
  • -

#4 Jayman   User is offline

  • Student of Life
  • member icon

Reputation: 423
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: How to return two int values from a method!

Posted 21 June 2008 - 11:03 AM

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:
public string ObtenerPuntos(int PtsLoc, int PtsVis)
.
.
return (PtsLoc.ToString() + "~" + PtsVis.ToString())
}


string values = ObtenerPuntos(5, 4);
string arrValues[] = values.Split("~");



Now you have an array arrValues with two elements that contain the values returned.
Was This Post Helpful? 0
  • +
  • -

#5 reCoded   User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 282
  • Joined: 25-February 08

Re: How to return two int values from a method!

Posted 21 June 2008 - 11:12 AM

View Postjayman9, on 21 Jun, 2008 - 11:03 AM, said:

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:
public string ObtenerPuntos(int PtsLoc, int PtsVis)
.
.
return (PtsLoc.ToString() + "~" + PtsVis.ToString())
}


string values = ObtenerPuntos(5, 4);
string arrValues[] = values.Split("~");



Now you have an array arrValues with two elements that contain the values returned.


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?
Was This Post Helpful? 0
  • +
  • -

#6 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: How to return two int values from a method!

Posted 21 June 2008 - 11:25 AM

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:

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:

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);
}



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#7 Jayman   User is offline

  • Student of Life
  • member icon

Reputation: 423
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: How to return two int values from a method!

Posted 22 June 2008 - 11:41 AM

View PostreCoded, on 21 Jun, 2008 - 11:12 AM, said:

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.
Was This Post Helpful? 0
  • +
  • -

#8 sphildreth   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 20-June 08

Re: How to return two int values from a method!

Posted 23 June 2008 - 01:22 PM

View Postpapuccino1, on 20 Jun, 2008 - 02:37 PM, said:

I want that at the end of all the if statements, that values PtsLoc and PtsVis be returned to whatever other method calls this method.

HELP PLEASE! this is the final key to my puzzle and homework! What's the syntax?


Quick way to do it
		public int[] ObtenerPuntos(int PtsLoc, int PtsVis)
		{
			int[] ints = { 0, 0 };
			int ptsLocPos = 0;
			int ptsVisPos = 1;

			if (PGolLocal > PGolVisitante)
			{
				ints[ptsLocPos] = 3;
				ints[ptsVisPos] = 0;
			}
			else if (PGolLocal == PGolVisitante)
			{
				ints[ptsLocPos] = 1;
				ints[ptsVisPos] = 1;
			}
			else
			{
				ints[ptsLocPos] = 0;
				ints[ptsVisPos] = 3;
			}
			return ints;

		}
	}


Was This Post Helpful? 0
  • +
  • -

#9 skaoth   User is offline

  • D.I.C Addict
  • member icon

Reputation: 91
  • View blog
  • Posts: 601
  • Joined: 07-November 07

Re: How to return two int values from a method!

Posted 23 June 2008 - 03:38 PM

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)

	/// <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

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);



good luck
Was This Post Helpful? 0
  • +
  • -

#10 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: How to return two int values from a method!

Posted 23 June 2008 - 05:32 PM

View Postskaoth, on 23 Jun, 2008 - 06:38 PM, said:

I've found the need for tuples to be quit common


Agreed. An FYI, if you don't want to roll your own, you can leverage the same builtin structure that the Dictionary class does. It's the KeyValuePair.
Was This Post Helpful? 0
  • +
  • -

#11 jtp   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 20-June 08

Re: How to return two int values from a method!

Posted 24 June 2008 - 03:25 AM

View Postpapuccino1, on 20 Jun, 2008 - 02:37 PM, said:

I want that at the end of all the if statements, that values PtsLoc and PtsVis be returned to whatever other method calls this method.

HELP PLEASE! this is the final key to my puzzle and homework! What's the syntax?

public int ObtenerPuntos(int PtsLoc, int PtsVis)
		{
			if (PGolLocal > PGolVisitante)
			{
				PtsLoc = 3;
				PtsVis = 0;
			}
			else
				if (PGolLocal == PGolVisitante)
				{
					PtsLoc = 1;
					PtsVis = 1;
				}
				else
				{
					PtsLoc = 0;
					PtsVis = 3;
				}
			

		}


u can return it on an indirect way .by giving the outputs to a String varible

(String mstr+= ptsloc+'$' )

and after returning the string u can retreive that values using split(new char[] {'$'})
Was This Post Helpful? 0
  • +
  • -

#12 cyberstrike   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-June 08

Re: How to return two int values from a method!

Posted 29 June 2008 - 10:57 AM

Just use pointers.
Was This Post Helpful? 0
  • +
  • -

#13 turtleC++   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 132
  • Joined: 07-May 08

Re: How to return two int values from a method!

Posted 01 July 2008 - 02:46 PM

View Postpapuccino1, on 20 Jun, 2008 - 02:37 PM, said:

I want that at the end of all the if statements, that values PtsLoc and PtsVis be returned to whatever other method calls this method.

HELP PLEASE! this is the final key to my puzzle and homework! What's the syntax?

public int ObtenerPuntos(int PtsLoc, int PtsVis)
		{
			if (PGolLocal > PGolVisitante)
			{
				PtsLoc = 3;
				PtsVis = 0;
			}
			else
				if (PGolLocal == PGolVisitante)
				{
					PtsLoc = 1;
					PtsVis = 1;
				}
				else
				{
					PtsLoc = 0;
					PtsVis = 3;
				}
			

		}



based on your code right now, I don't think it can return two values as matter fact it doesn't return any value so it will have error. Can you use reference instead?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1