PDA

Click to See Complete Forum and Search --> : Equivilant of an object array?


timeshifter
Feb 2nd, 2007, 03:51 PM
I'm stumped here. I just started up on C#, and I'm trying to rewrite some of my old VB6 projects to get a feel for how the new language works... this little Lottery program would be a good one, except it uses several arrays to do its calculations, and I can't figure out what the C# parallel is. I know how to do it with variables, but that's not the problem.. the problem is the labels...

Any pointers?

Buoy
Feb 2nd, 2007, 03:56 PM
What do you mean by "the problem is the labels"? Not sure if you're having trouble declaring an array of objects? If so, it would something like


object[] objects = new object[42];

Although there are a couple ways to declare and initialize an array.

timeshifter
Feb 2nd, 2007, 04:03 PM
Sorry... allow me to be a bit more specific.

This requires one array of 6 labels, and a corresponding array of six numbers. I got the numbers part...

int[] myNumbers = new int[5];

Assuming the label is named lblNumber, how would I go about creating that array?

jmcilhinney
Feb 2nd, 2007, 04:15 PM
When you create an array in C# you specify a length, not an upper bound. If you want an array of six integers then you specify 6, not 5. As for your Labels, they presumably already exist so you just create an array and initialise it with those existing Labels:int[] numbers = new int[6];
Label[] labels;

private void Form1_Load(object sender, EventArgs e)
{
this.labels = new Label[] { this.label1,
this.label2,
this.label3,
this.label4,
this.label5,
this.label6 };
}

timeshifter
Feb 2nd, 2007, 04:16 PM
So it creates the array by recieving the names of the objects, not the other way around like VB? Intriguing...

Buoy
Feb 2nd, 2007, 04:17 PM
Oh, that's a Classic VB thing, isn't it? - a control array? Sorry, not much good at Classic VB.

Not sure what you need to do with your labels - but if you need to loop through them, you could add your labels to an array labels. What are you needing to do with your labels?

Also note that when you declared your array of int's, you created 5 slots, not the 6 you're needing.

Edit: Little slow I guess - just what jmcilhinney said :)

timeshifter
Feb 2nd, 2007, 04:20 PM
No biggie.

I want an array so it's easier to loop through them for clearing and sorting... I suppose it's not vital... I could do it without an array.. but it would just be a lot more code. I'm trying to preserve as much of my VB style as possible, just for a new syntax. A lot of my stuff was built for speed...

jmcilhinney
Feb 2nd, 2007, 04:22 PM
There are no control arrays in .NET, VB or otherwise. There is a lot of information on how to do things in VB.NET for which you might have used a control array in VB6. The principles are exactly the same in C#, except the C# array syntax is a little different. Basically, an array is an array in .NET, whether it contains controls or anything else. There is no design time support and controls do not have an Index. The .NET event model really makes control arrays unnecessary.

timeshifter
Feb 2nd, 2007, 08:37 PM
Okay.. in that case, care to show me how I'd treat those six labels as an array? Stuff such as looping through them and interacting with each member...

jmcilhinney
Feb 2nd, 2007, 08:45 PM
I've already shown you how to create an array containing the Labels. As for looping through them, how do you normally loop through an array? With a for or foreach loop.

timeshifter
Feb 2nd, 2007, 08:56 PM
Yeah.. i know that... but as I said (I think) I just started C# today. I know almost nothing about it. And just to clear up the classic "we don't do homework for you" thing, I'm not even in school. I'm doing this because I want to learn the language.

Honestly, I don't even know how to make a For loop. When I ask a question, assume I'm an idiot. With this language, I am.

jmcilhinney
Feb 3rd, 2007, 07:22 PM
Just like VB and VB.NET, you can find most of what you need, and certainly when it comes to the langauge itself, with a simple search of MSDN. Even an idiot can do that, so your not being an idiot means that you'll have no trouble. :)

http://search.microsoft.com/results.aspx?mkt=en-AU&setlang=en-AU&q=for+loop+C%23
http://search.microsoft.com/results.aspx?mkt=en-AU&setlang=en-AU&q=for+each+loop+C%23
http://search.microsoft.com/results.aspx?mkt=en-AU&setlang=en-AU&q=foreach+loop+C%23