G
Girish
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 emulate the pattern im trying to follow in an
existing project.
These are my steps:
1) I have a method "printMe" existing in the application which originally
used to take in a string. This method is static and sits in the Driver
class. I cannot remove this method since it public and is used all over the
application.
2) Now, Ive realised that I need to do some parsing and cleaning up of the
string before the method can continue to execute its logic (printMe).
3) My solution was to change the method sig to take in a StringWrapper
class, created by me, that will take in an implicit string and return a
StringWrapper object with the cleaned up string.
4) Now, when the StringWapper object is used by the code in the method
printMe, I want to treat it as a String object and not a StringWrapper
object. So what Ive done is create another implicit conversion from
StringWrapper to String.
I hope this makes sense at this point. Essentially Im tring to minimize code
changes in the code that calls my printMe method so that it knows of only
String objects. AND the reminaing logic in printMe function should only know
of String objects as well. So the StringWrapper class will essentially
convert string to an object of its type, do the cleaning of the string and
then, whereever the StringWrapper instance is used in printMe method, it
should expose itself as a String instead.
So my question is
1) In my printMe method, I had a statement called
Console.Write(val.ToLower()). This threw me an exception because it said the
StringWrapper does not support the ToLower method. How come even though I
have an implicit conversion?
2) If I do Console.Write(((string)val).ToLower()); it works!! Even though
this is an explicit conversion and I havent defined any explicit
conversions....
Im kinda lost here.... can anyone help me with a solution to this problem?
Thanks,
Girish
Driver Class
-------------
using System;
namespace test1 {
class Driver {
//old func sig was; static public void printMe(String val)
static public void printMe(StringWrapper val) {
//Console.Write(val.ToLower()); //old
Console.Write(((string)val).ToLower());
}
[STAThread]
static void Main(string[] args) {
String b = "Testing 123";
Driver.printMe(b);
//wait for input before exiting
Console.Read();
}
}
}
StringWrapper Class
---------------------
using System;
namespace test1 {
public class StringWrapper {
private String _val;
public String val {
get { return _val; }
}
public StringWrapper(String val) {
this._val = val;
}
static public implicit operator StringWrapper(String val) {
val += " (im changed)";
return new StringWrapper(val);
}
static public implicit operator string(StringWrapper val) {
return val.val;
}
}
}
vice versa.
I have two classes, one Driver and one called StringWrapper. These are just
test classes that try and emulate the pattern im trying to follow in an
existing project.
These are my steps:
1) I have a method "printMe" existing in the application which originally
used to take in a string. This method is static and sits in the Driver
class. I cannot remove this method since it public and is used all over the
application.
2) Now, Ive realised that I need to do some parsing and cleaning up of the
string before the method can continue to execute its logic (printMe).
3) My solution was to change the method sig to take in a StringWrapper
class, created by me, that will take in an implicit string and return a
StringWrapper object with the cleaned up string.
4) Now, when the StringWapper object is used by the code in the method
printMe, I want to treat it as a String object and not a StringWrapper
object. So what Ive done is create another implicit conversion from
StringWrapper to String.
I hope this makes sense at this point. Essentially Im tring to minimize code
changes in the code that calls my printMe method so that it knows of only
String objects. AND the reminaing logic in printMe function should only know
of String objects as well. So the StringWrapper class will essentially
convert string to an object of its type, do the cleaning of the string and
then, whereever the StringWrapper instance is used in printMe method, it
should expose itself as a String instead.
So my question is
1) In my printMe method, I had a statement called
Console.Write(val.ToLower()). This threw me an exception because it said the
StringWrapper does not support the ToLower method. How come even though I
have an implicit conversion?
2) If I do Console.Write(((string)val).ToLower()); it works!! Even though
this is an explicit conversion and I havent defined any explicit
conversions....
Im kinda lost here.... can anyone help me with a solution to this problem?
Thanks,
Girish
Driver Class
-------------
using System;
namespace test1 {
class Driver {
//old func sig was; static public void printMe(String val)
static public void printMe(StringWrapper val) {
//Console.Write(val.ToLower()); //old
Console.Write(((string)val).ToLower());
}
[STAThread]
static void Main(string[] args) {
String b = "Testing 123";
Driver.printMe(b);
//wait for input before exiting
Console.Read();
}
}
}
StringWrapper Class
---------------------
using System;
namespace test1 {
public class StringWrapper {
private String _val;
public String val {
get { return _val; }
}
public StringWrapper(String val) {
this._val = val;
}
static public implicit operator StringWrapper(String val) {
val += " (im changed)";
return new StringWrapper(val);
}
static public implicit operator string(StringWrapper val) {
return val.val;
}
}
}