Solved

Why does List<string,bool> not work?

Posted on 2009-05-20
7
3,100 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…
Suggested Courses

734 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