Hey guys,

I need a little help this is what I currently have:

Object[][] teamList = new Object[10][];
Label[] tlKnights = new Label[25];
Label[] tlSkullitz = new Label[25];

teamList[0] = tlKnights;
teamList[1] = tlSkullitz;

tl -- stands for Team Label, Im working on better naming conventions as well.

I'm new to programming and I'm making a program that analyzes my draft picks in an upcoming fantasy sports draft. What I'd like to do is be able to pass a value into my method so that it fills the correct label on my form with the drafted player. So I've made an array of label Arrays and I'd like to be able to choose the label like this:

teamList[0][0].text

I'd like it to select the first label from the first list of label arrays so I can set the Name in the labels text value. I can't find any information on Jagged arrays that address my situation. I know I could just make massive IF statements and multiple methods to get around this issue but I'm trying to become a more robust programmer and cut down on my code. If you could help, that be great.

In the end there will be 10 teams but I was just testing with 2 to start.

Thank you!

Hey guys,
snipped
So I've made an array of label Arrays and I'd like to be able to choose the label like this:

teamList[0][0].text

I'd like it to select the first label from the first list of label arrays so I can set the Name in the labels text value.
snipped

I'm assuming all these labels are not set up to display on your form and you want to transfer the label data to a label that is displayed.
I haven't tested it but this should work:

MyLbl.Text = ((Label[]) teamList[0])[0].Text;

Splitting it out a bit:

Label[] jnk = (Label[]) teamList[0];//Cast the object as a Label array
MyLbl.Text = jnk[0].Text;// Transfer the text to an object that is displayed

Personally, I like the second version better. It's easier for me to read and I wrote it to begin with.

It currently isn't jagged, you do know you could create a two dimensional array of labels?

It currently isn't jagged, you do know you could create a two dimensional array of labels?

Nope, Please explain.

It currently isn't jagged, you do know you could create a two dimensional array of labels?

I'm assuming all these labels are not set up to display on your form and you want to transfer the label data to a label that is displayed.
I haven't tested it but this should work:

MyLbl.Text = ((Label[]) teamList[0])[0].Text;

Splitting it out a bit:

Label[] jnk = (Label[]) teamList[0];//Cast the object as a Label array
MyLbl.Text = jnk[0].Text;// Transfer the text to an object that is displayed

Personally, I like the second version better. It's easier for me to read and I wrote it to begin with.

That worked great! thank you so much!

Hmmm, not only easier to read, but I started to second guess myself and thought I'd made a mistake in the first line's set of parenthesis

MyLbl.Text = ((Label[]) teamList[0])[0].Text;

Re-reading it, I'm thinking it's right again. (Label[]) teamList[0] cast the first object back to a Label array. The parenthesis around that allows you to point to an item in the array "(...)[0]" and then the property is just part of that one Label.

Edited 6 Years Ago by kplcjl: right the first time

Hey guys,

I need a little help this is what I currently have:

Object[][] teamList = new Object[10][];
Label[] tlKnights = new Label[25];
Label[] tlSkullitz = new Label[25];

teamList[0] = tlKnights;
teamList[1] = tlSkullitz;

snip

For a two dimensional array solution:

Label[,] teamList = new Label[10,25];
teamList[0,0] = ...;//first label setup for tlKnights
teamList[1,0] = ...;//first label setup for tlSkullitz

Either you know the first number = 0 is for the tlKnights label and = 1 is for tlSkullitz and you never reference those objects or add

string[] teamNames= new string[10];
teamNames[0] = 'tlKnights';
teamNames[1] = 'tlSkullitz';

and now you can dynamically change the team names if a team decided they wanted to rename what to call their team. Again, objects tlKnights and tlSkullitz don't exist.
Also, is there any reason why you are using labels to store your data when a string array will work with what has been shown and would use less space?

For a two dimensional array solution:

Label[,] teamList = new Label[10,25];
teamList[0,0] = ...;//first label setup for tlKnights
teamList[1,0] = ...;//first label setup for tlSkullitz

Either you know the first number = 0 is for the tlKnights label and = 1 is for tlSkullitz and you never reference those objects or add

string[] teamNames= new string[10];
teamNames[0] = 'tlKnights';
teamNames[1] = 'tlSkullitz';

and now you can dynamically change the team names if a team decided they wanted to rename what to call their team. Again, objects tlKnights and tlSkullitz don't exist.
Also, is there any reason why you are using labels to store your data when a string array will work with what has been shown and would use less space?

I do have a string array to hold players names (per team) but I wanted to be able to update on the form what players were drafted easily, without having to type out every label name. This way I can just loop through the label array and set teamList[4 which is knights])[0 which is catcher].text to Joe Mauer.

I also didn't want to create separate draft methods for all of the teams. This way, I have an Radio group on the form that changes with every pick and when a pick is made a value is passed into my methods that says what team is picking so I only had to make one method.

So I have now

((Label []) teamList[sumInt])[anotherInt].text = strSelectedName

You are making a lot of extra work for yourself. If you are always going to store 10 teams and each team always has 25 players then a 2D array is perfect (as kplcjl mentioned).
First dimension is teams, second is players:

Label[,] teamList = new Label[10,25];
teamList[0,0] = ...;//team 1 player 1
teamList[0,1] = ...;//team 1 player 2
teamList[0,2] = ...;//team 1 player 3
teamList[1,0] = ...;//team 2 player 1
teamList[1,1] = ...;//team 2 player 2
teamList[1,24] = ...;//team 2 player 25

If you want to keep them as seperate arrays, you can create a jagged array of labels:

Label[][] teamList = new Label[10][];
            Label[] tlKnights = new Label[25];
            Label[] tlSkullitz = new Label[25];

            teamList[0] = tlKnights;
            teamList[1] = tlSkullitz;

            teamList[0][0].Text = "Team1 - Player1";
            teamList[1][0].Text = "Team2 - Player1";

If all the arrays will be arrays of labels this avoids the need to cast from Object to Label.

Be aware that you need to initialise your labels before you use them:

teamList[0][0] = new Label(); //if you remove this line the next line will throw a NullReferenceException
            teamList[0][0].Text = "Team1 - Player1";
This article has been dead for over six months. Start a new discussion instead.