Results 1 to 10 of 10

Thread: Index

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Posts
    155

    Index

    hi:
    What Is the Equivalent of index as in vb6 to C#?

    say I have 55 Textboxes, I need to place 50 data in only 50 textboxes, the remaining 5 are for other usage. How can I approach this ?

    (It is easy using index in VB6)



    Thanks

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

    Re: Index

    The design time support for control arrays doesn't exist in C# or VB.NET because it isn't needed. If you want to access objects by index then create an array and assign those objects to the elements. If those objects are TextBoxes then they still get treated the same way. An array is an array regardless.
    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

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Posts
    155

    Re: Index

    Any chance you can show me how to do this?

    Thanks

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Index

    C# Code:
    1. TextBox[] textBoxArray = new TextBox[2];
    2.  
    3.         private void Form1_Load(object sender, EventArgs e)
    4.         {
    5.             textBoxArray[0] = textBox1;
    6.             textBoxArray[1] = textBox2;
    7.             textBoxArray[2] = textBox3;
    8.         }
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Posts
    155

    Re: Index

    Quote Originally Posted by Atheist
    C# Code:
    1. TextBox[] textBoxArray = new TextBox[2];
    2.  
    3.         private void Form1_Load(object sender, EventArgs e)
    4.         {
    5.             textBoxArray[0] = textBox1;
    6.             textBoxArray[1] = textBox2;
    7.             textBoxArray[2] = textBox3;
    8.         }
    There are errors:

    "Error 3 Preprocessor directives must appear as the first non-whitespace character on a line"


  6. #6
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Index

    Sorry about that. It's a bug in the forum software which hasn't been fixed yet.

    Here's what the code should look like:
    Quote Originally Posted by Atheist
    Code:
            TextBox[] textBoxArray = new TextBox[2];
    
            private void Form1_Load(object sender, EventArgs e)
            {
                textBoxArray[0] = textBox1;
                textBoxArray[1] = textBox2;
                textBoxArray[2] = textBox3;
            }

  7. #7
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: Index

    I don't know but wouldn't it be better to add...

    Code:
    textBoxArray[0] = textBox1;
    textBoxArray[1] = textBox2;
    textBoxArray[2] = textBox3;
    in the InitializeComponent event instead?

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

    Re: Index

    Quote Originally Posted by Paul M
    I don't know but wouldn't it be better to add...

    Code:
    textBoxArray[0] = textBox1;
    textBoxArray[1] = textBox2;
    textBoxArray[2] = textBox3;
    in the InitializeComponent event instead?
    There is no InitializeComponent event. InitializeComponent is a method and it's generated by the IDE. You should not be editing any code in that method except in direst need. You can add code to a constructor, AFTER the call to InitializeComponent. That would be just as legitimate as adding it to the Load event handler but it really would offer no advantage. The only way that could be better is if you were going to try to access that array's elements before calling Show or ShowDialog on the form, and I can't think of a legitimate reason for that.
    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
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: Index

    It is the Class Constructor though, no?

    Thats why i assumed it would be best to do it there.

  10. #10
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Index

    Quote Originally Posted by Paul M
    It is the Class Constructor though, no?
    No. The constructor is the constructor.

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