I am trying to convert a string I made in a class into a double so I can use it in arithmetic however I am getting errors, I have tried the following:
/* Calling class to use number for conversion
double LocalCentToInch = Convert.ToDouble(CentI);
double YourAnswer = MyConvert * LocalCentToInch;
*/
//Ignore these lines just testing type converison and failing
//Console.WriteLine(CentI);
//Console.WriteLine(CentI.ToDouble());
double LocalCentToInch;
LocalCentToInch = Convert.ToInt32(CentI);
Console.WriteLine(LocalCentToInch);
I am calling from a class named convert one and it looks like this:
/////////////////////centimetres to inches///////////////////////////////
public class ConvertOne
{
public ConvertOne()
{
Centtoinch = "0.3937008";
}
public ConvertOne(string centtoinch)
{ //make new string with var data
Centtoinch = centtoinch;
}
public string Centtoinch { get; }
public override string ToString()
{
return Centtoinch;
}
}
///////////////////////end of centimetres to inches////////////////////////////
The main method is where the math is going down and it looks like this currently:
double YourAnswer = MyConvert * 0.3937008;
Console.WriteLine("\nYour answer is:");
Console.WriteLine(YourAnswer);
I do not want to use 0.39377008 though, I want to call this string from it's class and convert it into a double so that it can be used in the "YourAnswer" part.
As you can see I have tried different approaches although I'm not sure what I should try next.
The error I'm getting is this:
Unhandled Exception:
System.InvalidCastException: Specified cast is not valid.
at System.Convert.ToInt32 (System.Object value) [0x00003] in <71d8ad678db34313b7f718a414dfcb25>:0
at testConverter.Main () [0x000dc] in <93d359b0b3204d6482ac66dd3e4b0858>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.InvalidCastException: Specified cast is not valid.
at System.Convert.ToInt32 (System.Object value) [0x00003] in <71d8ad678db34313b7f718a414dfcb25>:0
at testConverter.Main () [0x000dc] in <93d359b0b3204d6482ac66dd3e4b0858>:0
exit status 1