zero element array creation

Z

Zytan

I want to make a zero element array. I know that Nothing is not the
same as a zero element array, since I can't get the length of, or
iterate through, an array = Nothing. I could make a zero element
array like this, but it seems like overkill:

Dim emptyArray as Byte() = System.Text.Encoding.Default.GetBytes("")

Is there a better way?

Zytan
 
S

Stephany Young

Dim emptyArray() as Byte = New Byte() {}
Console.Writeline(emptyArray.Length)

or

Dim emptyArray as Byte() = New Byte() {}
Console.Writeline(emptyArray.Length)

I don't know why you can write it both ways. There will be an esoteric
reason but I just don't know what it is.

Both forms DO compile to identical IL.

My own preference is for the 2nd form.
 
Z

Zytan

Dim emptyArray as Byte() = New Byte() {}

Right, of course.
I don't know why you can write it both ways. There will be an esoteric
reason but I just don't know what it is.

I'd like to know why, as well.
Both forms DO compile to identical IL.
Ok.

My own preference is for the 2nd form.

Mine, too.

Thanks again!!

Zytan
 
S

Stephany Young

Don't get me wrong Zytan - I have no inclination to find out why - In that
case I'm happy enough to accept that it just is.

There aren't enough hours in the day to chase down the reasons behind all
the foibles that one comes across.

I'm prepared to accept that the writers of VB.NET let a 'foible' exist
because:

a. they had a momentary aberration
b. it was early on a monday morning/late of friday afternoon
c. there is a really good reason for doing so
d. all of the above

Whether or not I like a particular 'foible' is a different matter but I'm
not going to lose any sleep over it.

That said, the various 'foible's do certanly make for som lively discussions
in here. :)
 
H

Herfried K. Wagner [MVP]

Zytan said:
I want to make a zero element array. I know that Nothing is not the
same as a zero element array, since I can't get the length of, or
iterate through, an array = Nothing. I could make a zero element
array like this, but it seems like overkill:

\\\
Dim a(-1) As Integer
///
 
C

Cor Ligthert [MVP]

Zytan,

Or I misunderstood it, or we are coming now on really problems which will
never exist in a real world.

\\\
Dim a(-1) As Integer
///
(I take Herfrieds sample because this shows it in my idea the nicest.)

Therefore all was it alone for me, what can be the use of this?

Cor
 
H

Herfried K. Wagner [MVP]

Cor,

Cor Ligthert said:
Or I misunderstood it, or we are coming now on really problems which will
never exist in a real world.

\\\
Dim a(-1) As Integer
///
(I take Herfrieds sample because this shows it in my idea the nicest.)

Therefore all was it alone for me, what can be the use of this?

Imagine a method like 'Directory.GetFiles' would be called on an empty
directory. Then I'd expect a zero-length array to be returned instead of
'Nothing'.
 
T

Tom Shelton

Zytan,

Or I misunderstood it, or we are coming now on really problems which will
never exist in a real world.

\\\
Dim a(-1) As Integer
///
(I take Herfrieds sample because this shows it in my idea the nicest.)

Therefore all was it alone for me, what can be the use of this?

Cor

It's sort of a variation of the null object pattern. If a method returns an
array, it's often nice to not have to check for a null reference (nothing)...

for each i as integer in getintegerarray ()
' do cool stuff with integer
next i

Instead of:

dim a() as integer = getintegerarray ()
if not a is nothing then
foreach i as integer in a
' do cool stuff with integer
next i
end if
 
D

Doug Glancy

Herfried,

That's what done before in VBA and was just trying to figure it out the
other day in .Net. Thanks for the answer. So simple once you know.

Doug
 
C

Cor Ligthert [MVP]

Tom/Herfried,

I don't see it clear, however I get the idea where you guys want to use it.

Thanks,

Cor
 
Z

Zytan

Don't get me wrong Zytan - I have no inclination to find out why - In that
case I'm happy enough to accept that it just is.

Well, at least now I know they are the same, and I can flow with the
concept that () means an array of whatever the type is, regardless of
where it is placed.
There aren't enough hours in the day to chase down the reasons behind all
the foibles that one comes across.

