2011-08-03 39 views
0

嘿,我想下面的代碼從這個網頁ASP.NET錯誤與會話密鑰字符串

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

轉換爲VB.NET和有方法轉換爲這樣的

' 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(name As String)As Object':匹配參數'name'的參數從'Double'縮小到'String'。

回答

1

+運算可用於字符串連接以及所述&運算符在VB.NET中,但符號是首選運算符。

可以肯定的是,您可以將它們切換爲&符號並查看錯誤是否仍然存在? ShoppingCarItemIDKeyBase是一個字符串,我不確定+運算符是否強制字符串轉換爲數字,因爲用戶可以選擇使用&。

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

的一個或這兩個變量是一個十進制類型,而不是一個整數:

  • ShoppingCartItemIDKeyBase
  • ShoppingCartOrderQuantityKeyBase
相關問題