Custom base types !

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

Guest

Hi all !

Here is a tricky one, only for gurus !
I would like to associate a presence information with a base type.
In C language, we added a boolean presence field with each optional field :

typedef struct
{
unsigned char myFieldIsPresent;
unsigned short myField;
} SExample;

Let's move to .NET. I would like to merge these two entities in a single one.
First try, I can create a generic class :

class MyType<T>
{
public bool IsPresent
{
get { ... }
set { ... }
}

public T Value
{
get { ... }
set { ... }
}
}

Quite fine, but I must use the Value property to access the value !

MyType<ushort> myField = new MyType<ushort>;
myField.Value = 12;

The Graal would be the following :

MysteriousType myField = new MysteriousType;
myField = 12;
myField.IsPresent = true;


Anybody feeling inspired ? :)

Anyway, thank you very much...


Thomas
 
"Thomas Weiss" <[email protected]> a écrit dans le
message de news: (e-mail address removed)...

| The Graal would be the following :
|
| MysteriousType myField = new MysteriousType;
| myField = 12;
| myField.IsPresent = true;
|
| Anybody feeling inspired ? :)

Add an implicit conversion operator ?

Joanna
 
Thomas Weiss said:
Unfortunately, the = operator cannot be overloaded in C#...

Well, that's fortunate in my view. However, you *can* still write an
implicit conversion which would allow:

MysteriousType myField = new MysteriousType();
myField = 12;

However, the second statement wouldn't change the value of the
initially created object's Value property - it would create a new
object. In other words, it would be equivalent to:

MysteriousType myField = 12;

Personally, I don't like implicit conversion operators in the first
place, but it's better than allowing

myField = 12;

to change only *part* of a value...
 
OK.

I guess this all means that the C# cannot allow me to do what
I am looking for...

Thomas
 
Thomas said:
Hi all !

Here is a tricky one, only for gurus !
I would like to associate a presence information with a base type.
In C language, we added a boolean presence field with each optional field :

typedef struct
{
unsigned char myFieldIsPresent;
unsigned short myField;
} SExample;

Let's move to .NET. I would like to merge these two entities in a single one.

Isn't this just Nullable Types ?
 
Larry Lard said:
Isn't this just Nullable Types ?

Well... YES IT IS !
Starting with C# 2.0, I was obnubilated with the generics... and didn't
see this wonderful innovation !

Just two words for you, Larry :
"Thank" and "you" !
 
Back
Top