UBound conversion from vb.net to c#

S

sunshine

I'm having trouble converting this FOR loop from VB.NET to C#:

Here's the VB.NET code:

Dim localCart As Object
localCart = Session("cart")

For i = 0 To UBound(localCart, 2)
If CStr(localCart(CARTID, i)) <> "" Then
orderTotal = orderTotal + (CDbl(localCart(CARTPPRICE, i)) *
CDbl(localCart(CARTPQUANTITY, i)))
'do something
End If
Next

I used a VB.NET to C# converter called Instrant C# and this is what I
got:
object localCart;
localCart = Session["cart"];
int tempFor1 = localCart.GetUpperBound(1);
for (i = 0; i <= tempFor1; i++)
{
if (System.Convert.ToString(localCart(CARTID, i)) != "")
{
orderTotal = orderTotal +
(System.Convert.ToDouble(localCart(CARTPPRICE, i)) *
System.Convert.ToDouble(localCart(CARTPQUANTITY, i)));
//do something
}
}

And here's the error message:
Compiler Error Message: CS0117: 'object' does not contain a
definition for 'GetUpperBound'

Any solutions please!
 
G

Guest

This is by design - we don't try to resolve the type inference issues that
the VB compiler addresses when variables are declared as 'Object'. In
general, more manual adjustments will be required when Option Strict is not
set in the original VB code.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter & VB to C++ converter
Instant J#: VB to J# converter
 
G

Guest

Hi sunshine,
from looking at your example the Session("cart") is storing 2D array of
doubles. I am not a VB.Net guru but UBound seems to be automatically casting
the localCart object variable to a System.Array type.

However C# is less forgiving and you normally need to be more explicit
that when using Vb.Net (which is why a lot of people like it). In the C#
conversion you are trying to access the GetUpperBound method on a variable
which is of type object, and object dos not have this method, you will need
to first cast the object to a 2D array of doubles like:

object localCart;
localCart = Session["cart"];
int tempFor1 = localCart.GetUpperBound(1);

should be:
double[,] localCart = (double[,])Session["Cart"];
int tempFor1 = localCart.GetUpperBound(1);


Hope that helps
Mark Dawson
http://www.markdawson.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top