none
How do I solve Cannot implicitly convert type 'uint' to 'int'. An implicit conversion exists(are you missing a cast?) RRS feed

  • Question

  •  I need help solve Cannot implicitly convert type 'uint' to 'int'. An implicit conversion exists(are you missing a cast?)

    The following line of code is working fine, however a during code review, I was asked to replace this line of code with a different one. The new line suggested is not working because I am getting the above error. 

    How to I make this new line of code work?

    Here is the line that is working

    objPrepHearDoc.Hearing.HearingKey = Convert.ToInt32(aobjXmlInputDoc.DocumentElement.SelectSingleNode("Case/Hearing[@ID=" + strEventId + "]/@ID").InnerText);

    Here is the suggested code change that is not working and generating cannot implicitly convert type 'uint' to 'int'. An implicit conversion exists(are you missing a cast?) 

    objPrepHearDoc.Hearing.HearingKey = Convert.ToUInt32(strEventId); 

    Wednesday, February 27, 2019 3:12 PM

All replies

  • Hi winkimjr2,

    It seems that the objPrepHearDoc.Hearing.HearingKey has a int data type, that why you are getting an error.

    IMHO, the code review suggestion is incorrect. Please see below.

    Working case:

    Not working case, the same error like you have because intHearingKey has int data type:

    Additionally, you are using old .NET Framework API dealing with XML.

    It is better to start using LINQ to XML.


    Wednesday, February 27, 2019 3:34 PM
  • Maybe ask the reviewer if you can also change the type of HearingKey to uint and make all of consequent adjustments.

    Wednesday, February 27, 2019 3:47 PM
  • Thanks Yitzhak. I did solve this by just using Convert.ToInt32. Also at my work, they do not use LINQ so I am not sure I will be allowed to use it. A few people here have suggested it also. 

    And can you tell me the advantage of using LINQ over just XML?

    Wednesday, February 27, 2019 4:53 PM
  • The code reviewer did accept what I did.
    Wednesday, February 27, 2019 4:54 PM
  • Hi winkimjr2,

    • I am glad that the code reviewer accepted the solution.
    • Microsoft introduced LINQ to XML more than a decade ago. It is a built-in part of the .NET Framework. No need to install anything. It is much easier to use and has much more powerful functionality than the older XML APIs of the .NET Framework.

    Please don't forget to 'Mark as Answer'.

    Wednesday, February 27, 2019 5:02 PM