Solved

Why does List<string,bool> not work?

Posted on 2009-05-20
7
3,016 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
[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
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
Technology Partners: 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!

 
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

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

749 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