i am trying to make a program in c#, in which the values of check boxes are retrieved from csv file.I have 4 check boxes and all of them are true or false according to the conditions in csv file. My question is i am using
if (strProg[a] == "JC")
{
chkJogging.Checked = true;
chkCycling.Checked = true;
}
else if(strProg[a] =="C")
{
chkCycling == true
}
else if(strProg[a] == "WK")
{
chkWeightLoss.Checked = true;
chkKoxing.Checked = true
}
else
{
chkBoxing.Checked = false;
chkJogging.Checked = false;
chkCycling.Checked = false;
chkWeightLoss.Checked = false
}
But for some reason the last one 'else' loop is not working. Thanks.
Don't use IF-ELSE This way.
read this : SWITCH-CASE
try this :
switch (strProg[a])
{
case "JC":
chkJogging.Checked = true;
chkCycling.Checked = true;
break;
case "C":
chkCycling.Checked = true;
break;
case "WK":
chkWeightLoss.Checked = true;
chkKoxing.Checked = true;
break;
default:
chkBoxing.Checked = false;
chkJogging.Checked = false;
chkCycling.Checked = false;
chkWeightLoss.Checked = false;
break;
}
you are missing '.Checked' here, so my guess is probably your code is breakin at this point.
else if(strProg[a] =="C")
{
chkCycling == true
}
try to change it with
else if(strProg[a] =="C")
{
chkCycling.Checked == true
}
and see if it works.
Related
In my IOS application, I need to show a drop-down menu when the menu button is pressed and hide it when the menu button is pressed again. I tried changing the hidden status to false and true as in the code below however that doesn't seem to work.
if (menuButtonActive == false)
{
menuButtonActive = true;
DropMenu.Hidden = true;
}
if (menuButtonActive == true)
{
menuButtonActive = false;
DropMenu.Hidden = false;
}
Thanks to anyone that helps!
It's just simple, try this:
In Swift:
yourView.isHidden = true //or false
In Objective-C:
yourView.hidden = YES; //or NO;
In C#:
yourView.Hidden = true; //or false;
In your case you are doing it right, but the problem is you are using only if in both cases. You have to use else if for second if condition in order to achieve the desired result.
Otherwise the second if condition will be always true and get executed you will see no effect of first if block.
For Your Case:
It should be like:
menuButtonActive = !menuButtonActive
DropMenu.Hidden = menuButtonActive
Hope this help you! :)
It must be simple
menuButtonActive = !menuButtonActive;
DropMenu.Hidden = menuButtonActive;
look at follow code , add a else
if (menuButtonActive == false)
{
menuButtonActive = true;
DropMenu.Hidden = true;
}
else if (menuButtonActive == true)
{
menuButtonActive = false;
DropMenu.Hidden = false;
}
I have a form in C# for inputting some data into a List.
The form consists of text boxes and up and down numeric boxes. Everything works fine but I want to have an error handler (try/catch) in my code so it will check if any of the text boxes are empty or the numeric boxes are left to 0, if thats the case it should pop up an error message.
I tried :
try
{
//code
}
catch (NoNullAllowedException e) //tried it without the e as well
{
//code
}
The code I was having in the brackets its the following one. Sometimes the GetItemDetails() was throwing me an error saying that not all code paths returns a value.
Any thoughts why is doing this or how can I fix it?
public iRepairable GetItemDetails()
{
Shirt shirt = null;
TShirt tshirt = null;
Trouser trouser = null;
Shoe shoe = null;
Boolean isShirt = true;
Boolean isTshirt = true;
Boolean isTrouser = true;
Boolean isShoe = true;
if (rdoShirt.Checked == true)
{
shirt = new Shirt(txtBrand.Text, Convert.ToDouble(txtPrice.Text), Convert.ToInt32(txtAmount.Text), txtCollection.Text);
isTshirt = false;
isTrouser = false;
isShoe = false;
}
else if (rdoTShirt.Checked == true)
{
tshirt = new TShirt(txtBrand.Text, Convert.ToDouble(txtPrice.Text), Convert.ToInt32(txtAmount.Text), txtCollection.Text);
isShirt = false;
isTrouser = false;
isShoe = false;
}
else if (rdoTrouser.Checked == true)
{
trouser = new Trouser(txtBrand.Text, Convert.ToDouble(txtPrice.Text), Convert.ToInt32(txtAmount.Text), txtCollection.Text);
isShirt = false;
isTshirt = false;
isShoe = false;
}
else
{
shoe = new Shoe(txtBrand.Text, Convert.ToDouble(txtPrice.Text), Convert.ToInt32(txtAmount.Text), txtCollection.Text);
isShirt = false;
isTrouser = false;
isTshirt = false;
}
if (isShirt)
{
return shirt;
}
else if (isTshirt)
{
return tshirt;
}
else if (isTrouser)
{
return trouser;
}
else //if(isShoe)
{
return shoe;
}
First of all, the NoNullAllowedException is not for list or just null values. Is the exception that throws when you want to insert null values in a column that doesn't allow them (for more info, MSDN).
For your code, place at the bottom of your code a default return value (but as far as i can see, your code shouldnt break at all)
I got a little problem. I got a if statement which says if Session isn't equal 3, then do something, and if that isn't true, then do something else. My problem is just, that it isn't working proberly.
I've already tried:
1)
if (Session["userrank"] != "3")
{
pnlAdmin.Visible = false;
}
else
{
pnlAdmin.Visible = true;
}
2)
if (Session["userrank"].ToString() != "3")
{
pnlAdmin.Visible = false;
}
else
{
pnlAdmin.Visible = true;
}
3)
if ((string)Session["userrank"] != "3")
{
pnlAdmin.Visible = false;
}
else
{
pnlAdmin.Visible = true;
}
4)
if (((string)Session["userrank"]) != "3")
{
pnlAdmin.Visible = false;
}
else
{
pnlAdmin.Visible = true;
}
but none of them seems to work. And i have already checked if there's a Session called userrank that is getting the result 3.
sorry for the "stupid" question. I'm kind of new to C# & ASP.net.
Best Regards,
Anton
Your code sets pnlAdmin.Visible = false; if whatever is in Session["userrank"] is not 3.
It sets pnlAdmin.Visible = true; if whatever is in Session["userrank"] is 3.
You said it is 3; therefore, the panel should be visible. And that seems to be what is happening.
I am pasting my code snippet below. Could some body suggest a better and effecient way of writing this. I would like minimum code to be written and avoid repetition.
private void SetControlVisibility()
{
if (DropDownList1.SelectedItem.Text.Equals("GetAssetsBasicById") || DropDownList1.SelectedItem.Text.Equals("GetAssetDetailsByIds"))
{
Label2.Text = "(Please enter asset ids for e.g. 1,2)";
chkExcludeMAPFunds.Visible = false;
chkPublishXML.Visible = true;
}
else if (DropDownList1.SelectedItem.Text.Equals("GetAssetsBasicBySedols") || DropDownList1.SelectedItem.Text.Equals("GetAssetDetailsBySedols"))
{
Label2.Text = "(Please enter sedols for e.g. B1YW440,0003496)";
chkExcludeMAPFunds.Visible = false;
chkPublishXML.Visible = true;
}
else if (DropDownList1.SelectedItem.Text.Equals("GetInvestmentReportByIds"))
{
Label2.Text = "(Please enter asset ids for e.g. 1:100)";
chkExcludeMAPFunds.Visible = true;
chkPublishXML.Visible = false;
}
else if (DropDownList1.SelectedItem.Text.Equals("GetInvestmentReportBySedol"))
{
Label2.Text = "(Please enter sedols for e.g. B1YW440:100)";
chkExcludeMAPFunds.Visible = true;
chkPublishXML.Visible = false;
}
}
You could use a dictionary to avoid both nested if's and switch/cases:
private readonly Dictionary<string, Tuple<string, bool, bool>> _dropDownMap = new Dictionary<string, Tuple<string, bool, bool>>
{
{"GetAssetsBasicById", new Tuple<string, bool, bool>("(Please enter asset ids for e.g. 1,2)", false, true) },
{"GetAssetDetailsByIds", new Tuple<string, bool, bool>("(Please enter asset ids for e.g. 1,2)", false, true) },
...
};
private void SetControlVisibility()
{
var mapping = _dropDownMap[DropDownList1.SelectedItem.Text];
if (mapping != null)
{
Label2.Text = mapping.Item1;
chkExcludeMAPFunds.Visible = mapping.Item2;
chkPublishXML.Visible = mapping.Item3;
}
}
If you favour readability over small code, then the Tuple could be replaced by an explicit VO class:
private class DropDownMappings
{
public DropDownMappings(label, excludeMAPFundsVisible, publishXMLVisible)
{
Label2Text = label;
ExcludeMAPFundsVisible = excludeMAPFundsVisible;
PublishXMLVisible = publishXMLVisible;
}
public string Label2Text { get; set; }
public bool ExcludeMAPFundsVisible { get; set; }
public bool PublishXMLVisible { get; set; }
}
private readonly Dictionary<string, DropDownMappings> _dropDownMap = new Dictionary<string, DropDownMappings>
{
{"GetAssetsBasicById", new DropDownMappings("(Please enter asset ids for e.g. 1,2)", false, true) },
{"GetAssetDetailsByIds", new DropDownMappings("(Please enter asset ids for e.g. 1,2)", false, true) },
...
};
private void SetControlVisibility()
{
var mapping = _dropDownMap[DropDownList1.SelectedItem.Text];
if (mapping != null)
{
Label2.Text = mapping.Label2Text;
chkExcludeMAPFunds.Visible = mapping.ExcludeMAPFundsVisible;
chkPublishXML.Visible = mapping.PublishXMLVisible;
}
}
Alternate code with switch:
private void SetControlVisibility()
{
if (DropDownList1.SelectedItem != null)
{
switch (DropDownList1.SelectedItem.Text)
{
case "GetAssetsBasicById":
case "GetAssetDetailsByIds":
Label2.Text = "(Please enter asset ids for e.g. 1,2)";
chkExcludeMAPFunds.Visible = false;
chkPublishXML.Visible = true;
break;
case "GetAssetsBasicBySedols":
case "GetAssetDetailsBySedols":
Label2.Text = "(Please enter sedols for e.g. B1YW440,0003496)";
chkExcludeMAPFunds.Visible = false;
chkPublishXML.Visible = true;
break;
case "GetInvestmentReportByIds":
Label2.Text = "(Please enter asset ids for e.g. 1:100)";
chkExcludeMAPFunds.Visible = true;
chkPublishXML.Visible = false;
break;
case "GetInvestmentReportBySedol":
Label2.Text = "(Please enter sedols for e.g. B1YW440:100)";
chkExcludeMAPFunds.Visible = true;
chkPublishXML.Visible = false;
break;
default:
// we do it wrong :(
throw new NotSupportedException();
}
}
}
Another solution is using Item's Tag property with predefined enum values.
My alternate code:
private void SetControlVisibility()
{
string resultText;
bool b = false;
switch (DropDownList1.SelectedItem.Text)
{
case "GetAssetsBasicById":
case "GetAssetDetailsByIds":
b = true;
resultText = "(Please enter asset ids for e.g. 1,2)";
break;
case "GetAssetsBasicBySedols":
case "GetAssetDetailsBySedols":
b = true;
resultText = "(Please enter sedols for e.g. B1YW440,0003496)";
break;
case "GetInvestmentReportByIds":
resultText = "(Please enter asset ids for e.g. 1:100)";
break;
case "GetInvestmentReportBySedol":
resultText = "(Please enter sedols for e.g. B1YW440:100)";
break;
default: return;
}
chkExcludeMAPFunds.Visible = !b;
chkPublishXML.Visible = b;
Label2.Text = resultText;
}
First, taking the chks out of the if and using the ?: operator will shorthand their notation.
Then, due to having only one statement inside each if - else if, brackets can be erased.
if (DropDownList1.SelectedItem.Text.Equals("GetAssetsBasicById") || DropDownList1.SelectedItem.Text.Equals("GetAssetDetailsByIds"))
Label2.Text = "(Please enter asset ids for e.g. 1,2)";
else if (DropDownList1.SelectedItem.Text.Equals("GetAssetsBasicBySedols") || DropDownList1.SelectedItem.Text.Equals("GetAssetDetailsBySedols"))
Label2.Text = "(Please enter sedols for e.g. B1YW440,0003496)";
else if (DropDownList1.SelectedItem.Text.Equals("GetInvestmentReportByIds"))
Label2.Text = "(Please enter asset ids for e.g. 1:100)";
else if (DropDownList1.SelectedItem.Text.Equals("GetInvestmentReportBySedol"))
Label2.Text = "(Please enter sedols for e.g. B1YW440:100)";
chkExcludeMAPFunds.Visible = (DropDownList1.SelectedItem.Text.Equals("GetInvestmentReportByIds") || DropDownList1.SelectedItem.Text.Equals("GetInvestmentReportBySedol") ? true : false;
chkPublishXML.Visible = (DropDownList1.SelectedItem.Text.Equals("GetInvestmentReportByIds") || DropDownList1.SelectedItem.Text.Equals("GetInvestmentReportBySedol") ? false : true;
This way, we have got rid of many lines.
So I have this code. What it does is to show the selected usercontrol when user tap a button. If that particular usercontrol is already visible, tapping its button will hide it.
The code is rather repetitive, any suggestion how can I make it more succinct?
private void changeControl(TextControl control)
{
switch (control)
{
case TextControl.TextBox:
if (IsRadTextBoxVisible == true)
{
IsRadTextBoxVisible = false;
}
else
{
IsRadTextBoxVisible = true;
}
IsCountriesListBoxVisible = false;
IsSliderFontSizeVisible = false;
IsSliderFontRotateVisible = false;
break;
case TextControl.Font:
if (IsCountriesListBoxVisible == true)
{
IsCountriesListBoxVisible = false;
}
else
{
IsCountriesListBoxVisible = true;
}
IsRadTextBoxVisible = false;
IsSliderFontSizeVisible = false;
IsSliderFontRotateVisible = false;
break;
case TextControl.Size:
if (IsSliderFontSizeVisible == true)
{
IsSliderFontSizeVisible = false;
}
else
{
IsSliderFontSizeVisible = true;
}
IsRadTextBoxVisible = false;
IsCountriesListBoxVisible = false;
IsSliderFontRotateVisible = false;
break;
case TextControl.Rotate:
if (IsSliderFontRotateVisible == true)
{
IsSliderFontRotateVisible = false;
}
else
{
IsSliderFontRotateVisible = true;
}
IsRadTextBoxVisible = false;
IsCountriesListBoxVisible = false;
IsSliderFontSizeVisible = false;
break;
default:
break;
}
}
var stateRad= IsRadTextBoxVisible;
var stateSlider = IsSliderFontRotateVisible;
var ........
var ........
IsCountriesListBoxVisible = false;
IsSliderFontSizeVisible = false;
IsSliderFontRotateVisible = false;
IsRadTextBoxVisible = false
switch (control)
{
case TextControl.TextBox:
IsRadTextBoxVisible = !stateRad
break;
case TextControl.Font:
IsCountriesListBoxVisible = !statexxx
break;
case TextControl.Size:
IsSliderFontSizeVisible = !statexxx
break;
case TextControl.Rotate:
IsSliderFontRotateVisible = !statexxx
break;
default:
break;
}
private void changeControl(TextControl control)
{
IsRadTextBoxVisible = control == TextControl.TextBox ? !IsRadTextBoxVisible : false;
IsCountriesListBoxVisible = control == TextControl.Font ? !IsCountriesListBoxVisible : false;
IsSliderFontSizeVisible = control == TextControl.Size ? !IsSliderFontSizeVisible : false;
IsSliderFontRotateVisible = control == TextControl.Rotate ? !IsSliderFontRotateVisible : false;
}
What it does:
control == TextControl.TextBox
returns either true or false.
Now, ternary operator ?: executes code after ? if expression before ? returned true,
or code after : if expression before ? returned false
In this case, if control matches, we're executing code after ?, which in this case toggles the property.
If we control doesn't match, we're executing code after :, which sets the property to false.
I such cases i would not use switch-case, becuase you can't make complex boolean-operations. Maybe you can make your code more efficent with some if-else statements. Switch-Cases are some times very limited and would not use them.