Accessing properties as an array
-
Is it possible to have a propety as an array? If so, how do I access it?? I usually create my properties in this manner
class Object{
private string sProperty;public string Property{
get{return sProperty;}
set{sProperty = value;}
}Can I do the following, and how do I properly access the array
class Object{
private string[] sProperty; // Dimension it elsewherepublic string[] Property{ // how do I access the array in the get/set methods?
get{return sProperty;}
set{sProperty = value;}
} -
Is it possible to have a propety as an array? If so, how do I access it?? I usually create my properties in this manner
class Object{
private string sProperty;public string Property{
get{return sProperty;}
set{sProperty = value;}
}Can I do the following, and how do I properly access the array
class Object{
private string[] sProperty; // Dimension it elsewherepublic string[] Property{ // how do I access the array in the get/set methods?
get{return sProperty;}
set{sProperty = value;}
}I don't know about string[], but if you use ArrayList like: private ArrayList alBtnCommands = new ArrayList(); public ArrayList ButtonCommands { get { return alBtnCommands; } set { alBtnCommands = value; } } then you can call it by: currentButton.ButtonCommands.Add(AnItem); currentButton.ButtonCommands.RemoveAt(i); // i(integer) is an index SomeThing = currentButton.ButtonCommands[i]; currentButton.ButtonCommands[i] = SomeThing; It's not what you asked for, but it might be similar for string[] Hope it helps Thomas
-
Is it possible to have a propety as an array? If so, how do I access it?? I usually create my properties in this manner
class Object{
private string sProperty;public string Property{
get{return sProperty;}
set{sProperty = value;}
}Can I do the following, and how do I properly access the array
class Object{
private string[] sProperty; // Dimension it elsewherepublic string[] Property{ // how do I access the array in the get/set methods?
get{return sProperty;}
set{sProperty = value;}
}Do it just like you wrote :
public string[] Property{ get{return sProperty;} set{sProperty = value;} }
and access it like soObject o = new Object(); o.Property = new String[] {"foo", "bar"}; Console.WriteLine("{0} {1}", o.Property[0], o.Property[1]);
another trick is to use default properties[^] write you property like thispublic string this[int index] { get { return sProperty[index]; } set { sProperty[index] = value; } }
and access like thisObject o = new Object(); o.Property = new String[2]; o[0] = "foo"; o[1] = "bar"; Console.WriteLine("{0} {1}", o[0], o[1]);
HTH
The smaller the mind the greater the conceit. Aesop
-
Do it just like you wrote :
public string[] Property{ get{return sProperty;} set{sProperty = value;} }
and access it like soObject o = new Object(); o.Property = new String[] {"foo", "bar"}; Console.WriteLine("{0} {1}", o.Property[0], o.Property[1]);
another trick is to use default properties[^] write you property like thispublic string this[int index] { get { return sProperty[index]; } set { sProperty[index] = value; } }
and access like thisObject o = new Object(); o.Property = new String[2]; o[0] = "foo"; o[1] = "bar"; Console.WriteLine("{0} {1}", o[0], o[1]);
HTH
The smaller the mind the greater the conceit. Aesop
The cop (FxCop) says do not return array as properties... Jonathan de Halleux.
www.dotnetwiki.org -
The cop (FxCop) says do not return array as properties... Jonathan de Halleux.
www.dotnetwiki.orgFunny, the Cop didnt seem to have a problem with the little snippet I compiled :confused: But if they're the rules fair enough. Cheers for the info.
The smaller the mind the greater the conceit. Aesop
-
I don't know about string[], but if you use ArrayList like: private ArrayList alBtnCommands = new ArrayList(); public ArrayList ButtonCommands { get { return alBtnCommands; } set { alBtnCommands = value; } } then you can call it by: currentButton.ButtonCommands.Add(AnItem); currentButton.ButtonCommands.RemoveAt(i); // i(integer) is an index SomeThing = currentButton.ButtonCommands[i]; currentButton.ButtonCommands[i] = SomeThing; It's not what you asked for, but it might be similar for string[] Hope it helps Thomas
Using an
IList
implementation like theArrayList
isn't always the answer. An array is fixed. Items can't be added or removed. This is often necessary in cases like returning the fonts installed on the system, or returning the printers installed on the system. In these cases, though, the property is read only (only implements theget
accessor). If it was a list, developers can easily modify it (without even instantiating a new one) which might corrupt the behavior of the object.Microsoft MVP, Visual C# My Articles
-
Do it just like you wrote :
public string[] Property{ get{return sProperty;} set{sProperty = value;} }
and access it like soObject o = new Object(); o.Property = new String[] {"foo", "bar"}; Console.WriteLine("{0} {1}", o.Property[0], o.Property[1]);
another trick is to use default properties[^] write you property like thispublic string this[int index] { get { return sProperty[index]; } set { sProperty[index] = value; } }
and access like thisObject o = new Object(); o.Property = new String[2]; o[0] = "foo"; o[1] = "bar"; Console.WriteLine("{0} {1}", o[0], o[1]);
HTH
The smaller the mind the greater the conceit. Aesop
-
Thanx - this is exactly what I needed. I only need it because I'm converting some VB code and it does it this way. What is the FxCop mentioned?
http://www.gotdotnet.com/team/fxcop/ [^] Check it out, it helps you write better code.
The smaller the mind the greater the conceit. Aesop