473,386 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 4813
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: User | last post by:
Hi, This is very basic, It may be a repost, if so I'm sorry. The problem is that this declaration : Private strMyArray(100) As String will create an array of string with a length of 101,...
35
by: VK | last post by:
Whatever you wanted to know about it but always were affraid to ask. <http://www.geocities.com/schools_ring/ArrayAndHash.html>
3
by: Mateusz [PEYN] Adamus | last post by:
Hi. I have a problem. I'm creating a structure - looks like this: <c#> internal struct TwEnumeration { public ushort ItemType; public uint NumItems;
1
by: Samuel R. Neff | last post by:
Are there any differences between using Array.Length and Array.GetUpperBound(0) on a one-dimensional array? We have a team of developers and most people use Array.Length but one developer uses...
8
by: Paul in Toronto | last post by:
Got this assignment in my VB .NET class. The program's basically a picture viewer that lets you add your pictures to an array so you can cycle through them once the file's been opened. So you...
4
by: Amit Limaye | last post by:
This is wht i want to do typedef struct _notificationdataformat_ { char *m_OID; char **m_OIDList; f1 funcptr; } NOTIFICATIONDATAFORMAT; NOTIFICATIONFORMAT...
26
by: MLH | last post by:
How would I modify the following to achieve a 2-dimensional array? Dim MyWeek, MyDay MyWeek = Array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun") ' Return values assume lower bound set to 1...
3
by: Jerry West | last post by:
I'd like to get the upper bound index of an integer array. I've tried the following: Dim i as Integer Dim arrayIng() as Integer i = arrayIng.GetUpperBound This doesn't work. It seems...
152
by: vippstar | last post by:
The subject might be misleading. Regardless, is this code valid: #include <stdio.h> void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); } int main(void) { double array = { {...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.