473,327 Members | 2,074 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

UBound confusion

JR
I may have posted a blank post accidentally. Sorry

I am coming over to VB.NET from C++/C# and am confused about the UBound
function.

Example code:

Dim i As Integer

Dim myarray(6) As Integer

i = UBound(myarray)

I would expect that myarray is of length 6 and that the array
subscripts run from 0 to 5. The doc. for UBound says:

Returns the highest available subscript for the indicated dimension of
an array.

It also says:

Since array subscripts start at 0, the length of a dimension is greater
by one than the highest available subscript for that dimension.

So...I would expect the value of i above to be 5 but instead it is 6.

What's up??

-jr

Nov 21 '05 #1
4 7836
JR wrote:
I may have posted a blank post accidentally. Sorry

I am coming over to VB.NET from C++/C# and am confused about the UBound
function.

Example code:

Dim i As Integer

Dim myarray(6) As Integer

i = UBound(myarray)

I would expect that myarray is of length 6 and that the array
subscripts run from 0 to 5. The doc. for UBound says:

Returns the highest available subscript for the indicated dimension of
an array.

It also says:

Since array subscripts start at 0, the length of a dimension is greater
by one than the highest available subscript for that dimension.

So...I would expect the value of i above to be 5 but instead it is 6.

What's up??

-jr


UBound is a careover function from VB6. Use myarray.GetUpperBound(0)
instead.

myarray(6) makes objects for 0 ... 6. not "6" objects.

Chris
Nov 21 '05 #2
JR,

Probably for compatible reasons is decided that in VBNet an array where is
written

dim a(6) as string contains 7 items.

Therefore when I need 6 items I use
dim a(5) as string

For the rest see the message from Chris.

I never use ubound or words like that.

a.lenght - 1 gives very nice the last item.

Luckily is AFAIK this the only place.

Cor
Nov 21 '05 #3

JR wrote:
I may have posted a blank post accidentally. Sorry

I am coming over to VB.NET from C++/C# and am confused about the UBound
function.
Actually...

Example code:

Dim i As Integer

Dim myarray(6) As Integer

i = UBound(myarray)

I would expect that myarray is of length 6 and that the array
subscripts run from 0 to 5.


.... the confusion lies here. While a C(etc) array defined as [6] does
indeed have 6 elements, indexed 0 1 2 3 4 5, a VB.NET array defined (6)
in fact has *seven* elements, indexed 0 1 2 3 4 5 6.

The reasons for this are 'historical' :) VB6 allowed arrays to have any
range of subscripts, using the To keyword: eg Dim a(-3 To 7) As
Integer, but the base index whtn you didn't use To was always 1. Or 0.
Depending on stuff. Anyway, the key point is that in Basic when you
declare an array (6) then there is an element (6).

I won't argue here about the relative merits of the C and Basic
approaches - as a C programmer you will have to bear the differences in
mind.

--
Larry Lard
Replies to group please

Nov 21 '05 #4
"JR" <xa****@charter.net> schrieb:
I am coming over to VB.NET from C++/C# and am confused about the UBound
function.

Example code:

Dim i As Integer

Dim myarray(6) As Integer


The line above creates an array with 7 elements (indices 0 through 6). 6 is
the upper bound ('UBound').

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jason | last post by:
I have a number of arrays that are populated with database values. I need to determine which array has the highest ubound out of all the arrays. The array size will always change based on the...
2
by: Rick Brown | last post by:
I get a "subscript out of range" error when I run this code. "Rcount = rsSupplierCode.RecordCount" Returns 49 BUT "numReturned = UBound(varRecords, 2) + 1" Returns only 3 I'm I wrong to...
3
by: Andi Twine | last post by:
Hi all, I really hope someone here can help ! I have designed and built an ASP.NET web service with Visual Studio .NET. The web service outputs some dummy XML data when its called with some...
4
by: Cindy Meister -WordMVP- | last post by:
Hi all Generally, it seems Option Strict ON is recommended practice? How, then, does one reconcile -- Option Strict On Public MyArray(6) Single 'Some Code here
2
by: Starbuck | last post by:
Hi All In one of my converted (VB6 to VB.Net) apps I have managed to remove all instances of - Imports Microsoft.VisualBasic - except for one form which raises errors in regard to LBound and...
3
by: Eugene Anthony | last post by:
<% if UBOUND(Arr) > 0 then%> <% end if %> I am getting the following error: Error Type: Microsoft VBScript runtime (0x800A000D) Type mismatch: 'UBOUND'
2
by: sunshine | last post by:
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...
4
by: Mark | last post by:
What is the consensus about using the Ubound function? Should it be avoided altogether? Is there an arry size where Ubound becomes less efficent? Also, is there a better way to increase the...
2
by: Adam Honek | last post by:
I have the following code below. The thing is, even if txtCCEmailAddress.Text is empty (no text), CCRecipeant will return a Ubound() higher than 0 and still go in the loop. If there's nothing...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.