CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2007
    Posts
    69

    newbie casting question

    hi all,

    i have what will seem a very basic question to most of you... in vb.net a simple conversion of a number input into a text box is as follows...

    Code:
    Dim result as Integer
    
    result = cint(txtResult.Text)
    i tried to do the same thing in C#...

    Code:
    int result=0;
    
    result = (int)txtResult.Text;
    and it spits out the error "Cannot convert from type string to int" i understand why this occurs, i just dont know how to fix it, or how to go about casting the string result to an integer... can someone please clarfify this question for me?

    *sigh* the 'pleasures' of learning a new language :P

    cheers
    adam

  2. #2
    Join Date
    Jan 2005
    Posts
    224

    Re: newbie casting question

    result = convert.toint32(txtresult.text)

  3. #3
    Join Date
    Dec 2006
    Posts
    203

    Re: newbie casting question

    I usually use int.TryParse:
    Code:
                string s = "1234";
                int int32 = 0;
                if (!int.TryParse(s, out int32))
                {
                    // Throw MessageBox or likewise here...
                }
    Sincerely,

    Martin Svendsen

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