Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Creating a public list

Posted on 2013-05-20
5
Medium Priority
?
278 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 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

Independent Software Vendors: 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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

688 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