0

我有一个 3 个单选按钮来控制几个文本框的状态,我为所有 3 个按钮使用一个事件,但是控制文本框的部分似乎可以流线更...必须有更好的方法处理这个事件然后我有什么

Private Sub rdo_SkipBitLockerY_CheckedChanged(sender As RadioButton, e As EventArgs) Handles rdo_SkipBitLockerY.CheckedChanged, rdo_SkipBitLockerN.CheckedChanged, rdo_SkipBitLockerU.CheckedChanged
    Select Case sender.Name
        Case "rdo_SkipBitLockerY"
            txt_BDEDriveLetter.Enabled = True
            txt_BDEDriveSize.Enabled = True
            txt_BDEInstall.Enabled = True
            txt_BDEInstallSuppress.Enabled = True
            txt_BDERecoveryKey.Enabled = True
            txt_TPMOwnerPassword.Enabled = True
            txt_OSDBitLockerStartupKeyDrive.Enabled = True
            txt_OSDBitLockerWaitForEncryption.Enabled = True
            AttributeRDOTable("SkipBitLocker") = "YES"
        Case "rdo_SkipBitLockerN"
            txt_BDEDriveLetter.Enabled = False
            txt_BDEDriveSize.Enabled = False
            txt_BDEInstall.Enabled = False
            txt_BDEInstallSuppress.Enabled = False
            txt_BDERecoveryKey.Enabled = False
            txt_TPMOwnerPassword.Enabled = False
            txt_OSDBitLockerStartupKeyDrive.Enabled = False
            txt_OSDBitLockerWaitForEncryption.Enabled = False
            AttributeRDOTable("SkipBitLocker") = "NO"
        Case Else
            txt_BDEDriveLetter.Enabled = False
            txt_BDEDriveSize.Enabled = False
            txt_BDEInstall.Enabled = False
            txt_BDEInstallSuppress.Enabled = False
            txt_BDERecoveryKey.Enabled = False
            txt_TPMOwnerPassword.Enabled = False
            txt_OSDBitLockerStartupKeyDrive.Enabled = False
            txt_OSDBitLockerWaitForEncryption.Enabled = False
            AttributeRDOTable.Remove("SkipBitLocker")
    End Select

我能做些什么来减少重复性?

4

1 回答 1

0

抱歉,我不了解 .Net 平台,我只是尝试像在 Java 中那样做,可能你会发现它很有用。

  • 您可以为您的参数使用一些默认值。假设您的默认值

txt_BDEDriveLetter.Enabled

txt_BDEDriveSize.Enabled

txt_BDEInstall.Enabled

txt_BDEInstallSuppress.Enabled

txt_BDERecoveryKey.Enabled

txt_TPMOwnerPassword.Enabled

txt_OSDBitLockerStartupKeyDrive

txt_OSDBitLockerWaitForEncryption

假的

  • 每个案例操作应包含不超过 1-2 条语句。这就是为什么如果你想遵守这条规则,你应该只进行提取方法重构。

  • 不要向方法发送比它需要知道的更多的信息。

您的方法不需要了解发件人对象。只需发送发件人对象的名称。

我将重构您的代码如下(对不起,我不知道.Net,我会尝试编写每个示例):

Private Sub rdo_SkipBitLockerY_CheckedChanged(Name As String, e As EventArgs) Handles rdo_SkipBitLockerY.CheckedChanged, rdo_SkipBitLockerN.CheckedChanged, rdo_SkipBitLockerU.CheckedChanged
    Select Case Name
        Case "rdo_SkipBitLockerY"
            doRdoSkipBitLockerY();
        Case "rdo_SkipBitLockerN"
            doRdoSkipBitLockerN()
        Case Else
            doElse();
    End Select

Private void doRdoSkipBitLockerY()
    txt_BDEDriveLetter.Enabled = True
    txt_BDEDriveSize.Enabled = True
    txt_BDEInstall.Enabled = True
    txt_BDEInstallSuppress.Enabled = True
    txt_BDERecoveryKey.Enabled = True
    txt_TPMOwnerPassword.Enabled = True
    txt_OSDBitLockerStartupKeyDrive.Enabled = True
    txt_OSDBitLockerWaitForEncryption.Enabled = True
    AttributeRDOTable("SkipBitLocker") = "YES"
Private void doRdoSkipBitLockerN()
    AttributeRDOTable("SkipBitLocker") = "NO"
Private void doElse()
    AttributeRDOTable.Remove("SkipBitLocker");

如果我想让它成为 OOP 方式(在 Java 中),我会将所有 3 个案例提取为枚举成员,并将其作为参数发送给一个通用方法 setVariables()

enum MY_CASES{
private boolean txt_BDEDriveLetter;
private boolean txt_BDEDriveSize;
...
MY_CASES(boolean txt_BDEDriveLetter, boolean txt_BDEDriveSize , ..){
this.txt_BDEDriveLetter = txt_BDEDriveLetter;
this.txt_BDEDriveSize = txt_BDEDriveSize;
... 
}

CASE1(true, true, ...), CASE2(false, false, ...), CASE3(false, false, ...), ...
}

private Sub rdo_SkipBitLockerY_CheckedChanged(Name String, e EventArgs)
    setVariables(MY_CASES.valueOf(NAME));

private void setVariables(MY_CASES case){
    this.txt_BDEDriveLetter = case.getTxt_BDEDriveLetter;
    this.txt_BDEDriveSize   = case.getTxt_BDEDriveSize;
    ...
}

此版本允许您添加其他案例,而无需重构您的 rdo_SkipBitLockerY_CheckedChanged 方法。

于 2013-04-10T11:57:02.967 回答