C# How to index arrays

Course Queries Syllabus Queries 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Syllabus Queries related to Course Queries. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 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);
        }
    }
}
profilepic.png
manpreet 2 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.