C#-APP:Getting variable type then use it to cast a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dantz
    New Member
    • Oct 2008
    • 71

    #1

    C#-APP:Getting variable type then use it to cast a string

    Hi, can anyone help me on this snippet.
    I want to improve the casting to be dependent on the variable it will be assigned to, is there a way? (Please see source code below)

    I tried the "typeof" but it is not the right thing to use.
    Hope anyone can help me on this.
    Thanks a lot in advance.

    Code:
    switch (nodeName.Name)
                {
                        //header elements
                    case ConstantVariables.ELEM_MSGID:
                        _messageID = Convert.ToUInt32(nodeName.InnerText);
                        break;
                    case ConstantVariables.ELEM_GPBAL:
                        GameState.PlayerState.Player.CurrentBalance = Convert.ToInt64(nodeName.InnerText);
                        break;
                    
                    default:
                        nodeName.ParentNode.RemoveChild(nodeName);
                        break; 
                }
    Last edited by dantz; Dec 3 '08, 06:49 AM. Reason: wrong code
  • PRR
    Recognized Expert Contributor
    • Dec 2007
    • 750

    #2
    Originally posted by dantz
    Hi, can anyone help me on this snippet.
    I want to improve the casting to be dependent on the variable it will be assigned to, is there a way? (Please see source code below)

    I tried the "typeof" but it is not the right thing to use.
    Hope anyone can help me on this.
    Thanks a lot in advance.

    Code:
    switch (nodeName.Name)
                {
                        //header elements
                    case ConstantVariables.ELEM_MSGID:
                        _messageID = Convert.ToUInt32(nodeName.InnerText);
                        break;
                    case ConstantVariables.ELEM_GPBAL:
                        GameState.PlayerState.Player.CurrentBalance = Convert.ToInt64(nodeName.InnerText);
                        break;
                    
                    default:
                        nodeName.ParentNode.RemoveChild(nodeName);
                        break; 
                }
    I guess you got it right now.... though look for
    InvalidCast
    Format and possibly overflow exception..

    Comment

    • dantz
      New Member
      • Oct 2008
      • 71

      #3
      reply

      thanks for the reply.
      i guess this solution is the simplest one..I better stick to it..

      Comment

      Working...