Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Type declaration ?

Type declaration ?

Scheduled Pinned Locked Moved C#
questionhelp
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    daveyerwin
    wrote on last edited by
    #1

    Heres what I do now

    Type a = b.GetType();
    if (a.Name == "Blog")
    {
    Console.WriteLine("blog = {0}", ((Blog)b).text);
    error = "ok";
    return 0;
    }
    if (a.Name == "String")
    {
    Console.WriteLine("blog = {0}", (String)b);
    error = "ok";
    return 0;
    }
    Console.WriteLine("blog = TypeError");
    error = "unKnownType";
    return 1;

    I want to set the type similar to this ((GetType())b).text How can I do this ?

    0 L L 3 Replies Last reply
    0
    • D daveyerwin

      Heres what I do now

      Type a = b.GetType();
      if (a.Name == "Blog")
      {
      Console.WriteLine("blog = {0}", ((Blog)b).text);
      error = "ok";
      return 0;
      }
      if (a.Name == "String")
      {
      Console.WriteLine("blog = {0}", (String)b);
      error = "ok";
      return 0;
      }
      Console.WriteLine("blog = TypeError");
      error = "unKnownType";
      return 1;

      I want to set the type similar to this ((GetType())b).text How can I do this ?

      0 Offline
      0 Offline
      0x3c0
      wrote on last edited by
      #2

      If b implements the interface IBlog, then you can cast b to an IBlog and provided Text is part of the interface declaration and is implemented in b's class, you can get the text. Something like:

      interface IBlog
      {
      public string text { get; }
      }

      public class Blog : IBlog
      {
      public string text
      {
      get { return "name"; }
      }
      }

      public int testThisExample()
      {
      object b = new Blog();

      if(b is IBlog)
          Console.WriteLine("blog = {0}", (b as IBlog).text);
      return 0;
      

      }

      Between the idea And the reality Between the motion And the act Falls the Shadow

      1 Reply Last reply
      0
      • D daveyerwin

        Heres what I do now

        Type a = b.GetType();
        if (a.Name == "Blog")
        {
        Console.WriteLine("blog = {0}", ((Blog)b).text);
        error = "ok";
        return 0;
        }
        if (a.Name == "String")
        {
        Console.WriteLine("blog = {0}", (String)b);
        error = "ok";
        return 0;
        }
        Console.WriteLine("blog = TypeError");
        error = "unKnownType";
        return 1;

        I want to set the type similar to this ((GetType())b).text How can I do this ?

        L Offline
        L Offline
        Leonscape
        wrote on last edited by
        #3

        You could override the ToString() function in the Blog class.

        class Program
        {
        static void Main( string[] args )
        {
        object obj = "Test from string";
        Console.WriteLine("The result of to string is:" + obj.ToString());
        obj = new Test();
        Console.WriteLine("The result of to string is:" + obj.ToString());
        Console.ReadKey();
        }
        }

        public class Test
        {
        public override string ToString()
        {
        return "Test from class";
        }
        }

        Using the wrong tool for the job is half the fun.

        1 Reply Last reply
        0
        • D daveyerwin

          Heres what I do now

          Type a = b.GetType();
          if (a.Name == "Blog")
          {
          Console.WriteLine("blog = {0}", ((Blog)b).text);
          error = "ok";
          return 0;
          }
          if (a.Name == "String")
          {
          Console.WriteLine("blog = {0}", (String)b);
          error = "ok";
          return 0;
          }
          Console.WriteLine("blog = TypeError");
          error = "unKnownType";
          return 1;

          I want to set the type similar to this ((GetType())b).text How can I do this ?

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi, 1. you could concentrate on the ToString() method which exists for all objects; you should override for most types since for most classes ToString() by default returns a type string. 2. you should NOT check types by getting the type's name, then string.compare. C# has the is keyword which returns true when the type matches (directly or through inheritance). :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups