473,412 Members | 3,343 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,412 software developers and data experts.

best way to do between check

This is a very simple question, I have a number and I want to check if
the number is between 2 integers. I can use 'if' to resolve this
situation. But I am curious, if there is any other verbs that I can
use in C# like sql 'between'?
Thanks.

Nov 5 '07 #1
4 1607
Nope, an if statement is really what you have to use here (along with an
&& operator to check both conditions). You could use the tertiary switch
statement (e.g. "3 < x && x < 6 ? "between" : "not between") but that's
really the same as an if statement.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"DBC User" <db*****@gmail.comwrote in message
news:11*********************@y42g2000hsy.googlegro ups.com...
This is a very simple question, I have a number and I want to check if
the number is between 2 integers. I can use 'if' to resolve this
situation. But I am curious, if there is any other verbs that I can
use in C# like sql 'between'?
Thanks.

Nov 5 '07 #2
This is a very simple question, I have a number and I want to check if
the number is between 2 integers. I can use 'if' to resolve this
situation. But I am curious, if there is any other verbs that I can
use in C# like sql 'between'?
No but you can always roll your own function. How about something like this::

public static bool IsInRange<T>(T value, T minValue, T maxValue) where T : IComparable<T>
{
Debug.Assert(minValue.CompareTo(maxValue) <= 0);
if (minValue.CompareTo(maxValue) 0)
{
string message = string.Format("\"minValue\" ({0}) must be less than or equal to \"maxValue\" ({1})",
minValue.ToString(), maxValue.ToString());
throw new ArgumentException(message);
}

return (value.CompareTo(minValue) >= 0 && value.CompareTo(maxValue) <= 0);
}

bool excellentStudent = IsInRange(grade, 90, 100);
Nov 5 '07 #3
On Nov 5, 9:56 am, "Larry Smith" <no_spam@_nospam.comwrote:
This is a very simple question, I have a number and I want to check if
the number is between 2 integers. I can use 'if' to resolve this
situation. But I am curious, if there is any other verbs that I can
use in C# like sql 'between'?

No but you can always roll your own function. How about something like this::

public static bool IsInRange<T>(T value, T minValue, T maxValue) where T : IComparable<T>
{
Debug.Assert(minValue.CompareTo(maxValue) <= 0);
if (minValue.CompareTo(maxValue) 0)
{
string message = string.Format("\"minValue\" ({0}) must be less than or equal to \"maxValue\" ({1})",
minValue.ToString(), maxValue.ToString());
throw new ArgumentException(message);
}

return (value.CompareTo(minValue) >= 0 && value.CompareTo(maxValue) <= 0);

}

bool excellentStudent = IsInRange(grade, 90, 100);
This is a pretty cool stuff.
Thanks.

Nov 5 '07 #4
Just to point out a caveat that could bite you on the backside if you're not
careful.

Technically, 'between' does NOT include either of the limits. I.E., 4 and 5
are between 3 and 6 but 3 is NOT between 3 and 6.

However, in everyday useage, between is often considered to include either
of the limits. I.E., 3, 4, 5, and 6 are ALL between 3 and 6. For example,
This is how the between operator is implemented in TRANSACT-SQL

The point is, that to be able to use any 'between' function/operator with
confidence you must be aware of just what that function/operator considers
'between' to mean.
"DBC User" <db*****@gmail.comwrote in message
news:11**********************@o38g2000hse.googlegr oups.com...
On Nov 5, 9:56 am, "Larry Smith" <no_spam@_nospam.comwrote:
This is a very simple question, I have a number and I want to check if
the number is between 2 integers. I can use 'if' to resolve this
situation. But I am curious, if there is any other verbs that I can
use in C# like sql 'between'?

No but you can always roll your own function. How about something like
this::

public static bool IsInRange<T>(T value, T minValue, T maxValue) where T
: IComparable<T>
{
Debug.Assert(minValue.CompareTo(maxValue) <= 0);
if (minValue.CompareTo(maxValue) 0)
{
string message = string.Format("\"minValue\" ({0}) must be less
than or equal to \"maxValue\" ({1})",
minValue.ToString(),
maxValue.ToString());
throw new ArgumentException(message);
}

return (value.CompareTo(minValue) >= 0 && value.CompareTo(maxValue)
<= 0);

}

bool excellentStudent = IsInRange(grade, 90, 100);

This is a pretty cool stuff.
Thanks.
Nov 5 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

131
by: Peter Foti | last post by:
Simple question... which is better to use for defining font sizes and why? px and em seem to be the leading candidates. I know what the general answer is going to be, but I'm hoping to ultimately...
4
by: SQLDBA | last post by:
What would be the best practice to follow to keep track of MS SQL server changes... Stroed procs, tables, views, triggers, indexes, DTS and also jobs ect.... I am not quite sure how Source safe...
3
by: kyle.tk | last post by:
So I have a central list of python objects that I want to be able to share between different process that are possibly on different computers on the network. Some of the processes will add objects...
0
by: Robert Stearns | last post by:
I need to validate (sanity check) the total of a group of values. I currently have the following statement: ALTER TABLE flushes ADD CONSTRAINT total CHECK( (coalesce(unfert,0) +...
1
by: Chris Uwins | last post by:
Hi there, i know theres a number of ways I can achieve this but want to know the best, (but still quite simple). Up until a year ago I never used Access but have designed a few databases for...
1
by: Herve MAILLARD | last post by:
Hi, I have to write a software doing the following : - Load a file (containing data) Data will be display in a Treeview and are typed as following Equipement Bloc Tag It could have for each...
1
by: Mike Moore | last post by:
Does anyone have suggestions on the best way to check if a user is logged into asp.net web application? We are not using forms authentication. We are authenticating our users against active...
45
by: Summercoolness | last post by:
it seems that range() can be really slow: the following program will run, and the last line shows how long it ran for: import time startTime = time.time() a = 1.0
14
by: John Salerno | last post by:
What is the best way to check if a file already exists in the current directory? I saw os.path.isfile(), but I'm not sure if that does more than what I need. I just want to check if a file of a...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
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.