Reputation: 157
When Validating a button ,I converted the String value into a float one, but when I am trying to compare the converted value with an actual float I obtain compile errors. I want to create a comparison in this if in order to not permit values under 50 to be written in the application.
private void tbBid_Validating(object sender, CancelEventArgs e)
{
var amount = 12345678.0f;
tbBid.Text = amount.ToString();
if(amount.ToString()<50)
{
e.Cancel = true;
epbid.SetError(tbBid,">50 lei");
}
}
Upvotes: 0
Views: 256
Reputation: 103
I've made a simplified version of this that may help you. I've used the code you have supplied and a version where amount was a string value first:
// current example simplified
float amount = 12345678.0f;
string text = amount.ToString();
if(amount < 50)
{
Console.WriteLine("Congratulations the first comparison worked!");
}
//if amount was a string to start with
string amountText = "12345678.0";
float amountFloat;
float.TryParse(amountText, out amountFloat);
if(amountFloat < 50)
{
Console.WriteLine("Congratulations the second comparison worked!");
}
Here's the .Net fiddle: https://dotnetfiddle.net/utyWUc
Upvotes: 1
Reputation: 1469
Try this
//Reading value from text box
var amount = tbBid.Text;
//Parsing to float
float amountFloat = float.Parse(amount);
//Comparison
if (amountFloat < 50.0f)
{
// Do your cancellation stuff
}
Upvotes: 1