Mar 1, 2007

Parameterized Properties in C#

To create Parameterized Properties in C# one can only use a Indexer (the default parameterized property... this[]). However if we wanted to store an array of a particular type one could use the following which would be a parameterized property itself.:

public string[] CardsDescription
{
get
{
if (this.ViewState["CardsDescription"] == null)
{
this.ViewState["CardsDescription"] = new System.String[3];
return (string[])this.ViewState["CardsDescription"];
}
else
return (string[])this.ViewState["CardsDescription"];
}
set
{
this.ViewState["CardsDescription"] = value;
}
}


Thus when setting values you would use

int index = 0;
foreach(System.Data.DataRow dr in productDT.Rows)
{
int productID = int.Parse(dr["ProductID"].ToString());

this.CardsDescription[index] = dr["Description"].ToString();

index += 1;
}

kick it on DotNetKicks.com

No comments: