Declaring an extended value type

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I want to create a type that behaves like in integer but only allows
assignment of say 3 digit integers where attempts to assign values greater or
loess will throw an exception. E.g. int threeDigitInteget = 7; will throw an
exception.

I can achive this behaviour through the new types constructor but cannot
work out how to intercept assignments using the = assignment operator.

If any know has a natty example or could point me to some documentation I'd
be really appreciative.

Ta in adv.,
Michael
 
Michael.McD said:
I want to create a type that behaves like in integer but only allows
assignment of say 3 digit integers where attempts to assign values greater or
loess will throw an exception. E.g. int threeDigitInteget = 7; will throw an
exception.
This would require you to extent System.ValueType. This is a special
class and it cannot be extened by user code.

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
 
Perhaps you can use a get/set property, check the range in the set and throw
an exception yourself. Just a thought.
:) Thom
 
Michael,
I want to create a type that behaves like in integer but only allows
assignment of say 3 digit integers where attempts to assign values greater or
loess will throw an exception. E.g. int threeDigitInteget = 7; will throw an
exception.

I can achive this behaviour through the new types constructor but cannot
work out how to intercept assignments using the = assignment operator.

If you by three digit integer mean that the value must be 100-999 then
something like this will almost get you there

struct MyInt
{
private int _value;

public MyInt(int value)
{
if ( value < 100 || value > 999 )
throw new ArgumentOutOfRangeException(...);
_value = value;
}

public static implicit operator MyInt(int value)
{
return new MyInt(value);
}

// ... add more operators to make it behave like int ...
}


MyInt mi = 123;

The biggest problem here is that value types can always be zero
initialized with the default constructor, so you can't prevent _value
from being set to zero.

But if you mean that

MyInt mi = 001;

should work but

MyInt mi = 1;

shouldn't, then you can't do it.




Mattias
 
Anders Nor?s said:
This would require you to extent System.ValueType. This is a special
class and it cannot be extened by user code.

Um, yes it can:

struct X
{
}

compiles to:

..class private sequential ansi sealed beforefieldinit Foo
extends [mscorlib]System.ValueType
{
.pack 0
.size 1
} // end of class Foo
 
Michael.McD said:
I want to create a type that behaves like in integer but only allows
assignment of say 3 digit integers where attempts to assign values greater or
loess will throw an exception. E.g. int threeDigitInteget = 7; will throw an
exception.

I can achive this behaviour through the new types constructor but cannot
work out how to intercept assignments using the = assignment operator.

If any know has a natty example or could point me to some documentation I'd
be really appreciative.

It sounds like you want to provide an implicit conversion from int to
your type. Personally I'd recommend against it as it makes it highly
non-obvious what's going on, but here's the code anyway:

using System;

struct Foo
{
int value;

public static implicit operator Foo (int value)
{
Foo ret = new Foo();

if (value < 100 || value > 999)
{
throw new ArgumentOutOfRangeException
("Invalid value: "+value);
}

ret.value = value;
return ret;
}
}

class Test
{
static void Main()
{
Foo x = 123;
x = 456;
x = 87;
}
}
 
Jon said:
Um, yes it can:

struct X
{
}

compiles to:

.class private sequential ansi sealed beforefieldinit Foo
extends [mscorlib]System.ValueType
{
.pack 0
.size 1
} // end of class Foo

I know, I wasn't thinking when I answered the question. Luckily Mattis
gave a good answer so I let it slide.

Anders
 
Back
Top