473,770 Members | 4,718 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

(Delphi-like) Limited-bounds-types?

As far as I can see, there is no similar way of replicating the
following Delphi-code in C#... Is that true?

type
IntRange = -5..5;
CharRange = 'a'..'z';

TColors = (Red, White, Green, Blue, Yellow);
ColorRange = White..Blue;

Jan 9 '06 #1
21 1963
matko,

No, there is not. In C# you would have to place explicit checks on your
methods/properties to validate that the values are in range.

However, this is not that difficult to do.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"matko" <ma*****@gmail. com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
As far as I can see, there is no similar way of replicating the
following Delphi-code in C#... Is that true?

type
IntRange = -5..5;
CharRange = 'a'..'z';

TColors = (Red, White, Green, Blue, Yellow);
ColorRange = White..Blue;

Jan 9 '06 #2
Hehe. Yes, I know it's not that difficult! ;)
It would, however, just be nice if there was a way...

Thanks for the reply!

Nicholas Paldino [.NET/C# MVP] wrote:
matko,

No, there is not. In C# you would have to place explicit checks on your
methods/properties to validate that the values are in range.

However, this is not that difficult to do.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"matko" <ma*****@gmail. com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
As far as I can see, there is no similar way of replicating the
following Delphi-code in C#... Is that true?

type
IntRange = -5..5;
CharRange = 'a'..'z';

TColors = (Red, White, Green, Blue, Yellow);
ColorRange = White..Blue;


Jan 9 '06 #3
matko wrote:
Hehe. Yes, I know it's not that difficult! ;)
It would, however, just be nice if there was a way...

Thanks for the reply!


You may well be able to write a generic one in .NET 2.0, if that's any
help. I don't know that it would really be worth it though...

Jon

Jan 9 '06 #4
There is a way to do this in .NET 1.1 and before. You can create
context-bound objects which will allow you to intercept calls and perform
actions (you would indicate your range through attributes). However,
generally, this isn't something you want to do only because there is some
overhead in processing the call stack into a message based format (which is
what the context bound interception model does).

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:11******** *************@z 14g2000cwz.goog legroups.com...
matko wrote:
Hehe. Yes, I know it's not that difficult! ;)
It would, however, just be nice if there was a way...

Thanks for the reply!


You may well be able to write a generic one in .NET 2.0, if that's any
help. I don't know that it would really be worth it though...

Jon

Jan 9 '06 #5
I don't know much about .NET 2.0 (yet), but couldn't you create an
IntRange struct in .NET 1.1 that would enforce a range? It wouldn't be
too difficult to do, and would incur minimum overhead.

The only downside is that you would have to declare a separate struct
for each base type, and define all of the implicit and explicit casts,
and math operators, etc, for the new struct.

Well... maybe I should qualify that "not too difficult to do": not too
hard on the brain, but lots of typing. :-)

Jan 9 '06 #6
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard .caspershouse.c om> wrote:
There is a way to do this in .NET 1.1 and before. You can create
context-bound objects which will allow you to intercept calls and perform
actions (you would indicate your range through attributes). However,
generally, this isn't something you want to do only because there is some
overhead in processing the call stack into a message based format (which is
what the context bound interception model does).


Well, you could do it in 1.1 without context-bound objects, by
converting the enum to its base type value and doing a range check on
that. It's just nicer with generics :)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 9 '06 #7
Bruce Wood <br*******@cana da.com> wrote:
I don't know much about .NET 2.0 (yet), but couldn't you create an
IntRange struct in .NET 1.1 that would enforce a range? It wouldn't be
too difficult to do, and would incur minimum overhead.

The only downside is that you would have to declare a separate struct
for each base type, and define all of the implicit and explicit casts,
and math operators, etc, for the new struct.

Well... maybe I should qualify that "not too difficult to do": not too
hard on the brain, but lots of typing. :-)


Here's a .NET 2.0 version (and test case). The .NET 1.1 version would
either require boxing or a separate type for each enum etc:

using System;

public class Range<T> where T : struct, IComparable
{
T min, max;

public Range(T min, T max)
{
this.min = min;
this.max = max;
}

public bool Includes(T value)
{
return min.CompareTo(v alue) <= 0 &&
max.CompareTo(v alue) >= 0;
}
}

enum Foo
{
First,
Second,
Third,
Fourth,
Fifth
}

class Test
{
static void Main()
{
Range<Foo> range = new Range<Foo>(Foo. Second, Foo.Fourth);

foreach (Foo f in Enum.GetValues( typeof(Foo)))
{
Console.WriteLi ne ("{0} - {1}", f, range.Includes( f));
}
}
}

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 9 '06 #8
Matko,

You could handle these special cases in static methods of a class that is
named appropriately. For example, the enumeration could be handled like
this
public class MyColors
{
public enum TypeEnum : byte
{
Unknown = 0,
Red,
White,
Green,
Blue,
Yellow
};

public static bool IsColorInTheFla g(TypeEnum eColor)
{
switch(eColor)
{
case TypeEnum.Red :
case TypeEnum.White:
case TypeEnum.Blue:
return true;
default :
return false;
}
}

public static bool IsValidColor(by te clr)
{
return ((clr > 0) && Enum.IsDefined( typeof(MyColors .TypeEnum), clr));
}

}

Dave

"matko" <ma*****@gmail. com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
As far as I can see, there is no similar way of replicating the
following Delphi-code in C#... Is that true?

type
IntRange = -5..5;
CharRange = 'a'..'z';

TColors = (Red, White, Green, Blue, Yellow);
ColorRange = White..Blue;

Jan 9 '06 #9
Generics doesn't really do much here (yes, compile time checking against
the type passed in is nice), but I think that the thing the OP is trying to
get at is that he doesn't want to perform a check manually, he wants to say
somehow that this is his range, and have the runtime enforce it.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Bruce Wood <br*******@cana da.com> wrote:
I don't know much about .NET 2.0 (yet), but couldn't you create an
IntRange struct in .NET 1.1 that would enforce a range? It wouldn't be
too difficult to do, and would incur minimum overhead.

The only downside is that you would have to declare a separate struct
for each base type, and define all of the implicit and explicit casts,
and math operators, etc, for the new struct.

Well... maybe I should qualify that "not too difficult to do": not too
hard on the brain, but lots of typing. :-)


Here's a .NET 2.0 version (and test case). The .NET 1.1 version would
either require boxing or a separate type for each enum etc:

using System;

public class Range<T> where T : struct, IComparable
{
T min, max;

public Range(T min, T max)
{
this.min = min;
this.max = max;
}

public bool Includes(T value)
{
return min.CompareTo(v alue) <= 0 &&
max.CompareTo(v alue) >= 0;
}
}

enum Foo
{
First,
Second,
Third,
Fourth,
Fifth
}

class Test
{
static void Main()
{
Range<Foo> range = new Range<Foo>(Foo. Second, Foo.Fourth);

foreach (Foo f in Enum.GetValues( typeof(Foo)))
{
Console.WriteLi ne ("{0} - {1}", f, range.Includes( f));
}
}
}

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jan 9 '06 #10

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

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.