|
Hello folks,
Happy New Year!!!
I have a class that has couple of properties.
Can I have a property in that class that can be a collection. Like say the MarketData. can I have a property say "FailedCurves" . It is a string property. But only thing is, this property is an ArrayList or Dictionary object that can have multiple string items in it.
How can I do that in C#? Am I thinking correct?
Thanks much
public class MarketData
{
#region "Private Data Members"
private int _countAdvanceCurve;
private int _countAdvancesSBCAgencyCurve;
private int _countAdvancesSBCAAACurve;
private int _countFhlbsfTreasuryCurve;
private int _countDNCOCurve;
#endregion "Private Data Members"
#region "Public Data Members"
public int CountAdvanceCurve
{
get { return _countAdvanceCurve; }
set { _countAdvanceCurve = value; }
}
}
|
|
|
|
|
You can have a property that is a List or Dictionary, no problem -- but you may want to make it read only.
What exactly is confusing you?
|
|
|
|
|
Yes you can have any sorts of collection in a properyt.
public ObservableCollection<int> CountAdvanceCurve
{
get { return _countAdvanceCurve; }
set { _countAdvanceCurve = value; }
}
or
public Dictionary<int,int> CountAdvanceCurve
{
get { return _countAdvanceCurve; }
set { _countAdvanceCurve = value; }
}
and so on.
|
|
|
|
|
Yes you can.
When you create a property, you can return anything you want - including objects you create as required.
For example, if you want to return only those values which are below 1000:
public Dictionary<string, int> LowValues
{
get
{
Dictionary<string, int> dict = new Dictionary<string, int>();
if (_countAdvanceCurve < 1000) dict.Add("AdvanceCurve", _countAdvanceCurve);
if (_countAdvancesSBCAgencyCurve < 1000) dict.Add("AdvancesSBCAgencyCurve", _countAdvancesSBCAgencyCurve);
if (_countAdvancesSBCAAACurve < 1000) dict.Add("AdvancesSBCAAACurve", _countAdvancesSBCAAACurve);
if (_countFhlbsfTreasuryCurve < 1000) dict.Add("FhlbsfTreasuryCurve", _countFhlbsfTreasuryCurve);
if (_countDNCOCurve < 1000) dict.Add("DNCOCurve", _countDNCOCurve);
return dict;
}
} This is one of the big advantages of Properties over fields - you do not have to store anything in a way that makes sense to the outside world. You can store in the best way for your class, and provide the outside world with the info it want, how it wants it, only when it asks for it.
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.
|
|
|
|
|
Thanks much. Best forum ever. I don't think I ever posted here...
This is the finished code.
private List<string> _curveswithnodata = new List<string>();
public List<string> Curveswithnodata
{
get { return _curveswithnodata; }
set { _curveswithnodata = value; }
}
public method()
{
if (basedonsomecondition)
{
this.Curveswithnodata.Add(CurveName);
}
}
|
|
|
|
|
It's a good idea to create a new List if you are going to return it to the outside world - remember that if you return a List, you actually return a reference to the list, so any changes made to it outside the class affect the version the class is using, but without you being notified. If you return a copy of a list then the outside world can do what it likes with the list without it affecting the one your class is using and potentially buggering up your data!
private List<string> _curveswithnodata = new List<string>();
public List<string> Curveswithnodata
{
get { return new List<string>(_curveswithnodata); }
set { _curveswithnodata = value; }
} The other solution is to return a read only version:
get { return _curveswithnodata.AsReadOnly());
But that returns a different type: ReadOnlyCollection<T> rather than List<T> which generally makes it harder to work with outside the class.
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.
|
|
|
|
|
I am building a webservice (not WCF). I need to store two values (name,value pairs).
Instead of using List<string>, I used Dictionary<string,int>. But it give me error message saying "Can't serialize the dictionary object.
Which collections object I can use (to be able to serialize) to store name value pair in a webservice .
Thanks
|
|
|
|
|
Dictionaries are not serializable via SOAP or XML serializers - but they are via DataContractSerializer[^]
The only other option is to use a List<KeyValuePair<string, yourtype>> which (frankly) is a PITA!
But, a serializable PITA!
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.
|
|
|
|
|
Griff,
Thanks. But you can't use this as a property of class. Can't have getters and setters.
nath
|
|
|
|