Click here to Skip to main content
15,878,748 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!

I am making a small program kinda like "Who Wants To Be A Millionaire". There is an option when the player press the 50:50 image, two of the random label will be hidden and only one correct answer and wrong answer will be left. I have started programming the logic for it. But it does no seem to be working.

C#
var label = new List<string> { "label1", "label2", "label3" };

int index1 = Random.Next(label.Count)
int index2 = Random.Next(label.Count)

//Make selected index of label be visible = false
index1.Visible = false;
index2.visible = false;


Thank you for the help.

FYr here is the logic:
1) put labels in an array.
2) random select 2 index form array. So that two wrong answers will be selected.
3) when selected, label will become invisible. Then only one wrong answer will be left.
Posted
Updated 12-Jul-14 5:45am
v2

C#
index1.Visible = false;

Given that you have declared index1 as an int what do you expect this line of code to do?

You should be using the randomiser to generate values that you can then use to access the controls on your form, which you can show or hide as required.
 
Share this answer
 
Comments
mitchiee1226 12-Jul-14 11:42am    
Index1 is the code which i will use to get the random number. Then use that random number to get the label to be disabled. Sorry the codes are a little messy. Jsut thought of the logic just just but do not know how to execute it.
That's probably because you haven't thought about what you are trying to do, but have just guessed and hoped it would work: that isn't a successful strategy in computer (or any other field I know of, either)

Let's loop at the most obvious problems - the ones that cause compilation errors:
1) There is no static method Random.Next - you need an instance of the Random class to use the Next method. As a result you get the error:
"An object reference is required for the non-static field, method, or property ..."
Fix it by creating a class level instance:
C#
private Random rand = new Random();
and using that instead.

2) Statements in c# are all terminated by a semicolon: that means you get a
"; expected"

Fix it by adding a semicolon to the end of each instruction.

3) C# is case sensitive, so "Visible" and "visible" are different - you need to use exactly the right name, which is "Visible"
Fix it by changing "visible" to "Visible"

4) Integers do not have a Visible (or visible) property, because they are not displayable user elements (or "controls" as they are known).
Fix: complicated...

The big problem is that a number is not a label, even if it is in some way related to the name of the label - and while there are ways to get the actual label of that name via reflection, that's advanced stuff and far, far beyond your skill level.
Instead, try using a simple if statement:
C#
int hideMe = rand.Next(numberOfLabelsLeft);
if (hideMe == 0) label1.Visible = false;
else if (hideMe == 1) label2.Visible = false;
else if (...
You get the idea...

You can do it using an array or a list, but the collection would have to contain the actual objects (Label class instances) rather than strings.
 
Share this answer
 
Comments
mitchiee1226 12-Jul-14 11:42am    
Index1 is the code which i will use to get the random number. Then use that random number to get the label to be disabled. Sorry the codes are a little messy. Jsut thought of the logic just just but do not know how to execute it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900