# How to return two int values from a method!

Page 1 of 1

## 12 Replies - 18828 Views - Last Post: 01 July 2008 - 02:46 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=55400&amp;s=a7893d2be0133ae8319df53e553dc99f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 papuccino1

• His name was Robert Paulson.

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

# How to return two int values from a method!

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

Reputation: 1663
• 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

### #3 rgfirefly24

• D.I.C Lover

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

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

Posted 20 June 2008 - 07:18 PM

PsychoCoder, 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?

### #4 Jayman

• Student of Life

Reputation: 423
• 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.

### #5 reCoded

• D.I.C Regular

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

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

Posted 21 June 2008 - 11:12 AM

jayman9, 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?

### #6 baavgai

• Dreaming Coder

Reputation: 7507
• 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.

### #7 Jayman

• Student of Life

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

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

Posted 22 June 2008 - 11:41 AM

reCoded, 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.

### #8 sphildreth

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

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

Posted 23 June 2008 - 01:22 PM

papuccino1, 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;

}
}

```

### #9 skaoth

Reputation: 91
• 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

### #10 baavgai

• Dreaming Coder

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

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

Posted 23 June 2008 - 05:32 PM

skaoth, 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.

### #11 jtp

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

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

Posted 24 June 2008 - 03:25 AM

papuccino1, 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[] {'\$'})

### #12 cyberstrike

Reputation: 0
• 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.

### #13 turtleC++

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

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

Posted 01 July 2008 - 02:46 PM

papuccino1, 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?