Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / October 2003

Tip: Looking for answers? Try searching our database.

Casting ValueType variable to primitive type

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
C?lio Cidral Junior - 30 Sep 2003 14:02 GMT
Hello.

(I don't know if this is the right newsgroup to post this message, so sorry
if I'm cross-posting).

I am writing a base class which has an abstract property of some type. This
property must be overrided and have different return types at base classes,
as follows:

public abstract class NumericTextBox : TextBox
{
    public abstract ValueType Value
    {
          get;
    }
}

public class DecimalTextBox : NumericTextBox
{
    public override Decimal Value
    {
          get
          {
                  return 123;
           }
    }
}

public class DoubleTextBox : NumericTextBox
{
    public override Double Value
    {
          get
          {
                  return 123;
           }
    }
}

I know the previous code won't work. Anyway, how can I do that? What's the
right way to do that?

Thank you in advance.

Celio Cidral Junior
WEG Eletric
Brazil
Jennifer Hamilton [MSFT] - 03 Oct 2003 19:26 GMT
You could box it and return an Object, then unbox it when you need to use
it.   C# will implicitly box a value type to a reference type, but you must
explicitly unbox it.

public abstract class NumericTextBox
{
   public abstract Object Value
   {
       get;
   }
}

public class DecimalTextBox : NumericTextBox
{
   public override Object Value
   {
       get
       {
           return (decimal)123;
       }
   }
}

public class DoubleTextBox : NumericTextBox
{
   public override Object Value
   {
       get
       {
           return 123.123d;
       }
   }
}

public class Test
{
   public static void Main()
   {
       NumericTextBox box1 = new DecimalTextBox();
       NumericTextBox box2 = new DoubleTextBox();
       Object[] values = new Object[2];
       values[0] = box1.Value;
       values[1] = box2.Value;
       for (int i=0; i < values.Length; i++)
       {
           if (values[i] is System.Decimal)
           {
               Decimal decimalValue = (decimal)values[i];
               Console.WriteLine("Value " + i + " is decimal value " +
decimalValue);
           }
           else if (values[i] is System.Double)
           {
               Double doubleValue = (double)values[i];
               Console.WriteLine("Value " + i + " is double value " +
doubleValue);
           }
           else
           {
               Console.WriteLine("Value " + i + " is unknown type " +
values[i]);
           }
       }
   }
}

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.