How to access a generic property argument in the following example:  
Author Message
Aleksey Nagoga.





PostPosted: Visual C# Language, How to access a generic property argument in the following example: Top

The calling code looks as follow: Session[KeyName]

I need to know the value of the KeyName inside the Session property below.

private static Dictionary<string, object> m_Session = new Dictionary<string, object>();

public static Dictionary<string, object> Session

{

      get

       {

            //need to check here whether a requested dictionary KeyName

            //exists before attempting to return its value.

            return m_Session;

       }

        set { m_Session = value; }

}



Visual C#18  
 
 
Mark Dawson





PostPosted: Visual C# Language, How to access a generic property argument in the following example: Top

Hi,

the ContainsKey method will tell you if a key exists in the dictionary i.e.

Dictionary<string, object> dict = new Dictionary<string, object>();

dict.ContainsKey("abcd");

Mark.



 
 
AlekseyNagoga





PostPosted: Visual C# Language, How to access a generic property argument in the following example: Top

Thank you, Mark. I guess I had to be more specific in my question. I need to know what to pass to ContainsKey method. I need to know how to retrieve the key value ("abcd" in your example above).
 
 
Figo Fei - MSFT





PostPosted: Visual C# Language, How to access a generic property argument in the following example: Top

Hi,

The signature is:   public bool ContainsKey (TKey key)


"abcd" mentioned above is the keyname you want to check.



 
 
Mark Dawson





PostPosted: Visual C# Language, How to access a generic property argument in the following example: Top

Hi,

you can do the following:

Dictionary<string, object> dict = new Dictionary<string, object>();

if (dict.ContainsKey("abcd"))

{

object o = dict["abcd"];

//do some work...

}

Mark.



 
 
boban.s





PostPosted: Visual C# Language, How to access a generic property argument in the following example: Top

You need to loop through all items in dictionary and to get the right one with same key value. For that you must use structure KeyValuePair:
        public bool CheckSessionKey(string keyName)
        {
            foreach (KeyValuePair<string, object> kvp in m_Session)
            {
                if (kvp.Key == keyName)
  
                return true;
            }
            return false;
        }

but this method should be called before you access the property, because you can't pass the keyName value. I have just show you how to loop trough Dictionary elements.



 
 
Aleksey Nagoga.





PostPosted: Visual C# Language, How to access a generic property argument in the following example: Top

Thank you, boban.s. If you are talking about calling the CheckSessionKey from the caller's side then it is not neccesary in my case as I already can do something like:

if (Session.Contains(KeyName))
{
    return Session[KeyName];
}
else
{
   return new object;
}


What I am trying to avoid is writing the "if" block above for every time I need to call "Session[KeyName]";

of course as a last resort I always can do something like:

======== CALLING SIDE =========

object MyObject1 = this.session(sKeyName);
object MyObject2 = this.session(sKeyName)
object MyObject3 = this.session(sKeyName)
//... n times I need to call session


private object session(string sKeyName)
{
   if (Session.Contains(KeyName))
   {
       return Session[KeyName];
   }
   else
   {
      return new object;
   }
}

======== RECEIVING SIDE =========

private static Dictionary<string, object> m_Session = new Dictionary<string, object>();

public static Dictionary<string, object> Session
{
      get
       {
            return m_Session;
       }
        set { m_Session = value; }
}

==============================

but I was trying to see if there is a way to know the KeyName value INSIDE the  Session property on RECEIVING side.


The rest of the folks answered:
Thank you for your replies. The mistake I was making is that I thought that the value of KeyName is being passed to the RECEIVING side in the example above. I don't know what I've been smoking yesterday :) Of course the retrieval of the key/value pair happens on the CALLING side, therefore there is no way I can check for the KeyName existance inside the Session property on the RECEIVING side. That pretty much answers my original question. Please feel free to comment/add.