473,763 Members | 3,855 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

zero element array creation

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.Enc oding.Default.G etBytes("")

Is there a better way?

Zytan

Feb 19 '07 #1
33 8190
Dim emptyArray() as Byte = New Byte() {}
Console.Writeli ne(emptyArray.L ength)

or

Dim emptyArray as Byte() = New Byte() {}
Console.Writeli ne(emptyArray.L ength)

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.
"Zytan" <zy**********@y ahoo.comwrote in message
news:11******** **************@ p10g2000cwp.goo glegroups.com.. .
>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.Enc oding.Default.G etBytes("")

Is there a better way?

Zytan
Feb 19 '07 #2
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

Feb 19 '07 #3
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. :)
"Zytan" <zy**********@y ahoo.comwrote in message
news:11******** **************@ v33g2000cwv.goo glegroups.com.. .
> 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
Feb 19 '07 #4
"Zytan" <zy**********@y ahoo.comschrieb :
>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
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Feb 19 '07 #5
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
"Zytan" <zy**********@y ahoo.comschreef in bericht
news:11******** **************@ p10g2000cwp.goo glegroups.com.. .
>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.Enc oding.Default.G etBytes("")

Is there a better way?

Zytan

Feb 19 '07 #6
Cor,

"Cor Ligthert [MVP]" <no************ @planet.nlschri eb:
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.GetF iles' would be called on an empty
directory. Then I'd expect a zero-length array to be returned instead of
'Nothing'.

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

Feb 19 '07 #7
On 2007-02-19, Cor Ligthert [MVP] <no************ @planet.nlwrote :
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

--
Tom Shelton
Feb 20 '07 #8
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

"Herfried K. Wagner [MVP]" <hi************ ***@gmx.atwrote in message
news:%2******** *******@TK2MSFT NGP05.phx.gbl.. .
Cor,

"Cor Ligthert [MVP]" <no************ @planet.nlschri eb:
>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.GetF iles' would be called on an empty
directory. Then I'd expect a zero-length array to be returned instead of
'Nothing'.

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

Feb 20 '07 #9
Tom/Herfried,

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

Thanks,

Cor

"Tom Shelton" <to*********@co mcastXXXXXXX.ne tschreef in bericht
news:l7******** *************** *******@comcast .com...
On 2007-02-19, Cor Ligthert [MVP] <no************ @planet.nlwrote :
>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

--
Tom Shelton

Feb 20 '07 #10

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

Similar topics

31
5019
by: RS | last post by:
Hi, Looking to see if the following construct is valid: typedef struct { int foo; char bar; } foobar; Basically, the idea is to have the structure above point to a message buffer that has a 4-byte integer followed by a stream of variable
1
20559
by: None | last post by:
Dynamic array creation Hi all... here's a good one for you... I have a situation where I have some bean, and I need to populate an array field in it... here's the problem... I do not know the element type of the array before runtime. Now, I've been planning on using Commons Beanutils, since the rest of this particular app does (it's doing a lot of introspection of the
19
7083
by: nileshsimaria | last post by:
Hi, I have seen some code were we have array with zero elements (mostly within structure) like, typedef struct foo { int data }foo;
0
9566
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9389
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10149
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9943
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9828
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7370
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3918
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2797
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.