Click here to Skip to main content
15,886,067 members
Articles / Desktop Programming / MFC
Article

A set of universal data check functions

Rate me:
Please Sign up or sign in to vote.
1.07/5 (15 votes)
24 Dec 20022 min read 53.3K   243   15   8
A set of universal data check functions can be used in any systems with checking requirement.

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

License

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


Written By
Software Developer (Senior)
China 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

 
Generalomg Pin
HeToC20-Dec-06 23:29
HeToC20-Dec-06 23:29 
QuestionBOOL ? Pin
Christian Graus26-Dec-02 14:00
protectorChristian Graus26-Dec-02 14:00 
GeneralNot necessary Pin
Rein Hillmann25-Dec-02 22:08
Rein Hillmann25-Dec-02 22:08 
GeneralRe: Not necessary Pin
Johnson Zhou26-Dec-02 18:41
Johnson Zhou26-Dec-02 18:41 
From General view of question , it is necessary.Blush | :O , From another aspect, it may be a good idea. See my next article will coming soon . it is a about a control group which using these functions.;P

Johnson Zhou
QuestionHuh? Pin
Daniel Turini25-Dec-02 9:08
Daniel Turini25-Dec-02 9:08 
AnswerRe: Huh? Pin
Johnson Zhou26-Dec-02 18:38
Johnson Zhou26-Dec-02 18:38 
GeneralHum... Pin
Daniel 'Tak' M.25-Dec-02 5:46
Daniel 'Tak' M.25-Dec-02 5:46 
GeneralRe: Hum... Pin
Johnson Zhou26-Dec-02 18:36
Johnson Zhou26-Dec-02 18:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

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