ASP.NET Error with session key strings
Hey I am trying to convert the following code to VB.NET from this webpage
http://msdn.microsoft.com/en-us/magazine/cc163730.aspx
And have a method converted as such
' 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(ShoppingCartI开发者_运维百科temIDKeyBase + i))
quantity = CInt(session(ShoppingCartOrderQuantityKeyBase + i))
Dim item As ShoppingCartItem = ShoppingCartItem.GetItem(ID)
Return New ShoppingCartOrder(item, quantity)
End Function
But getting the error around the lines
ID = CInt(session(ShoppingCartItemIDKeyBase + i))
quantity = CInt(session(ShoppingCartOrderQuantityKeyBase + i))
Error 1 Overload resolution failed because no accessible 'Item' can be called without a narrowing conversion: 'Public Default Property Item(index As Integer) As Object': Argument matching parameter 'index' narrows from 'Double' to 'Integer'. 'Public Default Property Item(name As String) As Object': Argument matching parameter 'name' narrows from 'Double' to 'String'.
The + operator can be used for string concatenation as well as the & operator in VB.NET, but the ampersand is the preferred operator.
Just to be sure, can you switch them to ampersands and see if the error still occurs? ShoppingCarItemIDKeyBase is a string, and I'm not sure if the + operator forces the strings to convert to numbers since the user has the option of using &.
ID = CInt(session(ShoppingCartItemIDKeyBase & i))
quantity = CInt(session(ShoppingCartOrderQuantityKeyBase & i))
One or both of these variables is a decimal type rather than an integer:
- ShoppingCartItemIDKeyBase
- ShoppingCartOrderQuantityKeyBase
精彩评论