Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 18th, 2004, 05:15 AM
Authorized User
 
Join Date: Jun 2003
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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
 
Old March 18th, 2004, 04:56 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Build your structure like this to get the default text to return what you want without explicitly having to call a member:

    private struct Thing
    {
        public string Text;
        public Thing(string sString)
        {
            Text = sString;
        }
        public override string ToString()
        {
            return Text;
        }
    }
 
Old March 19th, 2004, 04:39 AM
Authorized User
 
Join Date: Jun 2003
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks planoie that was just the trick.

Skin





Similar Threads
Thread Thread Starter Forum Replies Last Post
Casting pfrigo BOOK: Professional .NET 2.0 Generics 3 November 19th, 2007 04:49 PM
Casting - Can you help koco C# 1 September 24th, 2007 08:41 AM
table casting dk6607 ASP.NET 1.0 and 1.1 Professional 3 November 20th, 2006 12:48 PM
casting ravibodani Access 1 April 4th, 2006 01:19 PM
Casting question shantawn J2EE 4 August 2nd, 2005 02:40 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.