CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2009
    Posts
    34

    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.

  2. #2
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    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.

  3. #3
    Join Date
    May 2009
    Posts
    34

    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.

  4. #4
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    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.

  5. #5
    Join Date
    May 2009
    Posts
    34

    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
  •  





Click Here to Expand Forum to Full Width

Featured