Solved

Creating a public list

Posted on 2013-05-20
5
270 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 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
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
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now