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.
 
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`.
 
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.
 
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!
 
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.
 
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!
 
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;
        }
 

Latest posts

Back
Top Bottom