Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Creating a public list

Posted on 2013-05-20
5
Medium Priority
?
279 Views
Last Modified: 2013-06-03
Hi guys,

So I started with the following within a SelectedIndexChanged event...

List<String> lst = new List<String>();

 foreach (DataColumn dc in (DtSet.Tables[0].Columns))
                {
                    lst.Add(dc.ToString());
                }

Open in new window


So basically I put the column headers into a list (column headers can vary in name).

Now I realise it was stupid and they need to be public so as I can add and remove from the list anywhere else in the project.

Ive got this far...

public class ListMatch
        {
            public string SpreadSheetCol1 { get; set; }
            public string SpreadSheetCol2 { get; set; }
            public string SpreadSheetCol3 { get; set; }
            public string SpreadSheetCol4 { get; set; }
            public string SpreadSheetCol5 { get; set; }
            
        }

Then back in my event I have 

List<ListMatch> lst = new List<ListMatch>();

Open in new window


How can I create a foreach loop similar to my original one but to store the column headers in SpreadSheetCol1,SpreadSheetCol2 etc etc.

Thanks,
Dean
0
Comment
Question by:deanlee17
  • 3
  • 2
5 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 39181074
Why wouldn't you just make the List itself public?

e.g.

public class YourClass
{
    public List<String> lst = new List<String>();

...
}

Open in new window

0
 

Author Comment

by:deanlee17
ID: 39181093
So would that work for...

private void ddl_TabNames_SelectedIndexChanged(object sender, RoutedEventArgs e)
        {
            try
            {

                   public List<String> lst = new List<String>();
                    
                     foreach (DataColumn dc in (DtSet.Tables[0].Columns))
                        {
                            lst.Add(dc.ToString());
                         } 

                }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.ToString());
            }

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39181138
You would strike line 6. The list would be moved to class-level (what I posted), and it would be accessible to all areas of the class. The public would make it accessible throughout your assembly--at least to code that has a reference to an instance of the class.
0
 

Author Comment

by:deanlee17
ID: 39181156
My problem then will be accessing the dataset that is filling the list. I should have done this originally, but this is my current event...

private void ddl_TabNames_SelectedIndexChanged(object sender, RoutedEventArgs e)
        {
            try
            {

                System.Data.OleDb.OleDbConnection MyConnection;
                System.Data.DataSet DtSet;
                System.Data.OleDb.OleDbDataAdapter MyCommand;
                MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + Create_Instance_of_SearchResults.TheFileName + "';Extended Properties='Excel 12.0 xml;HDR=Yes;'");
                MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [" + (ddl_TabNames.SelectedItem.ToString()) + "]", MyConnection);
                MyCommand.TableMappings.Add("Table", "TestTable");
                DtSet = new System.Data.DataSet();
                MyCommand.Fill(DtSet);

                DtSet.Tables[0].Columns.Add("Remove", typeof(Boolean));
                DtSet.Tables[0].Columns["Remove"].SetOrdinal(0);

                gridControl1.ItemsSource = DtSet.Tables[0];



                // Assign column names to ddl's
               List<string> lst = new List<string>();

                //foreach (Auction item in auctions)


                foreach (DataColumn dc in (DtSet.Tables[0].Columns))
                {
                    lst.Add(dc.ToString());
                }

                // Assign list to all dropdown boxes

                int Newcount = VisualTreeHelper.GetChildrenCount(gpbColumnHeaders_grid);
                for (int i = 0; i < Newcount; i++)
                {
                    Visual childVisual = (Visual)VisualTreeHelper.GetChild(gpbColumnHeaders_grid, i);
                    if (childVisual is System.Windows.Controls.ComboBox)
                    {
                        ((System.Windows.Controls.ComboBox)childVisual).ItemsSource = lst;
                    }

             
                }


                MyConnection.Close();

                MyCommand.Dispose();
                MyConnection.Dispose();
                DtSet.Dispose();


            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.ToString());
            }

        }

Open in new window


How would you code that to make the list accessible publically?
0
 

Author Comment

by:deanlee17
ID: 39183754
Ok I now have the following, is this the correct was to go about things?...

 public class ListOfColumnHeaders
            {

                public List<String> lst = new List<String>();
            } 

private ListOfColumnHeaders Create_Instance_of_ListOfColumnHeaders;

  private void ddl_TabNames_SelectedIndexChanged(object sender, RoutedEventArgs e)
        {
            try
            {
                        .......
                       foreach (DataColumn dc in (DtSet.Tables[0].Columns))
                             {
                                 Create_Instance_of_ListOfColumnHeaders.lst.Add(dc.ToString());
                              }
              }
               catch (Exception ex)
                    {
                         System.Windows.MessageBox.Show(ex.ToString());
                     }

            }

Open in new window


I feel as tho 'private ListOfColumnHeaders Create_Instance_of_ListOfColumnHeaders;' should probably be sitting encapsulated elsewhere?

Many Thanks,
Dean
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Screencast - Getting to Know the Pipeline
Suggested Courses

916 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