0

I am fairly new to c# and I can't figure this out. The error says that I cannot convert string to int implicitly. Here is a snippet of my code. Thanks!

    private static int GenerateLetters(int size, bool lowercase) //added a return type
    {
        {
        string randomLetters = string.Empty;
        Random r = new Random();

        for (int i = 0; i < size; i++)
        {
            randomLetters += Convert.ToChar(r.Next(65, 90));                
        }

        if (lowercase)
            return randomLetters.ToLower();
        else
            return randomLetters.ToString();
            }
        }
1
  • here you are converting randomLetters into string using tostring and returning it... and in heading you have taken int value... that's why you are getting this error... Please change either of them.. Commented Jan 29, 2014 at 11:23

2 Answers 2

2

You should change return type of your method to string

private static string GenerateLetters(...)

By the way it would be better if you change your method like this:

private static string GenerateLetters(int size, bool lowercase) //added a return type
{

     char[] chars = new char[size];
     Random r = new Random();

     for (int i = 0; i < size; i++)
     {
         chars[i] = Convert.ToChar(r.Next(65, 90));
     }

    if (lowercase)
           return new String(chars).ToLower();
     else
          return new String(chars);

}
Sign up to request clarification or add additional context in comments.

2 Comments

chars[i] = Convert.ToChar(r.Next(65, 90)); not +=
@user3248531 your welcome and please consider accepting my answer if it's solve your problem.
1

change

private static int GenerateLetters

to

private static string GenerateLetters

you are returning string but method has return parameter int. What you are returning has to be compatible with method stub.

Read more about methods in c# at MSDN

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.