【问题标题】:ASP.NET Error with session key strings会话密钥字符串的 ASP.NET 错误
【发布时间】:2011-08-03 09:49:01
【问题描述】:

嘿,我正在尝试将此网页中的以下代码转换为 VB.NET

http://msdn.microsoft.com/en-us/magazine/cc163730.aspx

并且有一个这样转换的方法

' Get the order from the session on demand
Private Shared Function GetOrderFromSession(ByVal i As Integer) As ShoppingCartOrder
    Dim session As HttpSessionState = HttpContext.Current.Session

    Dim ID As Integer = 0
    Dim quantity As Integer = 0

    ' note: For simplicity session key strings are dynamically 
    ' created——for performance reasons they should be precreated.
ID = CInt(session(ShoppingCartItemIDKeyBase + i))
quantity = CInt(session(ShoppingCartOrderQuantityKeyBase + i))

    Dim item As ShoppingCartItem = ShoppingCartItem.GetItem(ID)
    Return New ShoppingCartOrder(item, quantity)
End Function

但是在线条周围出现错误

        ID = CInt(session(ShoppingCartItemIDKeyBase + i))
    quantity = CInt(session(ShoppingCartOrderQuantityKeyBase + i))

错误 1 ​​重载解析失败,因为没有缩小转换就无法调用可访问的“项目”: 'Public Default Property Item(index As Integer) As Object':参数匹配参数'index'从'Double'缩小到'Integer'。 “公共默认属性项(名称作为字符串)作为对象”:参数匹配参数“名称”从“双”缩小到“字符串”。

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    在 VB.NET 中,+ 运算符和 & 运算符可用于字符串连接,但 & 号是首选运算符。

    为了确定,你能不能把它们换成与号,看看错误是否仍然存在? ShoppingCarItemIDKeyBase 是一个字符串,我不确定 + 运算符是否强制字符串转换为数字,因为用户可以选择使用 &。

    ID = CInt(session(ShoppingCartItemIDKeyBase & i))
    quantity = CInt(session(ShoppingCartOrderQuantityKeyBase & i))
    

    【讨论】:

      【解决方案2】:

      这些变量中的一个或两个是十进制类型而不是整数:

      • ShoppingCartItemIDKeyBase
      • ShoppingCartOrderQuantityKeyBase

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-04
        • 2021-01-22
        • 2018-09-09
        • 1970-01-01
        • 2022-11-21
        相关资源
        最近更新 更多