Test Array Upper Bounds

A

Alan Roberts

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
B

Ben Dewey

What are you planning on using this for? you could always use if arr ==
null || arr.Length == 0 then array is empty.
 
B

Bill Butler

Alan Roberts said:
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
 
M

Michael S

Bill Butler said:
Alan Roberts said:
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
 
B

Bill Butler

Michael S said:
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
 

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