12,354,086 members (71,423 online)
alternative version

48.2K views
15 bookmarked
Posted

# A set of universal data check functions

, 24 Dec 2002
 Rate this:
A set of universal data check functions can be used in any systems with checking requirement.
<!-- Add the rest of your HTML here -->

## Introduction

We need to check the range of user data in many program. It is a hell for us to verify all data for a program with several thousands parameters. The best way is to check it automatically. In order to make it running , the trivial thing is how to write check routings in uniform way. I got it. Just a simple tips in C++. I provide the source code for every body. In later articles , I will give you a special control for using these routines in universal way.

## Checking theory

The basic theory to check the range of data is very simple but trivial. In mathematical, the range of data is domain. In a 1D condition , a domain has one or two limits, one for large boundary and one for small boundary . Usually , we write it as [min,max] for close domain or (min , max ) as open domain. Our check routines shall include all kinds conditions we may encountered.

I list the following condition:

```1) Let it be , no any check !
2) given number must be less than the limit.                    number < min
3) given number must be great than the limit.                    number > max
4) given number must be within a range                        min < number < max
5) given number must be less or equal to limit                    number <= min
6) given number must be great or equal to limit                    number >= max
7) given number must be within a range with lower equal                    min<= number < max
8) given number must be within a range with greater equal              min < number <= max
9) given number must be within a range equal to bound                min <= number <= max
10) given number must be outside a range without equal                  number < min  AND number > max
11) given number must be outside a range with lower equal              number <= min  AND number > max
12) given number must be outside a range with lower equal              number < min  AND number >= max
13) given number must be outside a range with lower equal              number <= min  AND number >= max
The following condition is important for compatility.
14 ) given number must be less than the limit ( the limit is set as max )    number <max
15 ) given number must be great than the limit ( the limit is set as min )    number >min
16 ) given number must be less than or equal to  the limit ( the limit is set as max ) number <= max
17 ) given number must be great than or equal to  the limit ( the limit is set as min ) number >=min```

## Dealing data type

Another headache is how to use these rulers on every kind of data structures. C++ provides server method, such as string , double , int ,long , datetime etc. Each has different way to check. That the relation sign has different meaning. The unsafe way is providing a macro. I think the better way is using template of C++, which is strong type , can make our code more safe when running.

```template<class T>
BOOL internalCheckLess( T& value , T& minv, T& maxv)
{
return value < minv;
}

template<class T>
BOOL internalCheckGreat( T& value , T& minv, T& maxv)
{
return value > maxv;
}

template<class T>
BOOL internalCheckLessMax( T& value , T& minv, T& maxv)
{
return value < maxv;
}

template<class T>
BOOL internalCheckGreatMin( T& value , T& minv, T& maxv)
{
return value > minv;
}

template<class T>
BOOL internalCheckLessMaxEqu( T& value , T& minv, T& maxv)
{
return value <= maxv;
}

template<class T>
BOOL internalCheckGreatMinEqu( T& value , T& minv, T& maxv)
{
return value >= minv;
}

template<class T>
BOOL internalCheckBoth( T& value , T& minv, T& maxv)
{
return value > minv && value < maxv ;
}

template<class T>
BOOL internalCheckLessEqu( T& value , T& minv, T& maxv)
{
return value <= minv;
}

template<class T>
BOOL internalCheckGreatEqu( T& value , T& minv, T& maxv)
{
return value >= maxv;
}

template<class T>
BOOL internalCheckBothEqu( T& value , T& minv, T& maxv)
{
return value >= minv && value <= maxv ;
}

template<class T>
BOOL internalCheckBothLessEqu( T& value , T& minv, T& maxv)
{
return value >= minv && value < maxv ;
}

template<class T>
BOOL internalCheckBothGreatEqu( T& value , T& minv, T& maxv)
{
return value > minv && value <= maxv ;
}
template<class T>
BOOL internalCheckOutEqu( T& value , T& minv, T& maxv)
{
return value <= minv && value >= maxv ;
}

template<class T>
BOOL internalCheckOutLessEqu( T& value , T& minv, T& maxv)
{
return value <= minv && value > maxv ;
}

template<class T>
BOOL internalCheckOutGreatEqu( T& value , T& minv, T& maxv)
{
return value < minv && value >= maxv ;
}

template<class T>
BOOL internalCheckOut( T& value , T& minv, T& maxv)
{
return value < minv && value > maxv ;
}```

## Promption

When the checking routine found that an error occurred when user inputted data, we shall raise a message to user and tell him or her that what kind of error found. Normally , we will give out a messagebox with reason . The message will be displayed according to user specified. We collect all error message in a header file for easy to be change according to their language or more proper words. We can write a function for each data type.

## Why not to use class

May be it is a good idea to write these function as a class . It is a light method to write these in functions . I think it is more covinent to use function than writing a class. I will demonstrate how to use these function in a control group in next article which will be coming soon.

That’s all ! Enjoy it.

## History

```Version            Author                    Date                Content
===========================================================================
0.9                Johnson Zhou        2002/08/01              Initial
1.0                Johnson Zhou        2002/12/07              Write internal function as template
1.01                Johnson Zhou        2002/12/25              Add function of 14)-17).
```

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

## About the Author

 Software Developer (Senior) China
I'm write program from 1990. My research field is CAG,CAD and Image processing. I select C/C++, ASP, Java, XML as my usaully developing tools. Occasional , write code in Delphi and VB. I'm using Visual C++ from 1996. If you have anything unclear, e-mail to :zhou_cn123@sina.com Software Engineering and CAD is my mainly research program.

You also can reach me on msn: zhoujohnson@hotmail.com

## Comments and Discussions

 First Prev Next
 omg HeToC20-Dec-06 23:29 HeToC 20-Dec-06 23:29
 BOOL ? Christian Graus26-Dec-02 14:00 Christian Graus 26-Dec-02 14:00
 Not necessary Reinout Hillmann25-Dec-02 22:08 Reinout Hillmann 25-Dec-02 22:08
 Re: Not necessary Johnson Zhou26-Dec-02 18:41 Johnson Zhou 26-Dec-02 18:41
 Huh? Daniel Turini25-Dec-02 9:08 Daniel Turini 25-Dec-02 9:08
 Re: Huh? Johnson Zhou26-Dec-02 18:38 Johnson Zhou 26-Dec-02 18:38
 Hum... Tak25-Dec-02 5:46 Tak 25-Dec-02 5:46
 Re: Hum... Johnson Zhou26-Dec-02 18:36 Johnson Zhou 26-Dec-02 18:36
 Last Visit: 31-Dec-99 18:00     Last Update: 28-Jun-16 3:07 Refresh 1

General    News    Suggestion    Question    Bug    Answer    Joke    Praise    Rant    Admin

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.