【问题标题】:Why am I getting: 'System.Collections.IEnumerable' has no type parameters and so cannot have type arguments?为什么我得到:'System.Collections.IEnumerable' 没有类型参数,因此不能有类型参数?
【发布时间】:2016-05-04 15:08:48
【问题描述】:

这是我的简单用户名和密码登录,它能够将用户重定向到不同的“会员区”页面。

但是我得到一个:

BC32045:“System.Collections.IEnumerable”没有类型参数,因此不能有类型参数

我也不知道为什么。

有人可以帮忙吗?

谢谢,大卫。

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 IEnumerable(Of Cred) = New () {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

【问题讨论】:

  • 你从哪里得到它?
  • @TimSchmelter 在这一行: Private ReadOnly _credentials As IEnumerable(Of Cred) = New () {New Cred("userone", "passwordone"), New Cred("usertwo", "passwordtwo" ), New Cred("userthree", "passwordthree", "/admin/custom.aspx")}
  • 您可能在某处导入 System.Collections 而不是 System.Collections.Generic。
  • 错误很明显,因为@Craig 表明IEnumerable&lt;T&gt; 驻留在命名空间System.Collections.Generic 中。您的编译器错误告诉您它找到了类型 System.Collections.IEnumerable,它没有类型参数。你错过了Imports System.Collections.Generic。
  • @CraigH 谢谢克雷格,但是所有这一切都只是我的头脑......关于如何重组我的代码以便它可以工作的任何想法?谢谢。大卫。

标签: asp.net vb.net passwords ienumerable credentials


【解决方案1】:

我得到正确的“类型预期”编译器错误。要么删除New(),所以替换

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

与

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

或使用New Cred(){...}

但是,您应该使用类而不是结构。对于Option Strict,您不能使用If user IsNot Nothing,因为Cred 是Structure,因此是值类型。

【讨论】:

  • 谢谢伙计,但是,我仍然收到相同的错误消息 Tim。任何想法为什么?非常感谢您的帮助!
  • @d.c:您是否尝试过重新打开 Visual Studio 并重新编译项目?编译器错误似乎与当前代码不正确。
  • 我使用 DreamWeaver,刷新后它只显示相同的错误消息。附:请不要因为我使用 DreamWeaver 而杀了我 :)
猜你喜欢
  • 1970-01-01
  • 2011-10-19
  • 2017-05-03
  • 2021-01-19
  • 2010-09-16
  • 1970-01-01
  • 2017-10-18
  • 2016-05-12
相关资源
最近更新 更多