This question already has answers here:
How can I convert String to Int?
(31 answers)
Closed 7 years ago.
I need to display a certain number on a label when the user input is between >=0 and <= 1. User input is a string and I need the numbers between to be a decimal or double. Obviously I can't compare the two like I did because the operators can't compare a string and decimal, double, or int.
private void voltageTextBox_TextChanged(object sender, EventArgs e)
{
var RTPower = powerTextBox.Text;
powerTextBox.CharacterCasing = CharacterCasing.Upper;
if (RTPower >= 0 && <= 1)
{
displayLabel4.Text = "1";
}
}
Error: Operator '>=' cannot be applied to operands of type 'string' and 'int'
Error: Operator '<=' cannot be applied to operands of type 'string' and 'int'
How can I make that if statement work? Do I have to keep it as a string, display it in label, convert the label to an integer then re-display it? I know I can make that work, but that is far too complicated. I just need an easier way to do this.
int RTPower = Int32.Parse(powerTextBox.Text);
or for decimal values
decimal RTPower = Decimal.Parse(powerTextBox.Text);
You need to convert the value from a string to an int.
Also, I assume you are new to c# - my advice would be to avoid using var and explicitly declare your variables. It will make things clear and easier for you to learn and understand.
You can convert to int like
int x = Int32.Parse(RTPower);
then you can compare x.
If, however, you know that the user input will be between [0, 9] then you could use
if(RTPower >= "0" && <= "1")
because strings are compared lexicographically so "1" is under "9" but "10" is under "2".
The first way is much better though, because it works for all numerical user inputs
Related
This question already has answers here:
Cannot implicity convert type 'double' to 'string'
(3 answers)
Closed 4 years ago.
I am trying to make a sum in c# but i keep getting these errors.
by pressing the + button i pretty much want the numbers to be summed up in a label.
Thanks in advance.
here's my code.
private void buttonPlus_Click(object sender, EventArgs e)
{
Int32 a = 10;
double b = 20.50;
double c = 50.10;
Int32 d = 20;
labelSum.Text = (a+b+c+d);
}
Not sure why you're using Int32 and not just int.
Also, try converting the sum to a string:
labelSum.Text = (a+b+c+d).ToString();
Text property is of type String while your expression will return a numeric data type not String so, the right hand side should be returning the same type what the left hand of expression is having when assigning.
Just convert the result of sum to string by calling ToString() :
labelSum.Text = (a+b+c+d).ToString();
or like:
labelSum.Text = Convert.ToString(a+b+c+d);
For example, I have declared the following enum in C#.
C# code:
public enum LobMaster :int
{
RetailBroking = 1,
HNI = 2,
TechnologyInfrastructure = 3,
}
cshtml code:
#int code = 2
#if (LobMaster.RetailBroking == code)
{
}
If I try to compare int enum with integer value it gives me following error message.
operator '==' cannot be applied to types 'LobMaster' and 'int'
While my enum
LobMaster
is already an int type.
But when I cast the enum to int and then compare with int value it works fine.
So my question is why I need to cast enum to int as enum is already an int.
The main purpose of Enums is to remove "magic numbers", so you should ask yourself - why do you want to use those magic numbers?
Much better comparison would be:
Session["Lob_code"] is LobMaster && LobMaster.MassAffluent == (LobMaster)Session["Lob_code"]
This way you are avoiding those magic numbers and your code just provides all the context.
#if ((int) LobMaster.MassAffluent == Convert.ToInt32(Session["Lob_code"])) { }
you should be able to cast the left hand value and then do the conoarison
This question already has answers here:
Converting between SQL char and C#
(3 answers)
Closed 5 years ago.
When i am trying to get datarow with the below code, got an error:
if (row["FREQ"] != DBNull.Value)
Char Freq = (Char)row["FREQ"];
Specified cast is not valid.
In Table, the column FREQ is declared as below:
[FREQ] [char](1) NULL
My requirement is to declare it as FREQ CHAR(1) as this field contains atleast one characher value. So i cant use string casting as mentioned in the given url.
Try generic extension method DataRow.Field<T>
var value = row.Field<string>("FREQ");
//If you sure that value is only one character long
var character = value != null ? value[0] : '';
You shouldn't cast like that, when it comes to data retrieved from database. You should use Convert static class. In your case:
string Freq = Convert.ToString(row["FREQ"]);
And then you could cast it to char.
This question already has answers here:
Cannot implicitly convert type 'string' to 'double' Issue
(5 answers)
Closed 8 years ago.
Hi guys I am trying to use set a text box to only accept double data types as the type of input to be accepted for product price but im getting the following error: cannot implicitly convert type 'string' to 'double'. Can anyone point in me in the right direction please?
((Employee)o).ProdPrice = this.textProdName.Text;
I am getting the eror under this.textProdName.Text.
I prefer TryParse, because if the string can't be parsed, I can decide on what value to return, in the sample below I used Doube.NaN.
Double val;
((Employee)o).ProdPrice = Double.TryParse(this.textProdName.Text, out val)?val:Double.NaN;
Try Convert.toDouble or Double.Parse
so like this: ((Employee)o).ProdPrice = Convert.ToDouble(this.textProdName.Text); or ((Employee)o).ProdPrice = Double.Parse(this.textProdName.Text);
I am trying to use set a text box to only accept double data types as the type of input to be accepted for product price
Assuming that you already validate the textbox to only accept double values, you need to convert this.textProdName.Text to double
((Employee)o).ProdPrice = Convert.ToDouble(this.textProdName.Text);
However, in case the textbox can't be converted to double, use Double.TryParse as below
Double prodPrice = 0;
if (Double.TryParse(this.textProdName.Text, out prodPrice))
{
((Employee)o).ProdPrice = prodPrice;
}
else
{
// do something when textProdName.Text can't be converted to double
}
private void buttonConvert_Click(object sender, EventArgs e)
{
//Convert number from C to F
double convertDecimal;
convertDecimal = 1.8;
textBoxF = double.Parse(textBoxC.Text) * double(convertDecimal) + 32;
^here is where I get the error
Error 1 Invalid expression term 'double'
I am pretty new to programming and but I just can't wrap my mind around trying to add, subtract, dividing, or multiplying numbers. I am trying to do a simple a simple conversion. Take the number from the Celsius textbox and convert it to Fahrenheit. I just don't understand the number part???
Thanks for your help!
double(convertDecimal) should be (double)convertDecimal
That looks like a C++ type-casting expression, which doesn't work in C#. And as convertDecimal already is of type double there's no need to cast it. Just use it directly:
textBoxF = double.Parse(textBoxC.Text) * convertDecimal + 32;
You only need to change the type of a variable (i.e. type-cast) when the variable is of a type not expected. Adding two double values is okay. Even adding a double and an int is okay because the integer is implicitly converted to a double.
Edit: You try to assign the result of the expression to a control, which will not work. You should convert the result to a string (e.g. with double.ToString), and then assign to the controls text field:
double farenheit = double.Parse(textBoxC.Text) * convertDecimal + 32;
textBoxF.Text = farenheit.ToString();