Limiting an assignment to one type or another

  • Thread starter Thread starter valentin tihomirov
  • Start date Start date
V

valentin tihomirov

// define two types
class Type1 {}
class Type2 {}

// define a third type which variables are of type1 or type2
enumeration AlternativeType {Type1, Type2}

// assign
AlternativeType var1 = new Type1();
AlternativeType var2 = new Type2();
 
The only way you could do this is if one type derived from another, they
both derived from the same type, or they both implemented the same
interface.

In any case, you would only be able to access the members on the type
which they shared in common. If you want to access functionality specific
to the type, then you have to cast to that type.
 
Hi,


valentin tihomirov said:
// define two types
class Type1 {}
class Type2 {}

// define a third type which variables are of type1 or type2
enumeration AlternativeType {Type1, Type2}

// assign
AlternativeType var1 = new Type1();
AlternativeType var2 = new Type2();


There is no way of doing that.
 
Hi,

Nicholas Paldino said:
The only way you could do this is if one type derived from another,
they both derived from the same type, or they both implemented the same
interface.

Yes, but how you can enforce that an instance of a given defined type (
pseudo code "enumeration AlternativeType {Type1, Type2}" ) can only be
assigned with an instance of Type1 or Type2 ?

I think that it was the question the OP posed
 
Sorry, the only way that the OP can come close to doing something like
this, as the way that he wants is not possible.

That suit your tastes better?
 
Ignacio Machin ( .NET/ C# MVP ) said:
Hi,





There is no way of doing that.

F# must have found a way, because Caml allows exactly what the OP is asking
about.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top