473,326 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

implicit conversion

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

Oct 30 '06 #1
4 2706
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
docschnipp wrote:
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
Oct 30 '06 #2
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/Languag...nCSharpRVS.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.

docschnipp wrote:
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
Oct 30 '06 #3
Hi Stephan,

"ssamuel" wrote:
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

Oct 30 '06 #4
docschnipp wrote:
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";
Oct 30 '06 #5

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

Similar topics

2
by: Russell Reagan | last post by:
In a newer version of a chess program I am writing, I have created classes that are (more or less) drop in replacements for things that used to be plain old integer or enumerated variables (colors,...
1
by: Christophe Poucet | last post by:
Hellom I have an issue with implicit conversions. Apparently when one calls an operator on a class Y which has a conversion operator to class X which has the operator . Sadly it will not do...
11
by: Steve Gough | last post by:
Could anyone please help me to understand what is happening here? The commented line produces an error, which is what I expected given that there is no conversion defined from type double to type...
9
by: Girish | last post by:
Im trying to understand implicit type conversions from object -> string and vice versa. I have two classes, one Driver and one called StringWrapper. These are just test classes that try and...
11
by: Aaron Queenan | last post by:
Given the classes: class Class { public static implicit operator int(Class c) { return 0; } } class Holder
36
by: Chad Z. Hower aka Kudzu | last post by:
I have an implicit conversion set up in an assembly from a Stream to something else. In C#, it works. In VB it does not. Does VB support implicit conversions? And if so any idea why it would work...
10
by: Pieter Breed | last post by:
Hi All, Please excuse me, but the bulk of my post will be a code post. It describes some weirdness with regards to the implicit casting operator. The crux of the problem is this: I want to...
3
by: utab | last post by:
Dear all, I was trying to write a more complex program, and while searching for sth in my reference C++ primer, by Lippman. I had a question in the mind, see the code below #include <string>...
1
by: drop | last post by:
Hi all, I'd like to know if it's possible to declare an implicit type conversion for when I use declarative Synthax in html. For example, when I have the DropDownList, I can declatively set...
8
by: jonpb | last post by:
Hi, Is it possible to define a implicit operator from base to derived types in C#? I have a Point class and would like to derive a Vector class from it and add a couple new vector related...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.