473,397 Members | 2,033 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,397 software developers and data experts.

If Clauses to Verify Array Subscript Validity

10
I have this array and Dim statement:


Dim curSales(1 to 5) as Currency


Using an If clause, I was wondering which one might verify that the array subscript called intX was valid? I realize that this has to be within the range to be valid such as 1 to 5.


If intX > 1 and intX < 5 Then
If intX >= 1 and intX <= 5 Then
If curSales(intX) >= 1 and curSales(intX) < 5 Then
If curSales(intX) >= 1 and curSales(intX) <= 5 Then
Feb 5 '08 #1
3 1548
Wagz
11
Dim curSales(1 to 5) As currency creates an array with indexes 1 through 5 inclusive. Therefore the valid indexes are 1,2,3,4,5 and the 2nd if statement you wrote is the one you'd want to use to verify that intX is valid.

Wagz
Feb 6 '08 #2
Killer42
8,435 Expert 8TB
Just as a personal preference, I like to use Select Case for range checking, simply because of the To operator.

Example
Expand|Select|Wrap|Line Numbers
  1. Select Case IntX
  2.   Case 1 To 5
  3.     ' Ok
  4.   Case Else
  5.     ' Not Ok
  6. End Select
The outcome is the same as your IF test, of course, I just feel it makes the code "self-documenting". Especially in cases where you need to check multiple ranges. (like 1-5, 6-10, etc.)

Example
Expand|Select|Wrap|Line Numbers
  1. Select Case ItemPrice
  2.   Case Is < 0
  3.     strDescription = "Program error - price CANNOT be negative."
  4.   Case 0
  5.     strDescription = "Free!!!"
  6.   Case 1 To 9
  7.     strDescription = "Less than ten dollars."
  8.   Case 10 To 19
  9.     strDescription = "Less than twenty dollars."
  10.   Case Else
  11.     strDescription = "Forget it, too expsensive."
  12. End Select
Feb 6 '08 #3
QVeen72
1,445 Expert 1GB
Hi,

Yes, you can check with :

Expand|Select|Wrap|Line Numbers
  1. If intX >= 1 and intX <= 5 Then
  2. ' Valid
  3. Else
  4.   MsgBox "InValid.."
  5. End If
  6.  
You can Generalize it further, so in future, if you cange the Range of array, you need not change the other code:

Expand|Select|Wrap|Line Numbers
  1. If intX >= LBound(cuRSales) and intX <= UBound(curSales) Then
  2. ' Valid
  3. Else
  4.   MsgBox "InValid.."
  5. End If
  6.  

Regards
Veena
Feb 6 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
29
by: shmartonak | last post by:
For maximum portability what should the type of an array index be? Can any integer type be used safely? Or should I only use an unsigned type? Or what? If I'm using pointers to access array...
0
by: Mark Gibson | last post by:
Hi, I've been playing about with array's, and found the concat operator '||' quite useful, apart from the fact that prepending an element places it in a lower subscript. Is there a way of...
51
by: Pedro Graca | last post by:
I run into a strange warning (for me) today (I was trying to improve the score of the UVA #10018 Programming Challenge). $ gcc -W -Wall -std=c89 -pedantic -O2 10018-clc.c -o 10018-clc...
272
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
3
by: uche | last post by:
Please give me some feed back on this issue: Here is the complier error: hexdmp.cpp: In function `void output(unsigned char, int, bool&)': hexdmp.cpp:133: error: invalid types `unsigned char'...
9
by: AM | last post by:
Hi, I have a C++ Dll that has a function that is being exported as shown below extern "C" __declspec(dllexport) validationResult __stdcall _validateData(double dataToMat, int time); A...
13
by: Ivan | last post by:
Hi, What is the best syntax to use a char to index into an array. /////////////////////////////////// For example int data; data = 1;
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
0
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...
0
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...
0
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...

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.