|
-
September 5th, 2010, 04:02 PM
#1
I need help with returning my list in one method to my labels in another method.
Hi,
I'm confused on how to return more then one item to my list to more then one label at the same time. Meaning i know how to return on 1 item from the list to 1 label but how do i return more then one item from the list to more then one label. for example on my code i for one item left on my list i use this code:
case 1:
firstlist[0] = firstlist[0] + 1;
return firstlist[0] = Int32.Parse(label1.Text);
but how would i use it for more then one item on list to equal to more then one label. exp.
list item index 0 = label1
list item index 1 = label2
list item index 2 = label3
I was thinking of using a while statement but how would i go and pick off each item of the list to send to the labels. Any help given would be great. thanks..
Here is my current Code: Its not complete but it includes both of the section that i'm working on..
private void button1_Click(object sender, EventArgs e)
{
Random Myrandom = new Random();
List<int> myInts = new List<int>();
label1.Text = RandomNumber(1, 53, Myrandom, myInts).ToString();
label2.Text = RandomNumber(1, 53, Myrandom, myInts).ToString();
label3.Text = RandomNumber(1, 53, Myrandom, myInts).ToString();
label4.Text = RandomNumber(1, 53, Myrandom, myInts).ToString();
label5.Text = RandomNumber(1, 53, Myrandom, myInts).ToString();
label6.Text = RandomNumber(1, 53, Myrandom, myInts).ToString();
}
private int RandomNumber(int min, int max, Random mynumb, List<int> LottNum)
{
int nRN = 0;
// Start the for loop
for (int i = 1; i > 0; i++)
{
string numTxt01,
numTxt02,
numTxt03,
numTxt04,
numTxt05,
numTxt06;
int numTxt1,
numTxt2,
numTxt3,
numTxt4,
numTxt5,
numTxt6,
total;
// Convert the first number to numeric
numTxt01 = textBox1.Text;
numTxt1 = Int32.Parse(numTxt01);
// Convert the second number to numeric
numTxt02 = textBox2.Text;
numTxt2 = Int32.Parse(numTxt02);
// Convert the third number to numeric
numTxt03 = textBox3.Text;
numTxt3 = Int32.Parse(numTxt03);
//Convert the fourth number to numeric
numTxt04 = textBox4.Text;
numTxt4 = Int32.Parse(numTxt04);
// Convert the fifth number to numeric
numTxt05 = textBox5.Text;
numTxt5 = Int32.Parse(numTxt05);
//Convert the sixth number to numeric
numTxt06 = textBox6.Text;
numTxt6 = Int32.Parse(numTxt06);
List<int> firstlist = new List<int>();
Random listRand = new Random();
//We add these number from the textbox to a list
firstlist.Add(numTxt1);
firstlist.Add(numTxt2);
firstlist.Add(numTxt3);
firstlist.Add(numTxt4);
firstlist.Add(numTxt5);
firstlist.Add(numTxt6);
int numberOfElementsToRemove = listRand.Next(3, 5);
while (numberOfElementsToRemove > 0)
{
firstlist.RemoveAt(listRand.Next(firstlist.Count() - 1));
numberOfElementsToRemove--;
}
int numberpicked = 0;
if (firstlist.Count == 1)
numberpicked = listRand.Next(1, 3);
else if
(firstlist.Count == 2)
numberpicked = listRand.Next(4, 7);
else if
(firstlist.Count == 3)
numberpicked = listRand.Next(8, 11);
switch (numberpicked)
{
case 1:
firstlist[0] = firstlist[0] + 1;
return firstlist[0] = Int32.Parse(label1.Text);
case 2:
firstlist[0] = firstlist[0] - 1;
return firstlist[0] = Int32.Parse(label1.Text);
case 3:
firstlist[0] = firstlist[0];
return firstlist[0] = Int32.Parse(label1.Text);
case 4:
// Add one
firstlist[0] = firstlist[0] + 1;
firstlist[1] = firstlist[1] - 1;
return firstlist[0] = Int32.Parse(label1.Text);
case 5:
// Add one
firstlist[0] = firstlist[0] - 1;
firstlist[1] = firstlist[1] + 1;
return firstlist[0] = Int32.Parse(label1.Text)
case 6:
// Add one
firstlist[0] = firstlist[0] - 1;
firstlist[1] = firstlist[1];
break;
case 7:
// Add one
firstlist[0] = firstlist[0] + 1;
firstlist[1] = firstlist[1];
break;
case 8:
// Add one
firstlist[0] = firstlist[0] + 1;
firstlist[1] = firstlist[1] - 1;
firstlist[2] = firstlist[2];
break;
case 9:
// Subtract one
firstlist[0] = firstlist[0] - 1;
firstlist[1] = firstlist[1] + 1;
firstlist[2] = firstlist[2];
break;
case 10:
// Leave as is
firstlist[0] = firstlist[0];
firstlist[1] = firstlist[1];
firstlist[2] = firstlist[2] - 1;
break;
case 11:
firstlist[0] = firstlist[0];
firstlist[1] = firstlist[1];
firstlist[2] = firstlist[2] + 1;
break;
}
-
September 5th, 2010, 05:10 PM
#2
Re: I need help with returning my list in one method to my labels in another method.
Honestly, I have no idea what you are trying to do. It looks real similar to the code you posted last week.
The best thing you can do is to start small and first understand the basics and learn how to use the debugger.
For example, write a simple program like:
Code:
using System;
namespace Loop
{
class Program
{
static void Main( string [ ] args )
{
for ( int i = 1; i > 0; i++ )
{
Console.WriteLine( "Line: {0}", i );
}
}
}
}
Set a breakpoint by putting the cursor on the Console.WriteLine statement and press F9. Next, debug the code with F5. The code should startup and stop on the Console.WriteLine statement. When it does hover the mouse pointer over the 'i' variable - you should see the value of 1. Next, press F5 again and hover the mouse over the 'i' variable again. You'll see the 'i' value is now 2.
This is the basics of debugging, i.e. stopping at points in your code and checking values. For a beginner, everyone talks about debugging and it seems so complicated (because no one ever explains how), but it's actually very simple.
After you've checked the 'i' value a couple of times, with the mouse hovered over the Console.WriteLine statement, press F9 to remove the breakpoint. Next, hit F5 to continue debugging. You'll notice that the program keeps running and running, etc in an infinite loop.
Now you may wonder why I don't simply point out the problems with the code you've posted. The reason is simple that you need to break the program apart and start by learning the basics (such as how to properly write a for loop).
In fact, after you've learned the for loop and debugging, I would suggest that you comment out most of your code and get rid of the random methods until you can copy over the arrays (btw, they aren't lists, they are arrays). Once you get the array code working you can reintroduce the random code.
-
September 5th, 2010, 06:29 PM
#3
Re: I need help with returning my list in one method to my labels in another method.
Arjay,
Thanks for the reply, but i was just asking a question dealing on how to return the number's in my list to the label thats all. I know how to return one number from the list to one label. I just want to know what is the logic or what's the code to return many items from the list to many labels. Meaning
list item 0 = label 1
list item 1 = label 2
list item 2 = label 3
etc..
Debugging will not help me get this logic. If it was dealing with debugging i wouldn't be posting it here. I did post something similar with previous post. But if you go back you will also see that i had figured it out also but this seems like a logic or more of something else such as a different method or something i haven't learned yet.. but please know that this is nottttttttttttttt.... a debuging issue......
-
September 6th, 2010, 01:45 AM
#4
Re: I need help with returning my list in one method to my labels in another method.
try to use a for loop for returning multiple times...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|