Categories

See More
Popular Forum

MBA (4887) B.Tech (1769) Engineering (1486) Class 12 (1030) Study Abroad (1004) Computer Science and Engineering (988) Business Management Studies (865) BBA (846) Diploma (746) CAT (651) B.Com (648) B.Sc (643) JEE Mains (618) Mechanical Engineering (574) Exam (525) India (462) Career (452) All Time Q&A (439) Mass Communication (427) BCA (417) Science (384) Computers & IT (Non-Engg) (383) Medicine & Health Sciences (381) Hotel Management (373) Civil Engineering (353) MCA (349) Tuteehub Top Questions (348) Distance (340) Colleges in India (334)
See More

C# How to index arrays

Course Queries Syllabus Queries
Max. 2000 characters
Replies

usr_profile.png
Rahul Chaudhary

User

( 5 months 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, 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);
        }
    }
}

usr_profile.png
Liza Sain

User

( 5 months 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]);
        }
    }

what's your interest


forum_ban8_5d8c5fd7cf6f7.gif