User Defined conversions

  • Thread starter Thread starter Raj
  • Start date Start date
R

Raj

How I can make classes compatible with each other without inheritance but
using casting?

Ex:

class Me
{

}

class You
{
}

Me m=new Me();
You u=(You)m;

I shall be able to access members of m through u

Could not find much information anywhere!

Thank you

Regards
Raj
 
Raj said:
How I can make classes compatible with each other without inheritance but
using casting?

You'll have to define "compatible" more precisely.

In C#, there is actually a such thing as "user defined conversion", per
the subject you gave this message thread. But those have nothing to do
with accessibility of members between classes, as the rest of your post
seems to suggest you are really asking about.

Given the contradiction in your post, without a more precisely stated
question it will be impossible to know exactly what question you want
answered.
Ex:

class Me
{

}

class You
{
}

Me m=new Me();
You u=(You)m;

I shall be able to access members of m through u

Accessibility is addressed through the use of accessibility modifiers:
"public", "protected", "internal", and "private". If you want code in
class "You" to be able to access members of class "Me", then those
members need to be declared with an accessibility modifier that is
sufficiently accessible for "You" to have access to them.

The "private" modifier will definitely not work in that case. The
"protected" modifier is unlikely to work (requires "You" be a sub-class
of "Me" and that the code in "You" be accessing the members of "Me"
through an instance of "You"). That leaves "internal" and "public".

Now, the code you posted appears to try to cast the instance of "Me" to
the type "You". This can work if you define an explicit conversion in
the code from "Me" to "You". But note that doing so does _not_ change
the accessibility.

At best, if you declare the conversion in the "Me" type, then it can use
private members to initialize the new instance of "You". But the class
"You" doesn't get access to those members in "Me". It can only see the
values of those members, as they appear in the newly initialized
instance of "You".

If the conversion is declared in the "You" class, then it can use only
those members of "Me" that the class "You" already has access to. The
conversion itself doesn't do anything to change accessibility.

In short: your post seems to be asking about two completely different
things: accessibility, and user-defined conversions. Which one are you
really looking for an answer to?
Could not find much information anywhere!

Where did you look? MSDN has an extensive discussion on the question of
accessibility.

Pete
 
Raj said:
How I can make classes compatible with each other without inheritance but
using casting?

Ex:

class Me
{

}

class You
{
}

Me m=new Me();
You u=(You)m;

I shall be able to access members of m through u

Could not find much information anywhere!

What exactly do you mean with "...able to access members of m through
u..."?

m and u will never be the same reference.

You can implement either an implicit or explicit operator, i.e. :

static public explicit operator You (Me obj)
{
You y = new You();

/*
* Convert and assign whatever you need
*/

return y;
}

If you want "You" to be a sort proxy, and to return values from the
original "Me" instance, your only option is to aggregate an instance of
"Me" into "You", and implement properties of "You" accordingly, i.e. :

static public explicit operator You (Me obj)
{
You y = new You(obj);
return y;
}

In both cases, I'd prefer to have a "Convert" method on the classes, and
have to the conversion. It's more clear than using operators.

Can you tell what the actual problem is that you're trying to solve?
 
Back
Top