U/LBound & copying arrays

S

Saga

Hello all, I am migrating a VB6 module to VB 2008. This module makes
use of the UBound and LBound functions to determine array bounds. I
left them as is and they worked fine in VB2008. I am aware that VB'08
has the GetUpperBounds() method, but I can't seem to find if there are
any downsides to leaving the U/LBound() functions as is or if I should
change these to the newer method. Is there any advantage/disadvantage
to this?

Also, since the arrays are large, the VB6 code is using the Windows
API CopyMemory to transfer data quickly. I found that in VB2008
(and some previous .NET versions, I assume) I can do this:

Buffer.BlockCopy(Source, SrcOff, Dest, DestOff, Length)

This works. Is this the best and fastest method to transfer data
from one large array to another (arrays can be 100K in size).

I also see that some use the following:

Array.Copy(srcAr, destAr, Length)

Any advantage of one method over the other? In my case, I
sometimes have to copy an array into an offset of the other, for
example something equivalent to the following:

SomeData is array previously dimensioned and containing data

ReDim Array2(UBound(SomeData) + 2)

For iIdx = 2 To Array2.GetUpperBound(0)
Array2(iIdx) = SomeData(iIdx - 2)
Next iIdx

I do this with Buffer.BlockCopy:

Buffer.BlockCopy(SomeData, 0, Array2, 2, UBound(SomeData))

I am not sure I can do this with Array.Copy. Thanks all! Saga
 
F

Family Tree Mike

Hello all, I am migrating a VB6 module to VB 2008. This module makes
use of the UBound and LBound functions to determine array bounds. I
left them as is and they worked fine in VB2008. I am aware that VB'08
has the GetUpperBounds() method, but I can't seem to find if there are
any downsides to leaving the U/LBound() functions as is or if I should
change these to the newer method. Is there any advantage/disadvantage
to this?

Also, since the arrays are large, the VB6 code is using the Windows
API CopyMemory to transfer data quickly. I found that in VB2008
(and some previous .NET versions, I assume) I can do this:

Buffer.BlockCopy(Source, SrcOff, Dest, DestOff, Length)

This works. Is this the best and fastest method to transfer data
from one large array to another (arrays can be 100K in size).

I also see that some use the following:

Array.Copy(srcAr, destAr, Length)

Any advantage of one method over the other? In my case, I
sometimes have to copy an array into an offset of the other, for
example something equivalent to the following:

SomeData is array previously dimensioned and containing data

ReDim Array2(UBound(SomeData) + 2)

For iIdx = 2 To Array2.GetUpperBound(0)
Array2(iIdx) = SomeData(iIdx - 2)
Next iIdx

I do this with Buffer.BlockCopy:

Buffer.BlockCopy(SomeData, 0, Array2, 2, UBound(SomeData))

I am not sure I can do this with Array.Copy. Thanks all! Saga

Well, first there is no practical use for LBound as that would always
return zero.

If the number of dimensions in the array is only one, I tend to use
Array.Length. That seems to self document the code better.

Personally, I would opt for a .Net function rather than
Microsoft.VisualBasic.* in all cases. When I look back to converted
code, I consider myself having been lazy if I left it as such. It also
gets you into the habit of doing things the framework way.
 
F

Family Tree Mike

Well, first there is no practical use for LBound as that would always
return zero.

If the number of dimensions in the array is only one, I tend to use
Array.Length. That seems to self document the code better.

Personally, I would opt for a .Net function rather than
Microsoft.VisualBasic.* in all cases. When I look back to converted
code, I consider myself having been lazy if I left it as such. It also
gets you into the habit of doing things the framework way.

Sorry, I neglected the Array.Copy versus Buffer.BlockCopy question.

If using BlockCopy within the same array, it does not observe
overlapping memory well. Therefore it can overwrite some memory before
it is copied.
 
C

Cor Ligthert[MVP]

I've never used the UBound and the LBound.

I was aware of it when there were more VB6 diehards migrating.

I always use the Count and the Length.
(And for me in combination with the zero indexer so always in the way of -1)

However, for some those bound properties looks for me sometimes like a
religion.
 
S

Saga

Thank you both for your replies. A few additional notes:

"Well, first there is no practical use for LBound as that would always
return zero."

Point well taken. Of course! If .NET arrays always start at 0, we
don't need the "genius" of LBound to tell us that the lower bound
of an array is 0.

"Personally, I would opt for a .Net function rather than
Microsoft.VisualBasic.* in all cases. When I look back to converted
code, I consider myself having been lazy if I left it as such."

You might call it "lazy", but the powers that be here would call
it "efficient". I would call it inappropriate. If I am using VB2008
as a dev. platform I want to use VB2008 conventions, not
conventions from another language.

Going back to Microsoft.VisualBasic.*, I had read somewhere
that it was Microsoft.VisualBasic.Compatibility that I should
stay away from. Am I missing a big picture here?

"However, for some those bound properties looks for me
sometimes like a religion."

Agreed. I certainly did not come from VB6 to VB2008 to keep
"programming" in VB6. I now have 1 month experience using
VB2008 and perhaps the most difficult aspect for me (so far) has
been determining which syntax is VB2008 and which one comes
from a previous version (such as UBound). The only function
that I need from the VB6 syntax is split(). The string object Split
method that VB2008 has only accepts single char delimiters
and the text data files that this program will be reading use the
double pipe char (||) as a delimiter.

Again, thanks both! Saga
 
C

Cor Ligthert[MVP]

The framework contains more namespaces, two of those are System.Net and
Microsoft.VisualBasic namespaces.

Limiting you only to System.Net makes no sense of all. Take that what is for
you the best. The Microsoft.VisualBasic namespace has very useful methods
like the conversion and casting ones but also many others. By instance the
Instr is the fastest search method for strings.

