Click here to Skip to main content
15,908,909 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am making a small C# application that uses a trackbar to change an integer. It will eventually affect the falling rate of an object.

My question is how else can I convert and int to a string in C#? I am using the following code to do so.

C#
private void timer1_Tick(object sender, EventArgs e)
{
    int myNum;
    String myString;
    myNum = trackBar1.Value;
    myString = myNum.ToString();
    label1.Text = myString;
}


Everything is working fine when I use this method, but it there a better way to do so?

And should I create a function that does this conversion for me? If so how could I make it be able to be used for any variables, I would like to be able to do something like follows:

C#
convertIntString(int1, string1)


The first argument would be the integer that I want to pass through to convert, and the second argument would be the name of the string that I want the integer to be converted to.

Does this make since?

Thanks!
Posted
Comments
Sergey Alexandrovich Kryukov 4-Sep-13 13:18pm    
The second sample is really a bad idea. Use ToString() in all cases.
—SA
Gigabyte Giant 4-Sep-13 13:21pm    
Yes, I will, but I was just wanting to know how to create a method that I could pass values through that would then return a certain value. I figured it out.
Sergey Alexandrovich Kryukov 4-Sep-13 13:34pm    
The idea to pass a variable method won't give you anything. If this is a member name, you can do it using Reflection. Don't do it: this is a really bad idea. I just don't want to get into very complex explanations of such things as CIL and metadata, as the result would be useless anyway.
—SA
Gigabyte Giant 4-Sep-13 13:35pm    
Ok, will avoid that then. Thank you.
Sergey Alexandrovich Kryukov 4-Sep-13 13:46pm    
Okay, I added an answer with some explanation, for your understanding, please see.
Will you accept it formally (green button) as well?
—SA

Just of bit of explanation why the attempt to use of the method with the signature shown in a second code sample is not really constructive.

The "name of the string" parameter may me the name of a variable or a name of a member. They are totally different in nature, so, even if your method was possible, it would not be universal anyway. Generally, you don't have an access to variable names.

As to member name, it can be found in assembly metadata, so reflection can be used. But the name along won't be enough: it would need also the full name of the class and the reference to the instance of the class, for an instant member. And even then, reflection is too slow to do it all for such a simple purpose. Way too slow. But more important is this: this approach cannot be supportable, as such names would not be supported by a compiler. Should you misspell the name of the class and member, or change any of the names, a compiler won't be able to detect a bug.

Generally, using names of programming entities during runtime is a bad idea, in most cases. There are much better approaches, in particular, based on interfaces.

In your case, there is nothing better then System.Object.ToString for any object, reference or value. Note that you also can use format parameters for ToString methods of different types supporting formatting.

—SA
 
Share this answer
 
Comments
Gigabyte Giant 4-Sep-13 13:48pm    
Thank you for the explanation.
5 stars for you. Will accept that as a second solution.

Thank you again.
Sergey Alexandrovich Kryukov 4-Sep-13 14:57pm    
You are very welcome.
Good luck, call again.
—SA
label1.Text = trackBar1.Value.ToString();


Here is a method idea you can extrapolate from"
public static string ConvertInt(int num, string name)
{
    name = num.ToString();
    return name;
    
}
 
Share this answer
 
v3
Comments
Gigabyte Giant 4-Sep-13 12:44pm    
Thank you for the quick answer!
This did work and I cannot believe I didn't try that before. Now do you know a way that I could make a method/function that I could pass arguments through and then it would set a variable with whatever final value? Do you know what I mean?
Richard C Bishop 4-Sep-13 12:48pm    
There is not really a conversion going on in that line of code. It is just assigning the ToString value to the label. So in this case, creating a method to do this is not really necessary and is more work. However, you could create a method that does convert an int to a string and gives it the name of the string value you passed in it. I will add an example to to my solution.
Gigabyte Giant 4-Sep-13 12:53pm    
Thank you, I really appreciate the help, 5 stars for you.
Richard C Bishop 4-Sep-13 12:55pm    
You are quite welcome.
Bhushan@88 4-Sep-13 13:31pm    
hi ,
just try to use convert.ToString(obj) instead of obj.ToString();
label1.Text = convert.Tostring(trackBar1.Value);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900