How to use arrays as properties of a class?
-
I am unable to figure out how to use the get-set style public properties to manipulate the private arrays of that class? for example public class Patient { private string[] m_labValues //this is what i am clueless about. //how do i get down to elements of this array? //i am sure that this implementation is wrong. public string LabValues { get { return m_labValues; } set { m_labValues = value; } } } from my main class i desire an ability to directly manipulate the elements...like: private Patient patient = new Patient(); patient.LabValues=new String[6]; patient.LabValues[0]="B.P is 120,80"; patient.LabValues[1]="Blood Sugar is 160" .. .. .. I'm totally clueless about implementing this. I understand that if i declare the m_LabValues itself as public, i will be able to manipulate it directly with out using the get-set style Can some one please help me?
dnahelix
-
I am unable to figure out how to use the get-set style public properties to manipulate the private arrays of that class? for example public class Patient { private string[] m_labValues //this is what i am clueless about. //how do i get down to elements of this array? //i am sure that this implementation is wrong. public string LabValues { get { return m_labValues; } set { m_labValues = value; } } } from my main class i desire an ability to directly manipulate the elements...like: private Patient patient = new Patient(); patient.LabValues=new String[6]; patient.LabValues[0]="B.P is 120,80"; patient.LabValues[1]="Blood Sugar is 160" .. .. .. I'm totally clueless about implementing this. I understand that if i declare the m_LabValues itself as public, i will be able to manipulate it directly with out using the get-set style Can some one please help me?
dnahelix
You need to give your class an "indexer."
public class Patient
{
private string[] m_labValues;public Patient(int labValueCount) { m\_labValues = new string\[labValueCount\]; for(int i = 0; i < m\_labValues.Length; i++) { m\_labValues\[i\] = string.Empty; } } public string this\[int index\] { get { return m\_labValues\[index\]; } set { m\_labValues\[index\] = value; } }
}
private Patient patient = new Patient(6);
patient[0] = "B.P is 120,80";
patient[1] = "Blood Sugar is 160";Error checking omitted for brevity's sake.
-
I am unable to figure out how to use the get-set style public properties to manipulate the private arrays of that class? for example public class Patient { private string[] m_labValues //this is what i am clueless about. //how do i get down to elements of this array? //i am sure that this implementation is wrong. public string LabValues { get { return m_labValues; } set { m_labValues = value; } } } from my main class i desire an ability to directly manipulate the elements...like: private Patient patient = new Patient(); patient.LabValues=new String[6]; patient.LabValues[0]="B.P is 120,80"; patient.LabValues[1]="Blood Sugar is 160" .. .. .. I'm totally clueless about implementing this. I understand that if i declare the m_LabValues itself as public, i will be able to manipulate it directly with out using the get-set style Can some one please help me?
dnahelix
You could simply do the following: public string[] LabValues { get { return m_labValues; } set { m_labValues = value; } } } This will work but is not considdered good coding. You should rather use a generic list or a public method to set the array. public void SetLabValues(string[] ar) { m_labValues=ar; }
-
I am unable to figure out how to use the get-set style public properties to manipulate the private arrays of that class? for example public class Patient { private string[] m_labValues //this is what i am clueless about. //how do i get down to elements of this array? //i am sure that this implementation is wrong. public string LabValues { get { return m_labValues; } set { m_labValues = value; } } } from my main class i desire an ability to directly manipulate the elements...like: private Patient patient = new Patient(); patient.LabValues=new String[6]; patient.LabValues[0]="B.P is 120,80"; patient.LabValues[1]="Blood Sugar is 160" .. .. .. I'm totally clueless about implementing this. I understand that if i declare the m_LabValues itself as public, i will be able to manipulate it directly with out using the get-set style Can some one please help me?
dnahelix
try to read on "Indexers" in c# leslie sanford gave an example of it also
-
try to read on "Indexers" in c# leslie sanford gave an example of it also
-
I am unable to figure out how to use the get-set style public properties to manipulate the private arrays of that class? for example public class Patient { private string[] m_labValues //this is what i am clueless about. //how do i get down to elements of this array? //i am sure that this implementation is wrong. public string LabValues { get { return m_labValues; } set { m_labValues = value; } } } from my main class i desire an ability to directly manipulate the elements...like: private Patient patient = new Patient(); patient.LabValues=new String[6]; patient.LabValues[0]="B.P is 120,80"; patient.LabValues[1]="Blood Sugar is 160" .. .. .. I'm totally clueless about implementing this. I understand that if i declare the m_LabValues itself as public, i will be able to manipulate it directly with out using the get-set style Can some one please help me?
dnahelix
Hi,
dnahelix wrote:
public string LabValues { get { return m_labValues; } set { m_labValues = value; } }
If you want the array m_labValues as a property, you must include the [] in its type: public string[] LabValues {...} That way your statements patient.LabValues[0]="B.P is 120,80"; patient.LabValues[1]="Blood Sugar is 160"; are OK If there is only one string array to expose (make publicly available) you could work with an indexor, as others already told you; but for more than one array, that becomes difficult since an indexor always uses the class name (in the ouside world) and this (within the class intself), so the only way to differentiate is by the type of the index variable, which does not have to be an int at all (it could be a string, or whatever you like), but would be an int if it really stands for an index in an array. :)
Luc Pattyn [My Articles] [Forum Guidelines]