Casting issue
I have a struct WMTText which I would like to use in the same manner I would an intrinsic value type.
Simple Example:
WMTText text1 = "Hello world";
WMTText text2 = text1;
Console.WriteLine("text1={0}, text2={1}", text1, text2);
Instead of text1 outputting "Hello world", it outputs the structs namespace instead.
I don't want to have to use a public property holding the internal value, and I don't want to call the ToString() either:
Console.WriteLine("text1={0}, text2={1}", text1.Value, text2.Value);
Console.WriteLine("text1={0}, text2={1}", text1.ToString(), text2.ToString());
Although I don't mind adding a ToString() method to return the internal value as long as this can be called implicitly with code like below:
WMTText text2 = text1;
Console.WriteLine("text1={0}, text2={1}", text1, text2);
If anyone knows how to do this I would greatly appreciate your help.
Code for the struct is as follows:
using System;
namespace Wfmc.Interface2.Types.BasicTypes
{
/// <summary>
/// Wfmc standards type: WMTText.
/// </summary>
public struct WMTText
{
//private char _value;
private string _value;
public WMTText(string Value)
{
try
{
_value = Value;
}
catch
{
throw new ArgumentException();
}
}
public static implicit operator string(WMTText Value)
{
return Value._value;
}
public static implicit operator WMTText(string Value)
{
return new WMTText(Value);
}
// TODO: Not sure this is the best way to deal with this
public int Length
{
get
{
return _value.Length;
}
}
}
}
Regards,
Skin
Skin
__________________
Skin
|