The code works fine when I store the state of 2 checkboxes, but when I try to store the state of 3 check boxes, I get the following error:
CS1729: 'CheckBoxState' does not contain a constructor that takes '3' arguments
Here's my code:
protected Dictionary
protected void grdOrders_Sorting(object sender, GridViewSortEventArgs e)
{
// Save checkbox states before sorting. State is saved here to maintain through sorting.
// If this is required through other events as well, may need to do this somewhere else
// (e.g. Page_Load for each post).
// Create new Dictionary object to hold checkbox states
this.checkBoxState = new Dictionary
// Loop through each row in the GridView...
foreach (GridViewRow row in grdOrders.Rows)
{
// Get references to the CheckBoxes
CheckBox chkDeny = (CheckBox)row.FindControl("chkDeny");
CheckBox chkApprove = (CheckBox)row.FindControl("chkApprove");
CheckBox chkNeither = (CheckBox)row.FindControl("chkNeither");
// The default state is both CheckBoxes unchecked, so only save state if one is checked
if (chkApprove.Checked || chkDeny.Checked || chkNeither.Checked)
{
// Get DataKey for reference. This will be used to retrieve the state later.
int key = Int32.Parse(grdOrders.DataKeys[row.RowIndex].Value.ToString());
// Save CheckBox state.
CheckBoxState state = new CheckBoxState(chkApprove.Checked, chkDeny.Checked, chkNeither.Checked);
this.checkBoxState.Add(key, state);
}
}
}
Here's the line that errors out:
CheckBoxState state = new CheckBoxState(chkApprove.Checked, chkDeny.Checked, chkNeither.Checked);
Thanks for any help!
mPosted Sep 3, 2008, 6:03 PM
I still don't really understand why it works though. Can anybody give me a quick and dirty explination of what get and set is doing? Possibly of what happens in my class when one of the check boxes is checked or unchecked?
What worked:
public class CheckBoxState
{
public CheckBoxState(bool chkApprove, bool chkDeny, bool chkNeither)
{
this._chkApprove = chkApprove;
this._chkDeny = chkDeny;
this._chkNeither = chkNeither;
}
private bool _chkApprove;
public bool chkApprove
{
get { return _chkApprove; }
set { _chkApprove = value; }
}
private bool _chkDeny;
public bool chkDeny
{
get { return _chkDeny; }
set { _chkDeny = value; }
}
private bool _chkNeither;
public bool chkNeither
{
get { return _chkNeither; }
set { _chkNeither = value; }
}
}
Ryan AlfordPosted Sep 3, 2008, 11:44 PM
public CheckBoxState(bool chkApprove, bool chkDeny, bool chkNeither)
{
this._chkApprove = chkApprove;
this._chkDeny = chkDeny;
this._chkNeither = chkNeither;
}
is called a class constructor. when you say "new CheckBoxState()", it must send three boolean values to the constructor.
the get/set really has nothing to do with your problem. your original problem was that you were trying to send three boolean values to the constructor when it only had a definition for two boolean values.
here is a good article on constructors...
http://www.c-sharpcorner.com/UploadFile/neerajsaluja/ConstructorsInCSharp11152005233222PM/ConstructorsInCSharp.aspx
mPosted Sep 3, 2008, 5:46 PM
public class CheckBoxState
{
public CheckBoxState(bool chkApprove, bool chkDeny)
{
this._chkApprove = chkApprove;
this._chkDeny = chkDeny;
}
private bool _chkApprove;
public bool chkApprove
{
get { return _chkApprove; }
set { _chkApprove = value; }
}
private bool _chkDeny;
public bool chkDeny
{
get { return _chkDeny; }
set { _chkDeny = value; }
}
}
Posted Sep 3, 2008, 5:32 PM
Your function prototype should be something like:
CheckBoxState(bool, bool, bool);
Ryan AlfordPosted Sep 3, 2008, 5:22 PM