Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a Dictionary
XML
Dictionary<string, string> Keywords = new Dictionary<string, string>();
Keywords.Add("start", "START");
Keywords.Add("int", "DT");
Keywords.Add("real", "DT");
.
.



this function is in a class and call many times if i define dictionary in it, every time it takes time. so where should i initialize it? if i do this in main() then how can i call it in my class?

C#
public string IsKeyword(string value)
{ 
}
Posted

Define the dictionary at the class level.
C#
class myClass
{
  Dictionary<string,> Keywords = new Dictionary<string,>();

  public void DictionaryInitializer()
  {
     Keywords.Clear();
     Keywords.Add("start", "START");
     Keywords.Add("int", "DT");
     Keywords.Add("real", "DT");
  }

}
 
Share this answer
 
v2
Comments
Sweety Khan 27-Aug-11 15:04pm    
gOod idea :) i define the dictionary in myClass constructor. is it fine from time saving and other factors point of view?
Sergey Alexandrovich Kryukov 27-Aug-11 23:41pm    
No. Declare at the class level and ***initialize*** where it is declared as Abhinav's code shows. You can also initialize if in constructor, but it makes no sense at all, as initialization does not depend on constructor parameters, previously called methods and anything like that. In this case, initializing at the point of declaration is safer.
--SA
Sergey Alexandrovich Kryukov 27-Aug-11 23:41pm    
Sure, my 5.
--SA
Abhinav S 28-Aug-11 1:20am    
Thank you SA.
Sweety Khan 28-Aug-11 2:43am    
okii i got it. so means before calling IsKeyword method or any i must first call the method DictionaryInitializer().
Of course both Abhinav's and SAK's wise comments point you in the right direction, but let me add one supplementary ... perhaps superfluous ... comment:

Where you initialize the Dictionary depends on its strategic use in the 'total scenario' in your project.

1. if the Dictionary object is part of a class that is instantiated many times, and it is your intent that each instance of the class has a private instance of this Dictionary, obviously the instantiation belongs in the class.

2. if the Dictionary object is a global resource for many classes (forms, whatever), and is invariant, you can consider putting it in a static class, exposing only those methods of access to it you wish, etc. Or, the Dictionary could be defined in a 'Singleton' Class.

best, Bill
 
Share this answer
 
Comments
Sweety Khan 28-Aug-11 2:44am    
i got ur point. thanx
Abhinav S 28-Aug-11 2:51am    
This is a good point.
Assuming that the keywords are constant, then it makes more sense that the collection is defined at the class level. The previous solutions have already mentioned that. I just want to add that since C#4 (or 3?) it's possible to initialize the collections directly (as sa has already mentioned above).


a complete class would then look as follows:

C#
static class myClass
{
    static Dictionary<string, string> Keywords = new Dictionary<string, string>() 
        {   
            {"start", "START" },
            { "int", "DT" },
            {"real", "DT"}
        };

    public static string IsKeyword(string value)
    { 
        return "???";
    }
}

I'm a bit confused about the signature of the IsKeyword method. for me as a user of the class it doesn't make sense to have a Is... method that returns a string, that's why I left the implementation empty.
 
Share this answer
 
v2
Comments
Sweety Khan 28-Aug-11 2:34am    
yeh u r right, me realized this toO. i was trying to do 2 things by one method. but now i have made two methods public bool IsKeyword and public string WHichKeyword.
Sweety Khan 28-Aug-11 2:59am    
one thing more u said c# 3 or 4 but i dont understand tht how can i know this. i only understand by their names that they are versions and they may have different features. i learned one c# and now working on visualstdio 2010 so whats the story plz tell me
Reto Ravasio 28-Aug-11 3:14am    
VS2010 uses C#4 by default but you can tell it to use another version. (Properties/Application/Target framework)
Sweety Khan 28-Aug-11 3:25am    
oki thanx a lOt :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900