implicit conversion

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

Guest

Hi,

I have an object that manages a kind of Variant type. For comfort reasons I
want to have the possibility to do this:

MyPropclass prop = new MyPropclass();
string m = "blabla";

prop = m;

How can I do this? Is there a way to overwrite "="? Or must I extend "string"?

Thanks
doc
 
doc,

Technically, you'd have to extend String. I'm not sure if base types
are declared partial in 2.0, but even if they are, that's probably a
bad idea.

You'd do much better to have a MyPropclass(string) constructor, or
maybe a static MyPropclass MyPropclass.Factory(string) method. If
MyPropclass encapsulates substantially more than a string, void
MyPropclass.Assimilate(string) may be appropriate. Alternately, if
you're into "clean" language constructs, you can write static
MyPropclass operator+(string, MyPropclass) and end up with:

MyPropclass prop = new MyPropclass();
[ ... do stuff with prop ... ]
string m = "mystring";
prop += m;


Does that help?


Stephan
 
Depends. Will the user be making changes to the string object before
it is put into the propclass? Or can the "new MyPropclass();" be
dropped? That is

MyPropclass prop = "blabla"; //constructs a MyPropclass object out of
the string

use implicit user-defined conversion operators:

http://www.c-sharpcorner.com/Language/TypeConversionsInCSharpRVS.asp -
scroll down to "user-defined".

I'm not positive it'll work, haven't tried it myself, but I *think*
that's what they're for.
 
Hi Stephan,

ssamuel said:
doc,

Technically, you'd have to extend String. I'm not sure if base types
are declared partial in 2.0, but even if they are, that's probably a
bad idea.

Agree, sounds bad or at least not very elegant.
You'd do much better to have a MyPropclass(string) constructor, or
maybe a static MyPropclass MyPropclass.Factory(string) method. If
MyPropclass encapsulates substantially more than a string, void
MyPropclass.Assimilate(string) may be appropriate. Alternately, if
you're into "clean" language constructs, you can write static
MyPropclass operator+(string, MyPropclass) and end up with:

MyPropclass prop = new MyPropclass();
[ ... do stuff with prop ... ]
string m = "mystring";
prop += m;

I am sticking with a bunch of overloaded ".Set(#sometype# newValue)"
functions. When I am deeper in C# I will try to come up with something nicer,
but for now it is okay.

Thanks for your help,

doc
 
docschnipp said:
Hi,

I have an object that manages a kind of Variant type. For comfort reasons I
want to have the possibility to do this:

MyPropclass prop = new MyPropclass();
string m = "blabla";

prop = m;

How can I do this? Is there a way to overwrite "="? Or must I extend "string"?

Thanks
doc

Implicit conversions works just fine. Example:

public class Demo {

private string _value;
public Demo(string value) {
_value = value;
}
public static implicit operator Demo(string value) {
return new Demo(value);
}

}

Usage:

Demo d = "asdf";
 

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