-
October 20th, 2011, 12:17 AM
#1
Assign numeric values to Textboxes
Beginner question:
I want to do some math during runtime and assign the result to a textbox.
I'm having TYPE problems.
For example:
double dValue1;
double dValue2;
txtMyTexBox.Text = dValue1 * dValue2 / 4.67;
Now I know you can't do this because the textbox is expecting a String. I'm just not sure how to do this correctly.
This doesn't work:
txtMyTexBox.Text = (string)(dValue1 * dValue2 / 4.67);
I'm still at the "Hello World" stage in C sharp coming from a VB6 background.
Help?
thx.
-
October 20th, 2011, 12:48 AM
#2
Re: Assign numeric values to Textboxes
Hello! All objects expose the ToString() method (and most output something useful when it is called) which returns a string representation or description of the object. In your case, use:
Code:
double dValue1;
double dValue2;
double result = dValue1 * dValue2 / 4.67;
txtMyTexBox.Text = result.ToString();
The more concise version works too:
Code:
double dValue1;
double dValue2;
txtMyTexBox.Text = (dValue1 * dValue2 / 4.67).ToString();
Make sense?
Best Regards,
BioPhysEngr
http://blog.biophysengr.net
--
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
-
October 20th, 2011, 01:10 AM
#3
Re: Assign numeric values to Textboxes
Yes.
The first example I knew. It is that having to create an extra variable to hold the value first just seemed like a waste as I have been used to doing without in VB.
The second example, however, I didn't know you could do.
Interesting.
Thank you.
-
October 20th, 2011, 01:45 AM
#4
Re: Assign numeric values to Textboxes
having to create an extra variable to hold the value first just seemed like a waste
I always recommend writing for maximum clarity. Defining an extra variable or not is not likely to impact performance. The compiler will automatically optimize code, so defining the variable or not is unlikely to impact performance in any way.
Of course, in this case, both are about equally informative.
Best Regards,
BioPhysEngr
http://blog.biophysengr.net
--
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
-
October 20th, 2011, 03:11 AM
#5
Re: Assign numeric values to Textboxes
True.
I'm all for clarity.
If you can achieve that in less lines though, that's preferable.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|