Please see the code below:
public bool IsValidateInputSuceeded(string EnteredCost, string EnteredPaymentReceived, out decimal Cost, out decimal PaymentReceived)
{
bool ValidateInputSuceeded = true;
EnteredCost= EnteredCost.Substring(EnteredCost.IndexOfAny("0123456789".ToCharArray()));
EnteredPaymentReceived = EnteredPaymentReceived.Substring(EnteredPaymentReceived.IndexOfAny("0123456789".ToCharArray()));
if ((Decimal.TryParse(EnteredCost.Replace(" ", "").Replace(",", ""), out Cost)) == false)
{
ValidateInputSuceeded = false;
}
if ((Decimal.TryParse(EnteredPaymentReceived.Replace(" ", "").Replace(",", ""), out PaymentReceived)) == false)
{
ValidateInputSuceeded = false;
}
if (ValidateInputSuceeded == false)
{
return false;
}
if (EnteredCost.IndexOf(" ") > 0|| EnteredPaymentReceived.IndexOf(" ") > 0)
{
return false;
}
if (EnteredCost[EnteredCost.Length-1] =='.' || EnteredCost[EnteredCost.Length - 2] == '.' || EnteredPaymentReceived[EnteredPaymentReceived.Length - 1]=='.' || EnteredPaymentReceived[EnteredPaymentReceived.Length - 2] == '.')
{
return false;
}
if (EnteredCost.IndexOf(".") == -1 || EnteredPaymentReceived.IndexOf(".")==-1)
{
return false;
}
return true;
}
EnteredCost and EnteredPaymentReceived are input parameters from MaskedTextBoxs'(https://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox(v=vs.110).aspx) both with an input mask of: £999,999.00. For example, a value of one of them could be: "£ , 3.22" (without quotes) as an input parameter into the function.
There are two string variables: EnteredCost and PaymentReceived. Should these variables be StringBuilders instead of Strings? The reason I ask is because there are a log of string manipulations going on e.g. using the REPLACE function and the indexOfAny function, which returns more strings.