Microsoft.VisualBasicCompatibility (strange name) is not anymore supported
in FrameWork 4 as was written already in FrameWork . It was a compatibility
framework to VB6 which is as development language not anymore supported.

Be aware that VB6 has probably more in common with VB10 then with VB1
although some persons sometimes want you to believe something else.
 
M

Michel Posseth [MCP]

"Well, first there is no practical use for LBound as that would always
return zero."

Point well taken. Of course! If .NET arrays always start at 0, we
don't need the "genius" of LBound to tell us that the lower bound
of an array is 0.

Wrong ....

By default .Net arrays have a zero based index (SZArrays or vectors )
however .Net also supports arrays with a different lower index
although these should be marked as non CLS-compliant.

array.Upperbound and array.Lowerbound return the number of elements along a
given dimension
unlike the LBound and UBound functions the dimension number is zero based ,
not one based

HTH

Michel Posseth
 
M

Michel Posseth [MCP]

The only function
that I need from the VB6 syntax is split(). The string object Split
method that VB2008 has only accepts single char delimiters
and the text data files that this program will be reading use the
double pipe char (||) as a delimiter.


text = System.Text.RegularExpressions.Regex.Split(test,
System.Text.RegularExressions.Regex.Escape("||"))

HTH

Michel
 
M

Michel Posseth [MCP]

In addition to this answer of Cor,

The only valid reasson i ever found to abandon the VB namespace completely
was when i discovered that it wasn`t suported on the X-Box 360
( no this is not a joke :) ) You can write software in .Net for the X-Box
360 and you can even do it in VB but then you have to abandon the VB
namespace

In all other situations the VB namespace is part of the default framework
and thus you have no extra dependancy or whatever it is there if you use it
or not
so why not just use it`s handy framework shortcuts ?

HTH

Michel
 
S

Saga

Point well taken. Of course! If .NET arrays always start at 0, we
Wrong ....

By default .Net arrays have a zero based index (SZArrays or vectors )
however .Net also supports arrays with a different lower index
although these should be marked as non CLS-compliant.
First, thanks for the reply and correction. I am over my head on this,
but when would I want/need to use non CLS-complaint code? My
first instinct would be to, whenever possible, be CLS complaint.
Saga
 
M

Michel Posseth [MCP]

Saga said:
First, thanks for the reply and correction. I am over my head on this,
but when would I want/need to use non CLS-complaint code? My
first instinct would be to, whenever possible, be CLS complaint.
Saga

Answer to the question is ,, "when you have not made the decission" what if
you need to work with data coming from other ( non .Net ) sources or just
sloppy coders on the other side ( belive i have been there ) :)


HTH

Michel
 
S

Saga

Michel Posseth said:
Answer to the question is ,, "when you have not made the decission" what
if you need to work with data coming from other ( non .Net ) sources or
just sloppy coders on the other side ( belive i have been there ) :)
Thanks for following up this thread. You mention "data coming from other
( non .Net ) sources". For example, what sources would be in this category?

I ask because at this time, my first VB2008/NET project consists of
developing/
migrating an application that will monitor a folder looking for text and gz
files. I
need to read and import the text files and decompress the gz files into text
files
and then proceed with processing, hence my initial question of how to move
large quantities of data using arrays. Since this is my first .NET project
I am
being ovewhemled by information (and loving it!)

I have some doubts about writing to binary files, but that is another
thread:)
Regards! Saga
 
C

Cor Ligthert[MVP]

In my opinion you would in the time where video is a normal data type not
worry anymore about text and a zipped files.

Those are relative to those so small that thinking in optimizing that is a
waste of times

But use for those as well not anymore arrays, the arraylist is the least you
can do, but generic classes is even a better way for those.
 
M

Mark Hurd

Cor Ligthert said:
I've never used the UBound and the LBound.

I was aware of it when there were more VB6 diehards migrating.

I always use the Count and the Length.
(And for me in combination with the zero indexer so always in the way
of -1)

However, for some those bound properties looks for me sometimes like a
religion.

Yeah, I continue to use UBound to avoid the .Length - 1 expression.
 
S

Saga

As usual, thank you for your response!

Cor Ligthert said:
In my opinion you would in the time where video is a normal data type not
worry anymore about text and a zipped files.
My apologies Cor, but I did not understand the above statement. Are you
saying that with the power and huge capacity that PCs have nowadays I
should not worry about compressing files?

If so, I should add that my app is a monitor that imports files placed by
other systems. In the case of the compressed file, this comes from a
handheld
terminal that has only a small fraction of the power and capacity of a
typical
PC, so they compress the file. Even if it came from a super computer, I
have no control over the data source, I just have to read and import
whatever
file, in whatever format (within reason) is placed in the poll location.

Sidenote: I can't imagine why the data terminal people would choose the
gz standard over ZIP. The data terminals, AFAIK, run with Windows CE
something or other, so in my humble ranting, I believed that ZIP was a more
common standard in the Windows world. My two conclusions were that either
the people that programmed the terminals came from a Unix background or
they purposely used gz to avoid any patent issues.
 
M

Michel Posseth [MCP]

VB6 for instance :)


Saga said:
Thanks for following up this thread. You mention "data coming from other
( non .Net ) sources". For example, what sources would be in this
category?

I ask because at this time, my first VB2008/NET project consists of
developing/
migrating an application that will monitor a folder looking for text and
gz files. I
need to read and import the text files and decompress the gz files into
text files
and then proceed with processing, hence my initial question of how to move
large quantities of data using arrays. Since this is my first .NET project
I am
being ovewhemled by information (and loving it!)

I have some doubts about writing to binary files, but that is another
thread:)
Regards! Saga
 

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