开发者

IDictonary<string,IParameter>

I am IDictionary<string, IParameter> QueryParameters and trying to assign values to it, however i am failed.

Sample code:

public IDictionary<string, IParameter> QueryParameters 
{ 
  get 开发者_如何学JAVA
  { 
     return new Dictionary<string, IParameter>(); 
  } 
}

Please can someone provide some example or let me know how to assign values to IParameter.


The problem is that you return a new instance everytime. Try:

private readonly IDictionary<string, IParameter> m_QueryParameters = new Dictionary<string, IParameter>(); 

public IDictionary<string, IParameter> QueryParameters 
{ 
  get { return m_QueryParameters; } 
}

Now you can use it:

QueryParameters.Add( "key", new Parameter() );


You are returning a new IDictionary<string, IParameter> instance each time the QueryParameters property is called.

You should probably keep this instance in the containing class:

class Foo
{
  private IDictionary<string, IParameter> dict =
    new Dictionary<string, IParameter>();

  public IDictionary<string, IParameter> QueryParameters 
  { 
    get
    {
      return dict;
    } 
  }
}

and then use it as follows:

void SomeFunc
{
   Foo f = new Foo();
   f.QueryParameters.Add("name", new Parameter());
   f.QueryParameters.Add("something else", new Parameter());
}


I am not certain what you are asking as you don't assign to IParameter which is a type. What you do is create a variable that has a type of IParameter and assign to that. Yo be more complete I assume that IParameter is an interface and so you need a concrete class that extends IParameter and create an instance of that.

Thus somewhwre

class Parameter extends IParameter { ... }

Your code

IDictionary dict = obj. QueryParameters(); // get the dictionary from your method Parameter para = new Parameter();

dict.Add("test", para);

Also see dalle's answer for improvments to you class shown

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