C# how to: IF Statement

150 pts.
Tags:
C#
IF
IF statement
loop
Hello guys, C# question here...Could I ask or is there better website? Regards

Software/Hardware used:
Visual Basic Express 2008

Answer Wiki

Thanks. We'll let you know when a new response is added.

Try this:

if (ComboBox1.SelectedIndex == 2 && Items[index].Price > <b>Convert.ToDouble(50.00)</b> )
{
txtManu.Text = Items[index].Manu;
txtPrice.Text = Convert.ToString(Items[index].Price);
}

Discuss This Question: 20  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Denny Cherry
    This website will do just fine. What's the question?
    66,140 pointsBadges:
    report
  • Ak1bo78
    I have a Windows Form with a combobox(inside combobox three items) and textboxes and forward and back button and also a class('Items' which holds items eg string,int and double variables) in the project. When I click the combobox in the form arrays from the form class are displayed in different texboxes depending on their type and the price will be displayed only if price of item is over 50.00 e.g. in code should be if (ComboBox1.SelectedIndex == 2 && Items[index].Price > 50.00 ) { txtManu.Text = Items[index].Manu; txtPrice.Text = Convert.ToString(Items[index].Price); } All with me? Great.So far so good. Now when I click the forward button on the form I expect all prices over 50.00 to be displayed in the txtPrice.Text textbox but its displaying ALL prices! The forward button is coded as below: else if (comboBox1.SelectedIndex == 2 && Items[index].Price > 50.00) { index += 1; if (index == Items.Length) index = 0; txtManu.Text = Items[index].Manu; } Please note: The combobox has index 0 and index 1 items too.I just didn't include to make it brief.E.g ComboBox1.SelectedIndex == 0 and ComboBox1.SelectedIndex == 1 And: The forward button has index 0 and index 1 items too.I just didn't include to make it brief.E.g. if (comboBox1.SelectedIndex == 0).... and if (comboBox1.SelectedIndex == 1)... Any suggestions? Thanks in advanced guys! And all help much appreciated. Yes kinda noob. :)
    150 pointsBadges:
    report
  • carlosdl
    Sure. You can ask. Some tips: Search for answers before you post. Search ITKnowledgeExchange.com to find answers to your question, because with over 10,000 answers, it may already exist. Choose your title wisely. Your title should describe the issue you are having and contain details of what software/hardware you are using. Provide as much information as possible. The more information you provide, e.g. what version of software/hardware you are using, specific error messages, etc., will help get your specific question answered appropriately. Job postings, homework assignments and Advertising-based messages will be deleted. --------------- And, if the question is about C#, I would not recommend to use "Visual Basic" as a tag.
    69,835 pointsBadges:
    report
  • carlosdl
    I don't see where you assign the price to the txtPrice item in the forward button code. I think you will need to post more code.
    69,835 pointsBadges:
    report
  • Ak1bo78
    Oh sorry the forward button is coded as below: else if (comboBox1.SelectedIndex == 2 && Items[index].Price > 50.00) { index += 1; if (index == Items.Length) index = 0; txtManu.Text = Items[index].Manu; txtPrice.Text = Convert.ToString(Items[index].Price); } Do you need to see any more code?
    150 pointsBadges:
    report
  • carlosdl
    You are checking the item price before incrementing the index, so you check if the price of one item is over 50, but you actually display the price of the next item, not the price for the one you checked.
    69,835 pointsBadges:
    report
  • Ak1bo78
    Does my code now check the value of the item,display it and then move on? :) else if (comboBox1.SelectedIndex == 2 && Items[index].Price > 50.00) { txtManu.Text = Items[index].Manu; txtPrice.Text = Convert.ToString(Items[index].Price); index += 1; if (index == Items.Length) index = 0; }
    150 pointsBadges:
    report
  • carlosdl
    I guess so, but the question is: are you still getting wrong results ? I suppose the answer will be yes, but I think the results are different now. With that code, when the current item price is not over 50, you will not nove to the next item.
    69,835 pointsBadges:
    report
  • Ak1bo78
    Thanks Mshen but its just stops on second item and there is two items with price > 50.00. Let me show you sample data guys: Test items: Items[0] = new items("Car", 30.00); Items[1] = new items("Cat", 55.00); Items[2] = new items("Cookie", 59.00); The combobox: if (ComboBox1.SelectedIndex == 0 && Items[index].Price > 50.00 ) { txtPrice.Text = Convert.ToString(Items[index].Price); } The forward button: if (comboBox1.SelectedIndex == 2 && Items[index].Price > 50.00) { index += 1; if (index == Items.Length) index = 0; txtPrice.Text = Convert.ToString(Items[index].Price); } txtPrice { //'single textbox' }
    150 pointsBadges:
    report
  • Ak1bo78
    Let me show you sample data guys: Test items: Items[0] = new items(”Car”, 30.00); Items[1] = new items(”Cat”, 55.00); Items[2] = new items(”Cookie”, 59.00); The combobox: if (ComboBox1.SelectedIndex == 0 && Items[index].Price > 50.00 ) { txtPrice.Text = Convert.ToString(Items[index].Price); } The forward button: if (comboBox1.SelectedIndex == 0 && Items[index].Price > 50.00) { index += 1; if (index == Items.Length) index = 0; txtPrice.Text = Convert.ToString(Items[index].Price); } txtPrice { //’single textbox’ }
    150 pointsBadges:
    report
  • Ak1bo78
    Answer is yes. Right results because first item shown is > 50.00 but there is more items and they are not showing.Any suggestions?
    150 pointsBadges:
    report
  • Ak1bo78
    Hey it worked but took me 5 mouse clicks to get to next item > 50.00!?? is it because there is 5 items? And got this error: http://msdn.microsoft.com/en-us/library/3zwz4dx3.aspx Help thanks if you know what it is. I gave you the address becauase if i tell you the error you give me the address :). Could it be that it doesnt like searching through 5 items when only 2 are > 50.00? Hmmm.
    150 pointsBadges:
    report
  • carlosdl
    Ak1bo78, could you please let us know what is the code you are really using for the forward button ? You posted some code yesterday, you changed it later to display the price and then move to the next item, but the last code you posted looks similar to your first version, and it is checking for the price of one item, but displaying the price of the next item. Also, it could be better if you post the complete code of your forward button.
    69,835 pointsBadges:
    report
  • Ak1bo78
    Hi guys improved code: The forward button: if (comboBox1.SelectedIndex == 0 && Items[index].Price > 50.00) { if (index == Items.Length) index = 0; txtPrice { //’single textbox’ } index += 1; But it goes through all the 3 items and you have to keep hittin the next button a few times and crashes....how would I add exception to this? Do I need it?
    150 pointsBadges:
    report
  • carlosdl
    Not knowing what you are using the combo box for, and assuming that what you want is to display the next item whose price is over 50, I would try something similar to this:
    if (comboBox1.SelectedIndex == 0)
    {
    	initialIndex = index
    	index += 1;
    	if (index == Items.Length) 
    		index = 0;
    	while (Items[index].Price <= 50.00 && index != initialIndex)
    	{
    		index += 1;
    		if (index == Items.Length) 
    			index = 0;
    	}
    	//display the value
    }
    If it "crashes", let us know how it crashes (i.e. what error message you are getting) On the other hand, I don't think that is the code you are using, because it doesn't even display anything. Please, help us help you.
    69,835 pointsBadges:
    report
  • Ak1bo78
    Ok its showing the items with price > 50.00 but I get the following error after forward button is pressed: Error: System.NullReferenceException was unhandled: Object reference not set to an instance of an object. Could it be there's no more items and C# doesn't like it?
    150 pointsBadges:
    report
  • Ak1bo78
    System.NullReferenceException was unhandled:
    Object reference not set to an instance of an object.
    
    Just trying ITKE's features :). 'I need to help people on here hopefully I can answer questions too very soon...YEY
    150 pointsBadges:
    report
  • carlosdl
    What is the line where you are gettin the null reference exception ?
    69,835 pointsBadges:
    report
  • mshen
    The forward button is not going through all of the items because you are not using a loop. Follow Carlosdl's example, but don't just copy it; apply the concepts to your program.
    27,385 pointsBadges:
    report
  • mshen
    The System.NullReferenceException probably means the index is outside the range of the array.
    27,385 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following