|
-
Feb 2nd, 2007, 04:51 PM
#1
Equivilant of an object array?
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?
-
Feb 2nd, 2007, 04:56 PM
#2
Lively Member
Re: Equivilant of an object array?
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
Code:
object[] objects = new object[42];
Although there are a couple ways to declare and initialize an array.
-
Feb 2nd, 2007, 05:03 PM
#3
Re: Equivilant of an object array?
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...
Code:
int[] myNumbers = new int[5];
Assuming the label is named lblNumber, how would I go about creating that array?
-
Feb 2nd, 2007, 05:15 PM
#4
Re: Equivilant of an object array?
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:
Code:
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 };
}
-
Feb 2nd, 2007, 05:16 PM
#5
Re: Equivilant of an object array?
So it creates the array by recieving the names of the objects, not the other way around like VB? Intriguing...
-
Feb 2nd, 2007, 05:17 PM
#6
Lively Member
Re: Equivilant of an object array?
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
-
Feb 2nd, 2007, 05:20 PM
#7
Re: Equivilant of an object array?
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...
-
Feb 2nd, 2007, 05:22 PM
#8
Re: Equivilant of an object array?
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.
-
Feb 2nd, 2007, 09:37 PM
#9
Re: Equivilant of an object array?
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...
-
Feb 2nd, 2007, 09:45 PM
#10
Re: Equivilant of an object array?
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.
-
Feb 2nd, 2007, 09:56 PM
#11
Re: Equivilant of an object array?
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.
-
Feb 3rd, 2007, 08:22 PM
#12
Re: Equivilant of an object array?
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....=for+loop+C%23
http://search.microsoft.com/results....each+loop+C%23
http://search.microsoft.com/results....each+loop+C%23
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
|