Solved

Creating a public list

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

Industry Leaders: 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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

630 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