Solved

Creating a public list

Posted on 2013-05-20
5
275 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
[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
  • 3
  • 2
5 Comments
 
LVL 75

Accepted Solution

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

763 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