Solved

Problem using an array.

Posted on 2004-04-13
3
227 Views
Last Modified: 2010-04-15
I created an array (using ArrayList()) called load when my form loads. I use the array as values for my combobox.
I am trying to use this array to assign a value to a text box when the combobox value is selected using the following code.

public void comboBox1_SelectedValueChanged(object sender, System.EventArgs e)
             {
                
      if(comboBox1.SelectedIndex != -1)
        Console.WriteLine(comboBox1.SelectedValue.ToString());
      else
        label2.Text=load[comboBox1.SelectedIndex].ToString();
                  
             }

My problem is I am from the old school and can't make my array Public. What would be a work around for this? Can someone shed some light on the theory why its like this?
0
Comment
Question by:Jimbo456
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 10819539
if(comboBox1.SelectedIndex != -1)
    access comboBox1.SelectedItem (which is the array element if you used databinding)
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 10819542
o yeah also your code will crash most likely since it will only ever try to access the array at element[-1]
0
 
LVL 4

Accepted Solution

by:
caner_elci earned 500 total points
ID: 10820612
Try some code like this:

ArrayList load = new ArrayList();

private void Form1_Load(object sender, System.EventArgs e)
{
      // Initialize load array
      load.Add( "Sample value 1" );
      load.Add( "Sample value 2" );
      load.Add( "Sample value 3" );
      load.Add( "Sample value 4" );
      load.Add( "Sample value 5" );

      // Add them to comboBox1, but first clear it
      comboBox1.Items.Clear();
      foreach( string val in load )
            comboBox1.Items.Add( val );
}

// Use this event, not SelectedValueChanged
private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
      if( comboBox1.SelectedIndex >= 0 )
            textBox1.Text = (string)load[comboBox1.SelectedIndex];
}
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question