How to access a generic property argument in the following example:

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; }

}



Answer this question

How to access a generic property argument in the following example:

  • SQLDataMonkey

    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.



  • sailorsurprise

    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.



  • ccrockatt

    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.


  • Jon Watte

    Hi,

    The signature is:   public bool ContainsKey (TKey key)


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



  • fods

    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).
  • Zadoras

    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.



  • How to access a generic property argument in the following example: