我为什么要获得表达式预期?

时间:2016-01-27 17:39:17

标签: asp.net vb.net expression structure username

这是我用于用户名和密码登录表单的简单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)

非常感谢。

大卫。

1 个答案:

答案 0 :(得分:1)

问题是您对structure class使用Cred。 请注意,结构是值类型,类是引用类型。

所以:

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

总是返回一个结构(当没有找到任何结构时,结构的成员会得到它们的默认值)。

您无法将结构与Nothing进行比较,因为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