I am new to programming and I have come across an issue called a "Possible mistaken empty statement" now I have done a lot of research on this topic, and the solution I was given was to remove the semi colon after the if statement, however this only produces more warnings and more errors that I currently do not know the solution to, I would very much appreciate the help, the language I am using is C#.
if (checkBox5.Checked = true) ;
Double value5 = Convert.ToDouble(checkBox4.Checked);
value5 = +1;
Double result5 = value5;
label2.Text = Convert.ToString(result5);
I guess that this is what you want:
if (checkBox5.Checked == true)
{
double value5 = Convert.ToDouble(checkBox4.Checked) + 1;
label2.Text = value5.ToString();
}
= is assigment, whereas == is checking for equality, on the other hand semicolon after if will terminate whole if statement.
Note that when you are checking for true then you can use this:
if (checkBox5.Checked) { }
You should use;
if (checkBox5.Checked == true)
not
if (checkBox5.Checked = true)
= is assignment operator, == is equality operator.
Check out;
http://msdn.microsoft.com/en-us/library/sbkb459w(v=vs.110).aspx
http://msdn.microsoft.com/en-us/library/53k8ybth(v=vs.110).aspx
And or course when you check your true value, you can use your if condition like;
if (checkBox5.Checked)
{
// Do something if checkBox5 is checked.
}
And if you use semicolon after your if condition;
if (checkBox5.Checked = true) ;
is equivalent to
if (checkBox5.Checked = true) { }
So in your case I think you shouldn't use semicolon either. Because if you use it, your code will be look like;
if (checkBox5.Checked = true)
{
}
Double value5 = Convert.ToDouble(checkBox4.Checked);
value5 = +1;
Double result5 = value5;
label2.Text = Convert.ToString(result5);
which I assume you don't want to do this. And remember, even if there is no body to execute, doesn't mean that the loop terminates.
The problem is the semi-colon at the end of your if as well as the single '=', should be:
if (checkBox5.Checked = true) ;
Should be:
if (checkBox5.Checked == true) // double '==' for equality comparison
if (checkBox5.Checked == true) ; // or you can use if (checkBox5.Checked)
// ^------------------- `=` assignment operator must be `==` equals
{ // missed
Double value5 = Convert.ToDouble(checkBox4.Checked);
value5 = +1;
// ^_______________ I assume you need `=+` addition assignment operator instead
// assigning `+1` to `value5 `
Double result5 = value5;
label2.Text = Convert.ToString(result5);
} /// missed
In addition to #des and #Soner answers, why don't you just do this :
if (checkBox5.Checked)
label2.Text = Convert.ToDouble(checkBox5.Checked).ToString();
I'm assuming you don't intend to increment value5 by 1 as it should be value5 += 1;
Related
I want that when it becomes a bool it becomes false the conditions indicate inside "if" they cancel.
I have this situation where floats remain altered by the bool even when it is false.
float x = 2;
if (Input.GetKeyDown(KeyCode.LeftShift) b = true;
if (b == true) { x = 3);
if (Input.GetKeyUp(KeyCode.LeftShift) b = false;
Isn't there a way to assert X to its original value, without adding X = 2 to b = false ..?
float x = 2f;
float xfall = 2f;
if(Input.GetKeyDown(KeyCode.LeftShift))
{
b = true;
}
else if(Input.GetKeyUp(KeyCode.LeftShift))
{
b = false;
x = xfall;
}
if(b)
{
x = 3f;
}
A lot of your code cannot be even used, next time try to make it clearer for people to read.
Another thing to note is that you do not have to if(b == true) if(b) will return the same thing because it is the same line of code.
You could use a ternary operator to simplify it somewhat if that's what you mean
x = b ? 3 : 2;
I've written an interpreter for my personal language (almost) and i've implemented cin, cout, variable declaration and others.
Now I'm stuck on implement if statement, first of all nested if statement.
I can't get the end of an if statement block when there are multiple nested if
because it stop before get the real end of the block due to end of other block
Example:
if var is 7 <- at every if my method start
if var is greater than 6
if var is less than 8
write no
else
write yes
end
else <- my method get this line but
write ok
end
else <- it should give me this line
write no
end
here is my code, i've try different one and this is the better
String[] Control = Global.Code; //here is the source code
bool isAlone = false;
int ok = 0; // variable to return (line of the else or end)
for (int i = riga + 1; i < Control.Length; i++)
{
restart:
if (Control[i].Contains("if"))
{
isAlone = true;
}
if (Control[i] == "end" && isAlone == true)
{
isAlone = false;
i += 1;
goto restart;
}
if(Control[i] == "end" || Control[i] == "else" && isAlone == false )
{
ok = i;
break;
}
}
return ok;
}
This is the code used to check whether the first number is greater than the second, but it is not working as expected. Can anyone please suggest the reason and correct me?
if (txtFirst.Text == "")
{
txtFirst.Text = "0";
if (txtSecond.Text == "")
{
txtSecond.Text = "0";
int first = Convert.ToInt32(txtFirst.Text);
int second = Convert.ToInt32(txtSecond.Text);
if (first < second)
{
txtResult.Text = "TRUE";
}
else
{
txtResult.Text = "FALSE";
}
}
}
Your scenario is working only if both the textboxes are blank(""). so it will be much better if you do like the following:
if (txtFirst.Text == "") {txtFirst.Text = "0";}
if (txtSecond.Text == ""){txtSecond.Text = "0";}
// it is good to check for null in this scenario since
// Convert.ToInt32() is not capable of handling null
int first = Convert.ToInt32(txtFirst.Text);
int second = Convert.ToInt32(txtSecond.Text);
if (first < second){txtResult.Text = "TRUE";}
else{txtResult.Text = "FALSE";}
I want to assign a data to an int volunteerEducation if I Checked radio button to save it to the database!
int volunteerEducation;
switch (volunteerEducation)
case radioButton9.Checked:
volunteerEducation= 9;
break;
case radioButton10.Checked:
volunteerEducation = 10;
break;
try this...
foreach(Control c in this.Controls)
{
if(c is RadioButton)
{
RadioButton rbtn = (RadioButton)c;
// now here you can use if else statements or Switch Statement
}
}
switch allows you to check for one of several values on a single variable. if statements allow arbitrary conditions, which can check a single variable or many. Hence, switch is unsuited for what you are trying to do.
There are a few obvious options:
Use a series of if ... else if statements
Use some fancier construct such as a lookup dictionary mapping to methods (Dictionary<RadioButton,Func<>> might be a good place to start), though if you are unfamiliar with such constructs as switch I'd strongly recommend against that (it's the old "you have to learn to walk before you start to run")
Iterate over the radio buttons and do your magic inside a loop
Such a loop would look like:
foreach (Control c in this.Controls)
{
RadioButton rbtn = c as RadioButton;
if(rbtn != null)
{
// ... your code to work with 'rbtn' goes here ...
if (rbtn.Checked)
{
// ...
}
}
}
Using as rather than is followed by an explicit cast is idiomatic, and also saves you a cast; null checking is almost certainly faster, and certainly not slower, than casting. The as operator gives you null if the given value cannot be cast to the given type. In the case where the value of the variable you are working can be changed from another thread (not likely in this case, but possible in other cases), it also saves you the headache of a nigh-impossible-to-reproduce bug when something changes the value of the variable out under your feet.
A set of if statements to do the same thing would be along the lines of
if (radioButton9.Checked)
{
// do something
}
else if (radioButton10.Checked)
{
// do something
}
Using else if mimics the behavior of a switch ... case ... break construct. If they are independent, simply omit the else. (Then, I'd suggest adding a blank line between the end of the if statement-block and the next if statement, for readability.)
I found the Answer and it's not really different from thus answers.
the Point is to declare int. e.g int volunteerEducation = 0; then you have to use if ... else if.
private void updateButton_Click(object sender, EventArgs e)
{
try
{
string volunteerID = updtIDTextBox.Text;
string volunteerName = updtNameTextBox.Text;
string volunteerZone = updtZoneTextBox.Text;
string volunteerStreet = updtStreetTextBox.Text;
int volunteerSex = updtMaleRadioButton.Checked ? 0 : 1;
DateTime volunteerBirthday = updtBirthdayDateTimePicker.Value;
if (updtHomePhoneTextBox.Text == "")
updtHomePhoneTextBox.Text = "0";
int volunteerHomePhone = int.Parse(updtHomePhoneTextBox.Text);
if (updtWorkPhoneTextBox.Text == "")
updtWorkPhoneTextBox.Text = "0";
int volunteerWorkPhone = int.Parse(updtWorkPhoneTextBox.Text);
if (updtMobile1TextBox.Text == "")
updtMobile1TextBox.Text = "0";
int volunteerMobile1 = int.Parse(updtMobile1TextBox.Text);
if (updtMobile2TextBox.Text == "")
updtMobile2TextBox.Text = "0";
int volunteerMobile2 = int.Parse(updtMobile2TextBox.Text);
string volunteerEmail = updtEmailTextBox.Text;
string volunteerJob = updtJobTextBox.Text;
string volunteerAffiliation = updtAffiliationTextBox.Text;
//The solution start from here
int volunteerEducation = 0;
if (radioButton10.Checked)
volunteerEducation = 1;
else if (radioButton11.Checked)
volunteerEducation = 2;
else if (radioButton12.Checked)
volunteerEducation = 3;
else if (radioButton14.Checked)
volunteerEducation = 5;
else if (radioButton15.Checked)
volunteerEducation = 6;
else if (radioButton16.Checked)
volunteerEducation = 7;
else if (radioButton17.Checked)
volunteerEducation = 8;
else if (radioButton18.Checked)
volunteerEducation = 9;
//end solution
string volunteerEducationPlace = addEducationPlaceTextBox.Text;
string volunteerEducationDepartmen = addEducationDepartmentTextBox.Text;
//same above
int volunteerInteresting = 0;
if (updtIntrstMdcnRadioButton.Checked)
volunteerInteresting = 1;
else if (updtIntrstSosclRadioButton.Checked)
volunteerInteresting = 2;
else if (updtIntrstLrnRadioButton.Checked)
volunteerInteresting = 3;
else if (updtIntrstTrnRadioButton.Checked)
volunteerInteresting = 4;
//end
string volunteerNotes = addNotesTextBox.Text;
int x = dataGridViewX1.SelectedRows[0].Index;
UpdateVolunteer(volunteerID, volunteerName, volunteerZone, volunteerStreet, volunteerSex, volunteerBirthday, volunteerHomePhone, volunteerWorkPhone, volunteerMobile1, volunteerMobile2, volunteerEmail, volunteerJob, volunteerAffiliation, volunteerEducation, volunteerEducationPlace, volunteerEducationDepartmen, volunteerInteresting, volunteerNotes);
showVolunteers(x);
updtIDTextBox.Text = "";
updtNameTextBox.Text = "";
updtZoneTextBox.Text = "";
updtStreetTextBox.Text = "";
updtBirthdayDateTimePicker.Value = DateTime.Now;
updtHomePhoneTextBox.Text = "";
updtWorkPhoneTextBox.Text = "";
updtMobile1TextBox.Text = "";
updtMobile2TextBox.Text = "";
updtEmailTextBox.Text = "";
updtJobTextBox.Text = "";
updtAffiliationTextBox.Text = "";
updtEducationPlaceTextBox.Text = "";
updtEducationDepartmentTextBox.Text = "";
updtNotesTextBox.Text = "";
}
catch (SqlException sql)
{
MessageBoxEx.Show(sql.Message);
}
}
Is it better to evaluate bool once but have more code like this
if (klient.Podmioty.PodmiotRodzaj == "Osoba") {
textImie.Enabled = true;
textNazwisko.Enabled = true;
textNazwa.Enabled = false;
} else {
textImie.Enabled = false;
textNazwisko.Enabled = false;
textNazwa.Enabled = true;
}
comparing to this
textImie.Enabled = klient.Podmioty.PodmiotRodzaj == "Osoba";
textNazwisko.Enabled = klient.Podmioty.PodmiotRodzaj == "Osoba";
textNazwa.Enabled = klient.Podmioty.PodmiotRodzaj != "Osoba";
It's a generic question and maybe this little example is micro optimization but I would like to know whether reusing same bool over and over is not considered bad code.
Probably useful note is that klient.Podmioty.PodmiotRodzaj is actually variable from SQL brought by Entity Framework.
Evaluate once and use the results:
var conditionValue = (klient.Podmioty.PodmiotRodzaj == "Osoba");
textImie.Enabled = conditionValue;
textNazwisko.Enabled = conditionValue;
textNazwa.Enabled = !conditionValue;
The first option, less calculation time and no replica of code...
or, option C:
bool value = klient.Podmioty.PodmiotRodzaj == "Osoba";
textImie.Enabled = value;
textNazwisko.Enabled = value;
textNazwa.Enabled = !value;
but definitely not your option B.
The first is the common answer, and the second is hard to read. However, since you're only setting bools to the same as the condition, there's a third option:
bool t = (klient.podmioty.PodmiotRodzaj == "Osaba");
TextImed.Enabled = t;
TextNazwisko.Enabled = t;
TextNazwa.Enabled = !t;
(On phone, so I prob got the variable names wrong) This is debatibly slightly less clear than the first option, but brief (and avoids code forks).