Welcome to Dream.In.Code
Getting C# Help is Easy!

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!



How to return two int values from a method!

2 Pages V  1 2 >  
Reply to this topicStart new topic

How to return two int values from a method!, HELP ME FAST PLEASE!

papuccino1
post 20 Jun, 2008 - 02:37 PM
Post #1


D.I.C Head

**
Joined: 2 Mar, 2008
Posts: 85


My Contributions


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?

CODE
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 Jun, 2008 - 02:38 PM
User is offlineProfile CardPM

Go to the top of the page


PsychoCoder
post 20 Jun, 2008 - 06:48 PM
Post #2


DIC.Rules == true;

Group Icon
Joined: 26 Jul, 2007
Posts: 7,142



Thanked 50 times

Dream Kudos: 7700

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, GDI

My Contributions


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
User is online!Profile CardPM

Go to the top of the page

rgfirefly24
post 20 Jun, 2008 - 07:18 PM
Post #3


D.I.C Regular

Group Icon
Joined: 7 Apr, 2008
Posts: 307



Thanked 5 times

Dream Kudos: 150
My Contributions


QUOTE(PsychoCoder @ 20 Jun, 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



you can return an Array of ints also cant you?
User is offlineProfile CardPM

Go to the top of the page

jayman9
post 21 Jun, 2008 - 11:03 AM
Post #4


Student of Life

Group Icon
Joined: 26 Dec, 2005
Posts: 6,300



Thanked 21 times

Dream Kudos: 500

Expert In: C#, VB.NET, Java

My Contributions


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


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


Now you have an array arrValues with two elements that contain the values returned.
User is offlineProfile CardPM

Go to the top of the page

reCoded
post 21 Jun, 2008 - 11:12 AM
Post #5


D.I.C Head

**
Joined: 25 Feb, 2008
Posts: 121

QUOTE(jayman9 @ 21 Jun, 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:
CODE

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?
User is offlineProfile CardPM

Go to the top of the page

baavgai
post 21 Jun, 2008 - 11:25 AM
Post #6


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,529



Thanked 41 times

Dream Kudos: 325

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions


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


Hope this helps.
User is offlineProfile CardPM

Go to the top of the page

jayman9
post 22 Jun, 2008 - 11:41 AM
Post #7


Student of Life

Group Icon
Joined: 26 Dec, 2005
Posts: 6,300



Thanked 21 times

Dream Kudos: 500

Expert In: C#, VB.NET, Java

My Contributions


QUOTE(reCoded @ 21 Jun, 2008 - 11:12 AM) *

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.
User is offlineProfile CardPM

Go to the top of the page

sphildreth
post 23 Jun, 2008 - 01:22 PM
Post #8


New D.I.C Head

*
Joined: 20 Jun, 2008
Posts: 6



Thanked 1 times
My Contributions


QUOTE(papuccino1 @ 20 Jun, 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?



Quick way to do it
CODE

        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;

        }
    }


User is offlineProfile CardPM

Go to the top of the page

skaoth
post 23 Jun, 2008 - 03:38 PM
Post #9


D.I.C Regular

Group Icon
Joined: 7 Nov, 2007
Posts: 320



Thanked 5 times

Dream Kudos: 100
My Contributions


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


good luck
User is online!Profile CardPM

Go to the top of the page

baavgai
post 23 Jun, 2008 - 05:32 PM
Post #10


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,529



Thanked 41 times

Dream Kudos: 325

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions


QUOTE(skaoth @ 23 Jun, 2008 - 06:38 PM) *

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.

User is offlineProfile CardPM

Go to the top of the page

jtp
post 24 Jun, 2008 - 03:25 AM
Post #11


New D.I.C Head

*
Joined: 20 Jun, 2008
Posts: 3

QUOTE(papuccino1 @ 20 Jun, 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?

CODE
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[] {'$'})




















User is offlineProfile CardPM

Go to the top of the page

cyberstrike
post 29 Jun, 2008 - 10:57 AM
Post #12


New D.I.C Head

*
Joined: 29 Jun, 2008
Posts: 3

Just use pointers.
User is offlineProfile CardPM

Go to the top of the page

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 8/29/08 10:24PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month