473,387 Members | 1,812 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,387 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 2709
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.