dr.pavan

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


Re: Visual C# General how to use arrays as properties for a class?

Evan H

public class Patient

{

private string[] _values;

public string[] Values

{

get { return _values; }

set { _values = value; }

}

}

you can do something like this:

Patient patient = new Patient();

patient.Values = new string[5];

patient.Values[0] = "test";

patient.Values[4] = "ing";

Console.WriteLine(patient.Values[4]);

I would recommend reading up on the difference between reference and value types. A lot of people tend to be confused about how the get/set works until they understand the difference.

http://www.awprofessional.com/articles/article.asp p=25322&seqNum=10&rl=1

You might think of using a List instead of an array..






Re: Visual C# General how to use arrays as properties for a class?

Manivannan.D.Sekaran

Instead of stick with the static array you can use the Dictonary object...

Code Snippet

public class Patient

{

private System.Collections.Generic.Dictionary<String, String> _LabValues;

public Patient()

{

_LabValues = new System.Collections.Generic.Dictionary<string, string>();

}

public System.Collections.Generic.Dictionary<String, String> LabValues

{

get

{

return _LabValues;

}

set

{

_LabValues = value;

}

}

}

class Program

{

static void Main(string[] args)

{

Patient p = new Patient();

p.LabValues["Blood Group"] = "O+ive";

p.LabValues["Sugar Ratio"] = "20%";

p.LabValues["Salt Ratio"] = "30%";

p.LabValues["FAT"] = "60%";

}

}






Re: Visual C# General how to use arrays as properties for a class?

Manivannan.D.Sekaran

If you want to validate the key & value use the following design,

Code Snippet

public class LabValues

{

private Dictionary<String, String> _LabValues = new Dictionary<string,string>();

public String this[string key]

{

set

{

//Set validtion for your value & key

_LabValues[key] = value;

}

get

{

//Set validtion for your value & key

return _LabValues[key];

}

}

}

public class Patient

{

private LabValues _LabValues;

public Patient()

{

_LabValues = new LabValues();

}

public LabValues LabValues

{

get

{

return _LabValues;

}

set

{

_LabValues = value;

}

}

}