如何在vb.net的子例程中声明可选文本框 [英] How to declare an optional textbox in a sub routine in vb.net
问题描述
亲爱的朋友
我想使用一些可选控件(如文本框和组合框)创建一个子例程.我的示例子例程如下;
私有 子 ActivateControls(rd As RadioButton,可选 txt As TextBox)
结束 子
但是,当我编写此子例程时,Vb.Net IDE给我的错误是每个可选参数都必须指定一个默认值.因此,在这里我无法理解文本框的默认值是什么.
如果有人可以帮助我,我将不胜感激.
由
Rashid Farooq
确实需要初始化可选参数,以便将其设置为Nothing.
私有 子 ActivateControls(rd As RadioButton,可选 txt As TextBox = 什么都没有) 结束 子
VB可选参数 [ 这是我的子程序.
私有 子 ActivateControls(rd As RadioButton,可选 txt As TextBox = Nothing ,txt2 As TextBox = Nothing ) 结束 子
当我在程序中以
形式调用此子例程时ActivateControls(rdbtn,txt)
它给了我以下错误
对象引用未设置为对象的实例"
但是当我将此子例程编写为
时ActivateControls(rdbtn,txt,txt2)
正确.
所以,请告诉我该问题的解决方案.
或
除了可以设置为可选控件(如文本框,组合框)的默认值之外,还有其他任何值
Dear Friends
I want to make a sub routine with some optional controls like Textbox and combobox. My sample sub routine is as under;
Private Sub ActivateControls(rd As RadioButton, Optional txt As TextBox) End Sub
But when I wrote this sub routine Vb.Net IDE gives me the error that each Optional Parameter must specify a default value. So, here I cant understand that what can be the default value of a textbox.
If any body can help me, I will be grateful.
By
Rashid Farooq解决方案Optional arguments do need to be initialized so you could set it to Nothing.
Private Sub ActivateControls(rd As RadioButton, Optional txt As TextBox = Nothing) End Sub
VB Optional Arguments[^]
Thanks for Answering Brother.
Your answer does work.
But when I make a the subroutine with a default value of Nothing and when I run the program it runs correctly only if i gave all the arguments including optional values. If i miss only one optional argument it gives the error that Object Reference not set to an instance of object.
Here is my subroutine.
Private Sub ActivateControls(rd As RadioButton, Optional txt As TextBox = Nothing, txt2 As TextBox = Nothing) End Sub
When I called this subroutine in my program as
ActivateControls(rdbtn,txt)
It gives me the following error
"Object Reference not set to an Instance of an Object"
But when I write this subroutine as
ActivateControls(rdbtn,txt,txt2)
It works right.
So, Please tell me what is the solution of this problem.
OR
Is there any other value Except Nothing That I can set as Default value of Optional controls(like textbox, combobox)
这篇关于如何在vb.net的子例程中声明可选文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!