C# How to index arrays

Course Queries Syllabus Queries 3 years ago

8.34K 2 0 0 0

User submissions are the sole responsibility of contributors, with TuteeHUB disclaiming liability for accuracy, copyrights, or consequences of use; content is for informational purposes only and not professional advice.

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 3 years ago

 

I want to add strings of a particular array in a combobox when I select item in another combobox and click a button. There are two arrays">arrays, S0 and S1. In S0 there are strings for syllabus of Mathemtics, and in S1 there are strings of syllabus for English.

My code:

for (int x = 0; x <= 1 ; x++)
{
    if (comboBox1.SelectedIndex == x)
    {
        foreach (string items in S+x  )
        {
            comboBox2.Items.Add(items);
        }
    }
}
0 views
0 shares

profilepic.png
manpreet 3 years ago

Unless you are planning to add additional subjects to the system, just use 2 separate foreach loops. If you want to make it expandible, use a 2 dimensional array of strings:

string[,] data;

//Fill data here using the first dimension for the subjects and the second for the different items in the subject. data[0,0] could be "Pythagoras" and data[1,0] "Shakespeare" for example.

for (int i=0; i<data.GetLength(0); i++)
    {
    for (int j=0; j<data.GetLength(1); j++)
        {
        comboBox2.Items.Add(data[i, j]);
        }
    }

0 views   0 shares

No matter what stage you're at in your education or career, TuteeHUB will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Similar Forum