Click here to Skip to main content
15,894,646 members
Please Sign up or sign in to vote.
2.14/5 (3 votes)
See more:
The method below is to assign elements of x to some textboxes.

        string[] x = new string[] { "NAME", "SEX", "AGE", "SCHOOL"};
        public void ClearControls(TextBox[] A)
        {
            for (int32 i = 0; i < A.Length; i++)
            {
                A[i].Text = x[i];
            }
        }

The implementation of the method is as sampled below but error line would appear under the ClearControls with hint message: No overload for method ‘ClearControls’ takes 4 arguments.
        
        private void btnNext_Click(object sender, EventArgs e)
        {
            ClearControls(txtNAME,txtSEX,txtAGE,txtSCHOOL);

         }
What do I do?
Posted

Hello Salisu Shaibu,

Note this two lines particularly:

C#
public void ClearControls(TextBox[] A)


...and...

C#
ClearControls(txtNAME,txtSEX,txtAGE,txtSCHOOL);


Something seems wrong?

JAFC
 
Share this answer
 
You've declared ClearControls to take an array as the parameter but you are passing the 4 individual TextBox items.
Change the declaration to:
C#
public void ClearControls(params TextBox[] A)

so that the arguments are collected into an array automatically.

Of course, if you call this with more than 4 TextBox arguments then the indexing into x will fail as out of bounds.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-May-14 16:23pm    
Sure, a 5.
—SA
Matt T Heffron 5-May-14 16:48pm    
Thanks
Salisu Shaibu 5-May-14 17:06pm    
10Q man;
Perfect!!!
Salisu Shaibu 5-May-14 17:46pm    
I tried to use:
public void ClearControls(params TextBox[] A, params String[] B)
so that "NAME", "SEX", ... can be added on implementation but error line hints that "parameter array must be last parameter in a formal parameter list".
What am I supposed to do please?
Matt T Heffron 5-May-14 18:08pm    
That you can't do!
You can have ONLY ONE params parameter that automatically assembles the "tail" of the argument list into an array and it must be the last parameter.
For what you now appear to want you should just do all of the assignments explicitly as in Solution 3.
If you insist on passing the strings and controls to a method then you must build the array for either the strings or the controls and pass that as an array parameter AHEAD of the params parameter of the other type:
public void ClearControls(string[] values, params TextBox[] textBoxes)
used as:
string[] v = new string[] { "NAME", "SEX", "AGE", "SCHOOL"};
ClearControls(v, txtNAME,txtSEX,txtAGE,txtSCHOOL);
OR
public void ClearControls(TextBox[] textBoxes, params string[] values)
used as:
TextBox[] tb = new TextBox[] { txtNAME,txtSEX,txtAGE,txtSCHOOL };
ClearControls(tb, "NAME", "SEX", "AGE", "SCHOOL");
Other solutions do what you want. But what you want is not quite good. As far as I understand, you want to initialize some text boxes with some values. I advice you to do that one by one. set each textbox text with its initial value as in the following.

C#
void ResetControls() {
  this.txtNAME.Text="[NAME]";
  this.txtSEX.Text="[SEX]";
  // other initializations
}


better, declare some constant strings for initial values and use those strings in reset method. Therefore, if you need to check a control against its initial value, you can use that constant.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900