Tuesday, April 5, 2011

Help me convert this line to C#

Please help me convert this line to C#.

objManagementBaseObject.SetPropertyValue("hDefKey", CType("&H" &
Hex(RegistryHive.LocalMachine), Long))

Related References in C#: System.Management

Thank you.

Additional Info:

Code was originally in VB.

From stackoverflow
  • It looks like RegistryHive.LocalMachine is just an enum value, in which case passing it through Hex() is just a waste of time:

    objManagmentBaseObject.SetPropertyValue("hDefKey", (long)RegistryHive.LocalMachine);
    
    Ishmael : Thanks. I realized it after walking away from the code that the VB line is just trying to typecast it to long.
    Joel Coehoorn : I suspect the code was originally written in VB without any casting at all, and the ugly Enum -> Hex -> String -> Object -> Long process was added because in certain cases the wrong conversion was happening. That's the more-charitable way you find this kind of code
  • objManagementBaseObject.SetPropertyValue("hDefKey", (long)RegistryHive.LocalMachine);
    

    is the C# equivalent to the VB.NET line.

    Thanks for the insight!

0 comments:

Post a Comment