Solved

Why does List<string,bool> not work?

Posted on 2009-05-20
7
2,715 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

867 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

24 Experts available now in Live!

Get 1:1 Help Now