int method to return string?

jmair

New member
Joined
May 12, 2015
Messages
4
Programming Experience
1-3
I apologize that this a simple question, but the solution escapes me.
I'm passing an int to a method but want to return a string.
C#:
  public static int Example(int x )
        {
            string myString = "";
            
            if(x < 10)
            {
                myString = "less than ten.";                
            }
            else
            {
                myString = "greater than ten.";
            }


            //return a string of what x means.
            return myString;
        }
Thank you for the assistance.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,130
Location
Sydney, Australia
Programming Experience
10+
Your method is declared as returning an `int`:
C#:
public static [B][U]int[/U][/B] Example(int x )
If you declare it as returning an `int` then it must return an `int`. If you want it to return a `string` then declare it as declaring a `string`.
 

Socarsky

Well-known member
Joined
Mar 3, 2014
Messages
59
Programming Experience
Beginner
I apologize that this a simple question, but the solution escapes me.
I'm passing an int to a method but want to return a string.
Your approach is totally wrong, the method's return type must be string as jimcilhinney emphasis, then you can get a string value.
 

jmair

New member
Joined
May 12, 2015
Messages
4
Programming Experience
1-3
Your approach is totally wrong, the method's return type must be string as jimcilhinney emphasis, then you can get a string value.
If it was correct, I don't know if I would have posted the question in the first place =)

But if the solution is to pass a string, parse it to an int and return the string back, I can do that. Never a bad idea to ask if there is a more elegant solution to help learn though.
Thank you!
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,130
Location
Sydney, Australia
Programming Experience
10+
But if the solution is to pass a string, parse it to an int and return the string back, I can do that. Never a bad idea to ask if there is a more elegant solution to help learn though.
Why would that be the solution? I've already told you what the problem is and what the solution is. You have declared your method's return type as `int` and you need to declare it as `string`. It's that simple.
 

jmair

New member
Joined
May 12, 2015
Messages
4
Programming Experience
1-3
Why would that be the solution? I've already told you what the problem is and what the solution is. You have declared your method's return type as `int` and you need to declare it as `string`. It's that simple.
I think we're saying the same thing. I'll post a solution for anyone else looking for a workaround.
C#:
int x = 11;


Example(Convert.ToString(x));


 public static string Example(string x )
        {
            string myString = "";
        int iNum = Convert.ToInt32(x);        


            
            if(iNum < 10)
            {
                myString = "less than ten.";                
            }
            else
            {
                myString = "greater than ten.";
            }




            //return a string of what x means.
            return myString;
        }
Please feel free to post a newer solution or a more elegant way of achieving this.
Thanks!
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,130
Location
Sydney, Australia
Programming Experience
10+
I think we're saying the same thing. I'll post a solution for anyone else looking for a workaround.
C#:
int x = 11;


Example(Convert.ToString(x));


 public static string Example(string x )
        {
            string myString = "";
        int iNum = Convert.ToInt32(x);        


            
            if(iNum < 10)
            {
                myString = "less than ten.";                
            }
            else
            {
                myString = "greater than ten.";
            }




            //return a string of what x means.
            return myString;
        }
Please feel free to post a newer solution or a more elegant way of achieving this.
Thanks!
No, we were not saying the same thing and you have not done what I said, or rather you have done what I said but also more that I didn't say. If more was required then I would have said more was required. What I said was to change the type of the method, NOT the parameter. Here's your original method, including the highlighting that I provided in my first reply:
C#:
        public static [B][U]int[/U][/B] Example(int x )
        {
            string myString = "";
            
            if(x < 10)
            {
                myString = "less than ten.";                
            }
            else
            {
                myString = "greater than ten.";
            }


            //return a string of what x means.
            return myString;
        }
Here's what your method would have looked like if you had followed my advice and only my advice:
C#:
        public static [B][U]string[/U][/B] Example(int x )
        {
            string myString = "";
            
            if(x < 10)
            {
                myString = "less than ten.";                
            }
            else
            {
                myString = "greater than ten.";
            }


            //return a string of what x means.
            return myString;
        }
 

jmair

New member
Joined
May 12, 2015
Messages
4
Programming Experience
1-3
lol, well yes. I suppose you could also do it that way :adoration:. Thanks for clearign that up jmcolhinney
 
Top Bottom