我怎么做到这一点? [英] How do I get this right?

查看:106
本文介绍了我怎么做到这一点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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?

推荐答案

Hello Salisu Shaibu,



特别注意这两行:



Hello Salisu Shaibu,

Note this two lines particularly:

public void ClearControls(TextBox[] A)





...和...





...and...

ClearControls(txtNAME,txtSEX,txtAGE,txtSCHOOL);





看似不对劲?



JAFC



Something seems wrong?

JAFC


你已声明 ClearControls 以数组作为参数,但您传递了4个单独的 TextBox 项目。

将声明更改为:

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



以便将参数收集到一个数组自动。



当然,如果你用超过4个 TextBox 参数调用它,那么索引到 x 将失败超出范围。


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.


其他解决方案可以满足您的需求。但你想要的并不是很好。据我所知,您想要初始化一些带有一些值的文本框。我建议你一个接一个地做。设置每个文本框文本的初始值,如下所示。



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.

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





更好,为初始值声明一些常量字符串,并在reset方法中使用这些字符串。因此,如果您需要根据其初始值检查控件,则可以使用该常量。



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.


这篇关于我怎么做到这一点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