Solved

Why does List<string,bool> not work?

Posted on 2009-05-20
7
2,603 Views
Last Modified: 2012-05-07
Hello,

I dont wanna use the below code:

I want to use instead: List<string,bool> directly

Is that not possible?
class Article()

{

    string article {get; set;}

    bool isFiltered {get;set}

}
 

var myArticlesList = new List<Article>();

Open in new window

0
Comment
Question by:MSFanboy
7 Comments
 
LVL 11

Expert Comment

by:saragani
ID: 24429984
You can't do a List of String,Bool.
There is no such thing. List is defined as List<T>, not List<X,Y>

If you need a "List" that contains Key and Value (The Key must be Uniqueu) then use Dictionary.

However, Dictionary does not maintain order (Foreach, or for int i = ... to count) does not give the items in the order that they were added to the Dictonary.
0
 
LVL 2

Expert Comment

by:Saari_Lex
ID: 24430532
What are you actually trying to achieve?
Do you simply want to iterate through a collection? Do you want the collection to maintain the order of the objects? Do you want to be able to sort the collection? Will you be using the collection to populate a data table?

Can a Dictionary help, as suggested by saragani?

..or a ListDictionary? http://msdn.microsoft.com/en-us/library/system.collections.specialized.listdictionary(VS.80).aspx

...or an OrderedDictionary? http://msdn.microsoft.com/en-us/library/system.collections.specialized.ordereddictionary(VS.80).aspx

...or a SortedDictionary? http://msdn.microsoft.com/en-us/library/f7fta44c(VS.80).aspx
0
 

Author Comment

by:MSFanboy
ID: 24430593
I want to write 2 values in sort of a Array to pass to the for loop where I do SQL statements.
I use now Multi-Dimensonal Array string[,] myarray = new string[1000,2];
that fits and I guess its fastest Array compared to any Dictionary.


0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 6

Expert Comment

by:HarryNS
ID: 24430630
You can probably use this,

Dictionary<string, bool> a = new Dictionary<string, bool>();
            a.Add("Test", true);
            a.Add("Test1", false);
            a.Add("Test2", true);
            a.Add("Test3", false);

            foreach (KeyValuePair<string, bool> item in a)
            {
                MessageBox.Show(item.Key);
                MessageBox.Show(item.Value.ToString());
            }
0
 
LVL 6

Expert Comment

by:HarryNS
ID: 24430634
You can probably use this,

Dictionary<string, bool> a = new Dictionary<string, bool>();
            a.Add("Test", true);
            a.Add("Test1", false);
            a.Add("Test2", true);
            a.Add("Test3", false);

            foreach (KeyValuePair<string, bool> item in a)
            {
                MessageBox.Show(item.Key);
                MessageBox.Show(item.Value.ToString());
            }
0
 
LVL 9

Accepted Solution

by:
tculler earned 500 total points
ID: 24432160
There are countless ways to do this, and from the sounds of your issue, using a Dictionary is definitely not one of them. I may be mistaken, but it sounds like you may have several keys that have the same values. A Dictionary is, well, a dictionary! You need to have a way to differentiate the items within it, but without sacrificing the integrity of its data.

What you have right now is probably pretty close to the best; why not use that? You could make your own class (called an ArticleCollection or something) that inherits directly from List<Article>. I'll give a sample demonstration. Other than that, what you have is pretty much as good as it gets; the List class only has one type parameter, and for good reason.
public class Article

{

     private System.String _article;

     private System.Boolean _filtered;
 

     public System.String Article

     {

          get { return this._article; }

          set { this._article = value; }

     }
 

     public System.Boolean IsFiltered

     {

          get { return this._filtered; }

          set { this._filtered = value; }

     }

}
 

public class ArticleCollection : System.Collections.Generic.List<Article>

{

     // Call base constructors so your class will basically be identical to the List<Article> class

     public ArticleCollection()

          : base() // Unnecessary, but clarity is always good

     {}

     public ArticleCollection(System.Int32 capacity)

          : base(capacity)

     {}

     public ArticleCollection(System.Collections.Generic.IEnumerable<Article> collection)

          : base(collection)

     {}
 

     // Not required, but this is a prime case for iterators. Figured I'd include one for you :)

     public System.Collections.Generic.IEnumerable<System.String> GetTitles()

     {

          foreach(Article art in this)

          {

               yield return art.Article;

          }

     }

     // Add whatever more functionality you need

}

Open in new window

0
 

Author Closing Comment

by:MSFanboy
ID: 31583428
I sticked to my Multidimensional Array  (concerning speed) and due to comfortable coding I did a Article class with properties string + bool , sort of overhead but very comfortable...
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 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

12 Experts available now in Live!

Get 1:1 Help Now