Yes, it impedes my progress, but I can't help but ask. And sometimes
an answer finds a better alternative, and that's how we progress.
I'm prepared to accept that the writers of VB.NET let a 'foible' exist
because:

a. they had a momentary aberration
b. it was early on a monday morning/late of friday afternoon
c. there is a really good reason for doing so
d. all of the above

I would think it is most likely:

e. backwards compatibility

So, far almost all the messes I've seen with C++ and VB .NET is due to
backwards compatibility. I can understand why it is important to
maintain it.
That said, the various 'foible's do certanly make for som lively discussions
in here. :)

Well, I hope that's a good thing!

Zytan
 
Z

Zytan

\\\
Dim a(-1) As Integer
///

Ok, right, thanks. I ran across this recently, actually. Now why
didn't I use this. I must have asked the question before this, or
forgot about it. So, there are these possibilities:

Dim zero1(-1) As Integer
Dim zero2() As Integer = {}
Dim zero3 As Integer() = {}

Zytan
 
Z

Zytan

Or I misunderstood it, or we are coming now on really problems which will
never exist in a real world.
...
Therefore all was it alone for me, what can be the use of this?

I want an array that will be 0 in size when there are 0 'answers'. If
there are more 'answers', then it will be an array of the answers.
The array will be used in a loop. If I used Nothing, instead, the
loop would fail, and I'd need more code to check for that. So, I'd
rather use an empty array.

Cor, I don't follow. Are you saying you don't know of any real reason
there would ever be an empty array? Perhaps there's a better way of
handling the above, with more exotic language contructs, but for now,
being a beginner, I can't see any better (simple!) way of doing this
other than using an empty array.
\\\
Dim a(-1) As Integer
///
(I take Herfrieds sample because this shows it in my idea the nicest.)

Actually, VB .NET's attempt to ease code porting from VB6 by declaring
arrays using the highest index, rather than the array size like every
other language, is, personally, the worst thing I've even seen in the
language. The reasoning behind is interesting but very unconvincing:

http://www.thecodingmonkey.net/2006/03/24/YouHaveToUnderstandTheHistory.aspx

Having said that, I'd rather using " = {}" than "a(-1)", since an
empty set is { }. -1 holds little meaning, it can only be understood
by logical extension of what the maximum index means.

But, by using "a(-1)", it does remind me of this language quirk. I
think perhaps explicitly showing the array bounds from min to max may
be best practive, to indicate precisely what is going on:

Dim tenElements(0 To 9) As Integer

But, it doesn't quite look as good for 0 length ararys:

Dim zeroElements(0 To -1) As Integer

So, I'm not sure. I think, personally, I like { } best.

Zytan
 
C

Cor Ligthert [MVP]

Zytan,

You wrote my answer always yourself.
Actually, VB .NET's attempt to ease code porting from VB6 by declaring
arrays using the highest index, rather than the array size like every
other language, is, personally, the worst thing I've even seen in the
language. The reasoning behind is interesting but very unconvincing:
Two answer, with the above I don't agree with you.
And before you understand it wrong, I have had much discussion with a very
longtime C# guy on this board. In my opinion VB was using the 1 as starting
indexer in fact the way it should be, the figure for first is 1. Numbers are
represented by a characterset from 0 to 9. However oldies like me are used
by the 0 as first indexer although it is in my eyes in fact completely
foolish. Before somebody start to write why it is a zero. I know that.

The other point an array is in fact in Net only usefull for fixed arrays.
For the rest the IList and Icollection implementing classes are much better
including the generic ones. The array has everytime to be redimmed.
Therefore I did not see the reason to use of a fixed array with a lenght of
real 0.

Cor
 
S

Stephany Young

Unfortunately, that blog only gives part of the history.

One hears a lot of people ask "Why isn't BASIC/VB more like C/C++?".

This would be a fair question if BASIC was "invented" after C. But it was
the other way around and therfore the more appropriate question should be
"Why isn't C/C++ more like BASIC/VB?".

Before anyone launches in, that, of course, is purely rhetorical, but does
help to put things in perspective.

