-3

Possible Duplicate:
How can I convert String to Int?

public List<int> GetListIntKey(int keys)
{
        int j;
        List<int> t;
        t = new List<int>();
        int i;
        for (i = 0; ; i++)
        {
            j = GetKey((keys + i).ToString());
            if (j == null)
            {
                break;
            }
            else
            {
                t.Add(j);
            }
        }
        if (t.Count == 0)
            return null;
        else
            return t;
}

The problem is on the line:

j = GetKey((keys + i).ToString());

I'm getting error say:

Cannot implicitly convert type 'string' to 'int'

Now the GetKey function is type of string:

public string GetKey(string key)
{
}

What should I do ?

Community
  • 1
  • 1
user1477444
  • 107
  • 1
  • 2
  • 12
  • This question asks about converting an int to a string, but you already figured that out, and the error message is about converting a string to an int. –  Jul 03 '12 at 19:29
  • 3
    Please edit your question and correct its title. The problem is about string to int conversion, not int to string. – daniloquio Jul 03 '12 at 19:44
  • 1
    I like how most of the correct answers are downvoted. – jrummell Jul 03 '12 at 19:45
  • 3
    This is an almost duplicate question with lots of correct answers (and lots of down votes followed by counter strike up votes). This question definitely fail on adding value to SO and should be closed as duplicate as @hvd suggested. – daniloquio Jul 03 '12 at 19:52

7 Answers7

5

The Problem is that "j" is an int, and you are assigning it to the return of GetKey. Either make "j" a string, or change the return type of GetKey to int.

Jaime Torres
  • 10,365
  • 1
  • 48
  • 56
3

Try this:

j = Int32.Parse(GetKey((keys + i).ToString()));

It will throw an exception if the value is not a valid integer.

An alternative is the TryParse, which returns a boolean if the conversion was not successful:

j = 0;

Int32.TryParse(GetKey((keys + i).ToString()), out j);
// this returns false when the value is not a valid integer.
Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
alf
  • 18,372
  • 10
  • 61
  • 92
2

the result type of your getkey is string en the j variable is declared int

the solution is:

j = Convert.ToInt32(GetKey((keys + i).ToString()));

i hope this is solution to your problem.

Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
1

You are getting the error because GetKey returns a string and you are trying to assign the return object to j which is declared as an int. You need to do as alfonso suggested and convert the return value to an int. You can also use:

j = Convert.ToInt32(GetKey((keys+i).ToString()));
Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
DarLom
  • 1,100
  • 2
  • 12
  • 30
1

try to improve your code, and look this:

public List<int> GetListIntKey(int keys)
{
    var t = new List<int>();

    for (int i = 0; ; i++)
    {
        var j = GetKey((keys + i).ToString());
        int n;
        // check if it's possible to convert a number, because j is a string.
        if (int.TryParse(j, out n))
            // if it works, add on the list
            t.Add(n);
        else //otherwise it is not a number, null, empty, etc...
            break;
    }
    return t.Count == 0 ? null : t;
}

I hope it help you! :)

Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194
0
What should i do ?

You have it all wrong. Read about value types and reference types.

Errors:

  1. Error is Cannot implicitly convert type 'string' to 'int'. Meaning implicitly it is getting a string which it cannot convert to int. GetKeys is returning string which you are trying to assign to integer j.

  2. Your j is integer. How can it be checked with null. When can a value type be null?

Use this

public List<int> GetListIntKey(int keys)
{
    int j = 0;
    List<int> t = new List<int>();
    for (int i = 0; ; i++)
    {
        string s = GetKey((keys + i).ToString());

        if (Int.TryParse(s, out j))
            break;
        else
            t.Add(j);
    }

    if (t.Count == 0)
        return null;
    else
        return t;
}
Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
-1

You have to use an exppicit type cast.

Use

int i = Convert.ToInt32(aString);

to convert.

marc wellman
  • 5,808
  • 5
  • 32
  • 59