471,318 Members | 3,212 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Test Array Upper Bounds

In VB6 I had a function that tested whether an array had any items in it as
follows

Public Function ArrayIsEmpty(TestVar As Variant) As Boolean
On Error Resume Next
Dim i As Long
i = UBound(TestVar, 1)
If Err.Number = 0 Then
ArrayIsEmpty = False
Else
ArrayIsEmpty = True
End If
End Function

I used a variant becaus I couldn't be sure what type of variable would be
passed to the routing (eg a string array, int array, object array etc etc.
How can I duplicate this functionality in Csharp?

Thanks

Alan
Dec 19 '05 #1
6 4735
Alan,

public bool ArrayIsEmpty(Array array)
{
// Return true if the array is null or there are
// no elements in it.
return (array == null || array.Length == 0);
}

As you can see, hardly worth declaring a method for. In VB6, I can
understand this, but in .NET, it's pretty much a non issue.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Alan Roberts" <al**@statistixl.co.uk> wrote in message
news:O1*************@TK2MSFTNGP12.phx.gbl...
In VB6 I had a function that tested whether an array had any items in it
as follows

Public Function ArrayIsEmpty(TestVar As Variant) As Boolean
On Error Resume Next
Dim i As Long
i = UBound(TestVar, 1)
If Err.Number = 0 Then
ArrayIsEmpty = False
Else
ArrayIsEmpty = True
End If
End Function

I used a variant becaus I couldn't be sure what type of variable would be
passed to the routing (eg a string array, int array, object array etc etc.
How can I duplicate this functionality in Csharp?

Thanks

Alan

Dec 19 '05 #2
What are you planning on using this for? you could always use if arr ==
null || arr.Length == 0 then array is empty.
Dec 19 '05 #3
Thanks guys. I guess I was looking for too hard an answer!!

Alan

"Alan Roberts" <al**@statistixl.co.uk> wrote in message
news:O1*************@TK2MSFTNGP12.phx.gbl...
In VB6 I had a function that tested whether an array had any items in it
as follows

Public Function ArrayIsEmpty(TestVar As Variant) As Boolean
On Error Resume Next
Dim i As Long
i = UBound(TestVar, 1)
If Err.Number = 0 Then
ArrayIsEmpty = False
Else
ArrayIsEmpty = True
End If
End Function

I used a variant becaus I couldn't be sure what type of variable would be
passed to the routing (eg a string array, int array, object array etc etc.
How can I duplicate this functionality in Csharp?

Thanks

Alan

Dec 19 '05 #4

"Alan Roberts" <al**@statistixl.co.uk> wrote in message news:O1*************@TK2MSFTNGP12.phx.gbl...
In VB6 I had a function that tested whether an array had any items in it as follows

Public Function ArrayIsEmpty(TestVar As Variant) As Boolean
On Error Resume Next
Dim i As Long
i = UBound(TestVar, 1)
If Err.Number = 0 Then
ArrayIsEmpty = False
Else
ArrayIsEmpty = True
End If
End Function

I used a variant becaus I couldn't be sure what type of variable would be passed to the routing
(eg a string array, int array, object array etc etc. How can I duplicate this functionality in
Csharp?


VB is not my thing so I'll skip answering the question.
I will point something out that probably is not relevant.

int[] intArray = new int[10];
intArray.Length is 10 and Yup it has 10 zeros in it.

MyClass[] myArray = new MyClass[10];
myArray.Length is 10 and it has 10 null refs in it.

You could argue that myArray is empty even though the Length is 10.

I don't know if this is a condition that you care about or not.
I only bring it up because it would fail the tests given so far.

Bill

Dec 20 '05 #5

"Bill Butler" <qw****@asdf.com> wrote in message
news:PlMpf.3489$Ap1.704@trndny06...

"Alan Roberts" <al**@statistixl.co.uk> wrote in message
news:O1*************@TK2MSFTNGP12.phx.gbl...
In VB6 I had a function that tested whether an array had any items in it
as follows

Public Function ArrayIsEmpty(TestVar As Variant) As Boolean
On Error Resume Next
Dim i As Long
i = UBound(TestVar, 1)
If Err.Number = 0 Then
ArrayIsEmpty = False
Else
ArrayIsEmpty = True
End If
End Function

I used a variant becaus I couldn't be sure what type of variable would be
passed to the routing (eg a string array, int array, object array etc
etc. How can I duplicate this functionality in Csharp?


VB is not my thing so I'll skip answering the question.
I will point something out that probably is not relevant.

int[] intArray = new int[10];
intArray.Length is 10 and Yup it has 10 zeros in it.

MyClass[] myArray = new MyClass[10];
myArray.Length is 10 and it has 10 null refs in it.

You could argue that myArray is empty even though the Length is 10.

I don't know if this is a condition that you care about or not.
I only bring it up because it would fail the tests given so far.

Bill


I would consider that a bug and not a valid case to test for.

- Michael S
Dec 20 '05 #6

"Michael S" <no@mail.com> wrote in message news:%2****************@TK2MSFTNGP12.phx.gbl...

"Bill Butler" <qw****@asdf.com> wrote in message news:PlMpf.3489$Ap1.704@trndny06...
<snip> MyClass[] myArray = new MyClass[10];
myArray.Length is 10 and it has 10 null refs in it.

You could argue that myArray is empty even though the Length is 10.

I don't know if this is a condition that you care about or not.
I only bring it up because it would fail the tests given so far.

Bill


I would consider that a bug and not a valid case to test for.


Personally, I avoid arrays with null refs as well, but that does not mean they are a BUG.
Here is a (contrived) example.

I have 10 slots to lease out for a flea market.
I create Vendor[] Vendors = new Vendor[10];

where each array location corresponds to a Physical slot.
As vendors sign up they are assigned to a particular slot
Vendors[slot] = new Vendor(blah);

To check if a slot is available just do
if (Vendors[slot] == null)

Now there might be prettier way to accomplish this, but this does work.
More importantly, it would not be unthinkable that code like this exists in MANY shops.

So, under some situations, it may be a valid case.
Granted, this is not the NORMAL use for arrays, but it is not inconceivable either.

Bill


Dec 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Mateusz [PEYN] Adamus | last post: by
8 posts views Thread by Paul in Toronto | last post: by
reply views Thread by rosydwin | 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.