471,350 Members | 1,805 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

UBound conversion from vb.net to c#

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!

*** Sent via Developersdex http://www.developersdex.com ***
Feb 2 '06 #1
2 11490
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

"sunshine" wrote:
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!

*** Sent via Developersdex http://www.developersdex.com ***

Feb 3 '06 #2
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


"sunshine" wrote:
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!

*** Sent via Developersdex http://www.developersdex.com ***

Feb 3 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Cindy Meister -WordMVP- | last post: by
3 posts views Thread by Brad Markisohn | last post: by
2 posts views Thread by Starbuck | last post: by
4 posts views Thread by JR | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.