将变量从形式解析为函数

本·布莱克摩尔

我有一个小应用程序,它由一个表单组成,该表单获取当前登录用户的用户名,并具有一个输入文本框和一个提交给函数的按钮。我希望将用户名和文本框输入(6个数字)解析为通过单击按钮调用的函数。我目前可以使用以下代码来获取文本框输入以进行解析。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Input As Integer
Input = Integer.Parse(Textbox1.Text)
<Do stuff>
End Sub

但我似乎无法通过用户名。以我的形式

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim currentADUser As System.DirectoryServices.AccountManagement.UserPrincipal
currentADUser = System.DirectoryServices.AccountManagement.UserPrincipal.Current
Dim LogonName As String = currentADUser.SamAccountName
End Sub

并且我尝试使用以下方法解析该函数​​:

Dim LogonName As String
LogonName = String.Parse(LogonName.Text)

但这与以下错误:

“解析”不是“字符串”的成员。

如何将表单中的变量解析为函数?我正在使用Visual Studio 2012。

the_lotus

目前尚不清楚您要做什么。这是我的意见。

只需添加逻辑即可将用户名添加到按钮单击事件中

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim Input As Integer
    Input = Integer.Parse(Textbox1.Text)

    Dim currentADUser As System.DirectoryServices.AccountManagement.UserPrincipal
    currentADUser = System.DirectoryServices.AccountManagement.UserPrincipal.Current
    Dim LogonName As String = currentADUser.SamAccountName

    <Do stuff>
End Sub

如果在其他地方使用相同的逻辑,请创建一个函数

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim Input As Integer
    Input = Integer.Parse(Textbox1.Text)
    Dim LogonName As String = GetLogonName()

    <Do stuff>
End Sub

Private Function GetLogonName() As String
    Dim currentADUser As System.DirectoryServices.AccountManagement.UserPrincipal
    currentADUser = System.DirectoryServices.AccountManagement.UserPrincipal.Current
    Return currentADUser.SamAccountName
End Function

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章