2016-01-27 31 views
0

這是一個重定向至不同的「會員區」頁面的用戶名和密碼登錄表單我簡單的CodeFile VB代碼:爲什麼我要表達期待?

Public Class MyPage 
Inherits Page 
Private Structure Cred 
    Public Username As String 
    Public Password As String 
    Public RedirectUrl As String 
    Public Sub New(un As String, pw As String, Optional ru As String = "/admin/default.aspx") 
     Username = un 
     Password = pw 
     RedirectUrl = ru 
    End Sub 
End Structure 

Private ReadOnly _credentials As System.Collections.Generic.IEnumerable(Of Cred) = New Cred(){New Cred("userone", "passwordone"), New Cred("usertwo", "passwordtwo"), New Cred("userthree", "passwordthree", "/admin/custom.aspx")} 

Public Sub Page_Load(sender As Object, e As EventArgs) 
    Dim user = _credentials.SingleOrDefault(Function(x) x.Username = UserName.Text AndAlso x.Password = Password.Text) 
    If user IsNot Nothing Then 
     Session("Admin") = True 
     Response.Redirect(user.RedirectUrl) 
    Else 
     Session("Admin") = False 
     LtlLogin.Text = "<p>Sorry, you have provided incorrect login details.</p>" 
    End If 
End Sub 
End Class 

它的上線:

Dim user = _credentials.SingleOrDefault(Function(x) x.Username = UserName.Text AndAlso x.Password = Password.Text) 

非常感謝。

大衛。

+0

有關存儲密碼的明文強制性警告:不要 – Plutonix

+0

@Plutonix我知道,謝謝你,但我只是新的VB所以服用它慢:) –

+0

閱讀類和結構。 – shadow

回答

1

問題是您使用structure針對class針對Cred。 請注意,結構是值類型,類是引用類型。

所以:

Dim user = _credentials.SingleOrDefault(Function(x) x.Username = UserName.Text AndAlso x.Password = Password.Text) 

始終返回結構(沒事的時候發現,則結構的成員獲得其默認值)。

爲TI爲引用類型,你不能比較沒有的結構。

更改結構類,你會被罰款。

或更改與檢查:

If Not user.Equals(New Cred) Then 

檢查this

更新與實例

類中房

Imports System.Linq 

Module StartupModule 

    Private ReadOnly _credentials As System.Collections.Generic.IEnumerable(Of Cred) = New Cred() { 
     New Cred("userone", "passwordone"), 
     New Cred("usertwo", "passwordtwo"), 
     New Cred("userthree", "passwordthree", "/admin/custom.aspx")} 

    Sub Main() 
     Dim userName As String = "" 
     Dim password As String = "" 

     Dim crd = _credentials.Where(Function(x) x.Username = userName AndAlso x.Password = password).SingleOrDefault 

     If crd Is Nothing Then 
      Console.WriteLine("user is nothing") 
     Else 
      Console.WriteLine("user is something") 
     End If 


     Console.ReadLine() 
    End Sub 

    Private Class Cred 
     Public Username As String 
     Public Password As String 
     Public RedirectUrl As String 
     Public Sub New(un As String, pw As String, Optional ru As String = "/admin/default.aspx") 
      Username = un 
      Password = pw 
      RedirectUrl = ru 
     End Sub 
    End Class 


End Module 

結構中房

Imports System.Linq 

Module StartupModule 

    Private ReadOnly _credentials As System.Collections.Generic.IEnumerable(Of Cred) = New Cred() { 
     New Cred("userone", "passwordone"), 
     New Cred("usertwo", "passwordtwo"), 
     New Cred("userthree", "passwordthree", "/admin/custom.aspx")} 

    Sub Main() 
     Dim userName As String = "" 
     Dim password As String = "" 

     Dim crd = _credentials.Where(Function(x) x.Username = userName AndAlso x.Password = password).SingleOrDefault 

     If crd.Equals(New Cred) Then 
      Console.WriteLine("user is nothing") 
     Else 
      Console.WriteLine("user is something") 
     End If 


     Console.ReadLine() 
    End Sub 

    Private Structure Cred 
     Public Username As String 
     Public Password As String 
     Public RedirectUrl As String 
     Public Sub New(un As String, pw As String, Optional ru As String = "/admin/default.aspx") 
      Username = un 
      Password = pw 
      RedirectUrl = ru 
     End Sub 
    End Structure 


End Module 
+0

對不起,我是新手,所以如果我將'Structure'更改爲'Class'並在該行之後添加新的代碼行,它將起作用嗎?感謝您的幫助。 –

+0

我相信,如果您將'Structure Cred'更改爲'Class Cred',則您已準備就緒。我想'UserName.Text'是一個文本框或東西。 – shadow

+0

不得不從'end structure'中更改'end class',但我仍然在該行上獲得Expression Expected。任何想法這裏有什麼問題。是的Username.Text和Password.Text是asp:TextBox。謝謝! –

相關問題