150 pts.
 C# how to: IF Statement
Hello guys, C# question here...Could I ask or is there better website? Regards

Software/Hardware used:
Visual Basic Express 2008
ASKED: December 1, 2009  11:16 PM
UPDATED: December 3, 2009  11:35 PM

Answer Wiki:
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); }
Last Wiki Answer Submitted:  December 2, 2009  12:47 am  by  mshen   27,310 pts.
All Answer Wiki Contributors:  mshen   27,310 pts.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _


 

This website will do just fine. What’s the question?

 64,520 pts.

 

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 pts.

 

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.

 63,535 pts.

 

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.

 63,535 pts.

 

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 pts.

 

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.

 63,535 pts.

 

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 pts.

 

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.

 63,535 pts.

 

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 pts.

 

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 pts.

 

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 pts.

 

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 pts.

 

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.

 63,535 pts.

 

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 pts.

 

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.

 63,535 pts.

 

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 pts.

 
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 pts.

 

What is the line where you are gettin the null reference exception ?

 63,535 pts.

 

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,310 pts.

 

The System.NullReferenceException probably means the index is outside the range of the array.

 27,310 pts.