Friday, March 16, 2012

Passing an Array as a Property

I need some help with the syntax of creating a property, as i don't understand them very well.
I would like to use the following in a property:

SqlParameter [] param = new SqlParameter[3];

param[0] = new SqlParameter("@dotnet.itags.org.attend", SqlDbType.NVarChar, 3);
param[0].Value = this.TextBox1.Text;
.
.
.
the textboxes are also in another class (webform), so i'm not sure how can i use them as well.
If anybody has any suggestions, i'd really appreciate it.The syntax to create a property in a class is as follows:

1) create a private variable in the class to hold and return the value
2) create the Get & Set accessors for the private variable described in 1

Example:

This will create a "Customer Name" property to a class:


private string _customerName;

public string CustomerName
{
get
{
return _customerName;
}
set
{
_customerName=value;
}
}

Now with the syntax out of the way.. your specific question .. try this.. I am doing this from memory but it should work:


private SqlParameter[] _parameters;

public SqlParameter[] Parameters
{
get
{
return _parameters;
}
set
{
_parameters = value;
}
}

Now.. to use the property, you could do this:


MyObject obj = new MyObject();
obj.Parameters = new SqlParameter[2];
obj.Parameters[0] = new SqlParameter("@.par1","some value");
obj.Parameters[1] = new SqlParameter("@.par2","some value");

Hope this helps,

0 comments:

Post a Comment