As for zero or 1 based indexing, we Homo Sapiens do not, intuitively start
count counting from zero. You don't hear someone say "I've made my zeroth
million.". Likewise there was no year zero between BCE and CE which is also
why the year 2000 was in the 20th century and is not in the 21st century.

Certainly computers count from zero quite happily because all bits off just
happens to be zero. Once it was determined that indexing from zero was
actually a very good idea the Option Base was introduced to BASIC to allow
it without destroying existing code, but this still predates VB.

Another aspect to consider is that, unlike a lot of other languages, BASIC
is NOT controlled by a commitee. COBOL is controlled by CODASYL, C++ is
controlled by ANSI, etc. Those languages MUST, at least, comply with the
ratified standard, and anything else is a compiler-specific extension. What
VB/VB.NET does (or doesn't do) is purely up to Microsoft.

Certainly, Option Base is no longer supported in VB.NET (2005), but arrays
are still 'sized' by supplying the value for the upper-bound, the same way
it has been since before C was invented.

If you had a nice white fence and your neighbour painted his cacky yellow,
I'm sure that you would be more than a bit miffed if people started saying
that you should paint your's cacky yellow just because he did.


Actually, VB .NET's attempt to ease code porting from VB6 by declaring
arrays using the highest index, rather than the array size like every
other language, is, personally, the worst thing I've even seen in the
language. The reasoning behind is interesting but very unconvincing:

http://www.thecodingmonkey.net/2006/03/24/YouHaveToUnderstandTheHistory.aspx
</snip>
 
T

Tom Leylan

Stephany Young said:
"Why isn't C/C++ more like BASIC/VB?".

Because BASIC wasn't intended to be used to write operating systems. Why
isn't a Mercedes-Benz more like the Ford Model-T since the Ford came first?
Before anyone launches in, that, of course, is purely rhetorical, but does
help to put things in perspective.

It's an odd perspective that suggests that modern things should be more like
the things cavemen used because they were first. Why aren't MP3s more like
33 1/3 LPs, can't they make them scratch and warp?
As for zero or 1 based indexing, we Homo Sapiens do not, intuitively start
count counting from zero.

Homo sapiens do not calculate divisions accurate to 64- bits nor do run at
65 MPH. These are machines. They aren't supposed to emulate people, we
have people for that.
You don't hear someone say "I've made my zeroth million.".

I hear them say "my bad" so this should be the error message when the CPU
overflows? :)
Certainly, Option Base is no longer supported in VB.NET (2005), but arrays
are still 'sized' by supplying the value for the upper-bound, the same way
it has been since before C was invented.

The article points out (if I read it correctly) that the arrays are sized
one larger than expected. Expect a bank transfer error any day now and it
will be due to "oh yeah the array was one larger than we thought."
If you had a nice white fence and your neighbour painted his cacky yellow,
I'm sure that you would be more than a bit miffed if people started saying
that you should paint your's cacky yellow just because he did.

Yeah when your neighbor uses the new fire resistant paint it is important to
stick to your principals. He's going to have to pay when a fire at your
house burns his down anyway so who honestly cares as long as 1963 is the
basis of all future decisions? We don't need energy efficient light bulbs
(they're oddly shaped). If people want to flush their toilets directly into
the Thames I say "why not" they were doing it in 1830.

Tom :)
 
S

Stephany Young

Next time I'll staet clearly that my question isn't rehtorical and then
nobody will reply :)
 
C

Cor Ligthert [MVP]

I see I wrote a complete unreadable message.

Starting part of it was:
You wrote my answer always yourself.
Were I did mean:.

"You wrote the answer almost yourself".

And more of that stuff, Stephany gave however an excelent correction from
the first part so I keep it with that.

Cor
 
C

Cor Ligthert [MVP]

Tom,
If people want to flush their toilets directly into the Thames I say "why
not" they were doing it in 1830.
Because there are now much more people than in 1830 surrounding the Thames.

Assuming you mean with the Thames that River witch goes through the Thames
Valley to the North Sea/Canal and not a River in the USA.

I assume that the problem about this is much less in most places in the
country were you and Stephany live than were I live. (And that is global
wise seen beside that Thames). With this you show well, that problems have
forever to be seen in its own context and own and current environment.

Cor
 

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