How convert Method to Class in C#? Application will be better organised if same type of work is grouped. stringToDouble method is in Form, but it is needed to be used by public classes in Globals.cs. It could be moved, only, if it is converted to Class. After conversion to Class, stringToDouble is accessible from whole application.
As Method:
private double stringToDouble(string valuE)
{
double mla;
mla = Convert.ToDouble(valuE);
if (valuE.Contains("."))
{
if (Globals.commaCountry == Globals.yeS)
{ mla = Convert.ToDouble(valuE.Replace(".", ",")); } // but if you use decimal comma we need fix
}
else
{ mla = Convert.ToDouble(valuE); } // convert string to double
return mla;
}
As Class:
class stringToDouble
{
public stringToDouble(string valuE) // coverts string to double having in mind decimal sign
{
double mla; // init variable
mla = Convert.ToDouble(valuE); // convert with default function
if (valuE.Contains(".")) // check for decimal point
{
if (Globals.commaCountry == Globals.yeS) // check if we use decimal comma
{ mla = Convert.ToDouble(valuE.Replace(".", ",")); } // but if you use decimal comma we need fix
}
else
{ mla = Convert.ToDouble(valuE); } // convert string to double
this.doublE = mla; // return values
}
public double doublE { get; private set; }
}
How to use as Class:
stringToDouble ca = new stringToDouble(f);
double a = ca.doublE;
No comments:
Post a Comment