Results 1 to 12 of 12

Thread: Equivilant of an object array?

  1. #1

    Thread Starter
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    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?

  2. #2
    Lively Member
    Join Date
    Oct 2006
    Posts
    71

    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.

  3. #3

    Thread Starter
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    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?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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 };
    }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    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...

  6. #6
    Lively Member
    Join Date
    Oct 2006
    Posts
    71

    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

  7. #7

    Thread Starter
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    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...

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    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...

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    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.

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width