Solved

Why does List<string,bool> not work?

Posted on 2009-05-20
7
2,825 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Coding C# in Linux 8 72
Code works but it's slow 28 72
Install Problem 13 32
Example works but why do I need to click twice on button 4 43
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

825 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