开发者

accessing methods and variables in c#

Am working in console applications with visual studio in c# language.am new to this and am having silly doubts.. Am using a method with parameters as

public void display(int rank)
        {
            Console.WriteLine("rank is:" +rank);                
        }

Now I want to assign this value to a variable and have to display.i write the method as

pu开发者_StackOverflow中文版blic void get(int rank)
        {
            string a;
            a = rank;
            Console.WriteLine("rank is:" +a  );                
        }

but am getting the error while accessing this through main function creating objects.Where am going wrong?


You don't mention what error you get, but from what i can see, you're trying to assign a string to an int (a = rank). Convert it to a string before assigning:

a = rank.ToString();


You need to use ToString on rank:

rank is integer. You cannot assign int to string.

public void get(int rank)
{
    string a;
    a = rank.ToString();
    Console.WriteLine("rank is:" + a);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