Vb.net代码语法错误

时间:2011-12-31 06:03:44

标签: vb.net

我有以下vb.net代码,我收到语法错误

更新

 Protected Sub OpenLogin_Click(ByVal src As Object, ByVal e As CommandEventArgs)
        Dim StrUri As String = e.CommandArgument.ToString()
        Dim openid As New OpenIdRelyingParty()
        Dim b = New UriBuilder(Request.Url)
        With Key
            .Query = ""
        End With
        'var b = new UriBuilder(Request.Url) { Query = "" };

        Dim req = openid.CreateRequest(StrUri)

        Dim fetchRequest = New FetchRequest()
        fetchRequest.Attributes.AddRequired(WellKnownAttributes.Contact.Email)
        fetchRequest.Attributes.AddRequired(WellKnownAttributes.Name.First)
        fetchRequest.Attributes.AddRequired(WellKnownAttributes.Name.Last)

        req.AddExtension(fetchRequest)

        req.RedirectToProvider()

    End Sub

现在代码中的错误是“未声明密钥”我现在该怎么做

3 个答案:

答案 0 :(得分:0)

AFAIK,你不需要大括号。

With Something
    .Property1 = True
    .Property2 = "Inactive"
    ' And so on 
End With

<强>更新

With Key 
  .Query = ""
End With

为什么你在这里需要一个With子句?以上在语法上等同于

Key.Query = ""

答案 1 :(得分:0)

两个问题:

  1. 仅在使用Key标识用于分组的多部分密钥时才使用Enumerable.GroupBy保留字。使用构造函数内联设置对象属性不是必需的。

  2. 您遇到的问题是将With装饰器分隔到新行,这在语法上是不正确的,因为您现在将其视为With块,这意味着每一行点访问将紧跟With语句后的变量。您需要使用行继续符或将With放在与对象构造函数相同的行上:

  3. Ex 1

    Dim b = New UriBuilder(Request.Url) With {
                .Query = ""
            }
    

    Ex 2

    Dim b = New UriBuilder(Request.Url) _
                With { .Query = "" }
    

    修改

    您不能将此语法与Visual Studio 2005 / .NET 2.0项目一起使用。只需构造对象然后初始化属性:

    Dim b As New UriBuilder(Request.Url)
    b.Query = ""
    

答案 2 :(得分:0)

声明Key变量,或者从代码中删除它。