473,493 Members | 2,254 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Declaring an extended value type

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
Nov 16 '05 #1
6 1496
Michael.McD wrote:
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/
Nov 16 '05 #2
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #3
Perhaps you can use a get/set property, check the range in the set and throw
an exception yourself. Just a thought.
:) Thom

"Michael.McD" wrote:
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

Nov 16 '05 #4
"Anders Nor?s [MCAD]" <an**********@objectware.no> wrote:
Michael.McD wrote:
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.


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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
Michael.McD <Mi********@discussions.microsoft.com> wrote:
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 Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
Jon Skeet [C# MVP] wrote:
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
Nov 16 '05 #7

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

Similar topics

3
1575
by: Rim | last post by:
Hi, When I extend a built-in type, how do I cause the extended type to be used by default, without having to specify it? For instance, say I extend the int type: class myint(int): def...
16
1961
by: G Patel | last post by:
Hi, If I want to call functions that don't return int without declaring them, will there be any harm? I only want to assign the function(return value) to the type that it returns, so I don't...
1
2855
by: Maarten Terlingen | last post by:
Sample: ClassA: ClassB ClassB: ClassC ClassA x = new Class C I want to know the declaring type of C. x.GetType() returns C x.GetType().BaseType returns B x.GetType().BaseType.BaseType...
9
2400
by: Mario Vázquez | last post by:
Hi, I have a component which exposes an extended property. I want to edit this property using a UITypeEditor. The problem is that when the designer raises my UITypeEditor sends a reference of...
1
1982
by: Ilya N. Golubev | last post by:
There is already one function processing pointer values this way. And there may easily appear more, including wrappers for this function. For purposes of further discussion, consider simplified...
10
6552
by: AZRebelCowgirl73 | last post by:
This is what I have so far: My program! import java.util.*; import java.lang.*; import java.io.*; import ch06.lists.*; public class UIandDB {
13
48019
by: ramif | last post by:
Is there a way to print extended ASCII in C?? I tried to code something, but it only displays strange symbols. here is my code: main() { char chr = 177; //stores the extended ASCII...
12
1266
by: Fabien Henriet | last post by:
Hello, I got a problem when I try to extend a textbox. So I overwrite the Render method. For the stuff I try to do, I would like to add a custom attribute to my customized textbox. So it...
0
1034
radcaesar
by: radcaesar | last post by:
I was saving the schema of a dataset using Dataset.WriteXmlSchema. I am getting the result like this <?xml version="1.0" standalone="yes"?> <xs:schema id="NewDataSet" xmlns=""...
0
7195
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...
1
6873
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
7367
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
5453
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4889
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...
0
4579
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3088
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1400
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
644
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.