I want to Convert an int to a specific type and then return a string whose format depends on the type I converted it to.
I have a property that returns a Type object, and another that I want to return a string whose format depends on the Type.
Why doesn't the compiler like the code in HexString below?
Is there another equally brief way to do this?
public class TestClass
{
private int value;
public bool signed;
public int nBytes;
public int Value { get {return value;} set {this.value = value;}}
public Type Type {
get {
switch (this.nBytes) {
case 1:
return (this.signed ? typeof(SByte) : typeof(Byte));
case 2:
return (this.signed ? typeof(Int16) : typeof(UInt16));
default:
return null;
}
}
}
public String HexString {
get {
//?? compiler error: "no overload for method 'ToString' takes '1' arguments
return (Convert.ChangeType(this.Value, this.Type)).ToString("X" + this.nBytes);
}
}
}