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. Creating subobjects within an object?

Creating subobjects within an object?

Scheduled Pinned Locked Moved C#
questionworkspace
5 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.
  • G Offline
    G Offline
    Goalie35
    wrote on last edited by
    #1

    Ok, I'm sure this is a simple question but how would I create subobjects within an object? I want to create an object that lists the "status" of my application, to determine if it ran ok or not. So I want to have validation code like the following:

    Status status = RunReport();
    if(status.Exception) //boolean
    {
    string exceptionMessage = status.Exception.Message;
    string stackTrace = status.Exception.StackTrace;
    }

    Or....perhaps this:

    Status status = RunReport();
    if(status.Success) //boolean
    {
    //Ran ok. Hooray!!!
    }

    So my question is, how could I setup my "Status" object to have examples like the following: Status.Success (boolean) Status.Exception (boolean) Status.Exception.Message (string) Status.Exception.StackTrace (string) Thanks.

    L T 2 Replies Last reply
    0
    • G Goalie35

      Ok, I'm sure this is a simple question but how would I create subobjects within an object? I want to create an object that lists the "status" of my application, to determine if it ran ok or not. So I want to have validation code like the following:

      Status status = RunReport();
      if(status.Exception) //boolean
      {
      string exceptionMessage = status.Exception.Message;
      string stackTrace = status.Exception.StackTrace;
      }

      Or....perhaps this:

      Status status = RunReport();
      if(status.Success) //boolean
      {
      //Ran ok. Hooray!!!
      }

      So my question is, how could I setup my "Status" object to have examples like the following: Status.Success (boolean) Status.Exception (boolean) Status.Exception.Message (string) Status.Exception.StackTrace (string) Thanks.

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

      You can define your own Status type (class or struct). It could have whatever public fields (or better properties) you like. You would instantiate such type like any other. Maybe so:

      public Status RunReport() {
      ...
      Status status=new Status(true);
      return status;
      }

      However, seeing you are thinking "Exception", why not use real exceptions, i.e. return results when all goes well, and throw an Exception when something goes wrong, so the caller can catch that Exception?

      Goalie35 wrote:

      Status.Exception (boolean)
      Status.Exception.Message (string)

      That is not possible. Either Exception is a boolean and it hasn't subfields; or it is a composite type (with fields/properties Message, StackTrace, etc.) and then it doesn't have an overall boolean value. I would suggest you return to your C# book and read the chapters on classes, and Exceptions. :)

      Luc Pattyn [My Articles] Nil Volentibus Arduum

      B 1 Reply Last reply
      0
      • L Luc Pattyn

        You can define your own Status type (class or struct). It could have whatever public fields (or better properties) you like. You would instantiate such type like any other. Maybe so:

        public Status RunReport() {
        ...
        Status status=new Status(true);
        return status;
        }

        However, seeing you are thinking "Exception", why not use real exceptions, i.e. return results when all goes well, and throw an Exception when something goes wrong, so the caller can catch that Exception?

        Goalie35 wrote:

        Status.Exception (boolean)
        Status.Exception.Message (string)

        That is not possible. Either Exception is a boolean and it hasn't subfields; or it is a composite type (with fields/properties Message, StackTrace, etc.) and then it doesn't have an overall boolean value. I would suggest you return to your C# book and read the chapters on classes, and Exceptions. :)

        Luc Pattyn [My Articles] Nil Volentibus Arduum

        B Offline
        B Offline
        BobJanova
        wrote on last edited by
        #3

        It is actually possible (but note, OP, this is a flight of intellectual fancy and don't do this in real code):

        class MyExceptionThing {
        public string Message { get; set; }
        public string StackTrace { get; set; }

        public static implicit operator bool(MyExceptionThing thing) { return Message != null; }
        }

        class Status {
        public MyExceptionThing Exception { get; private set; }
        }

        Custom implicit cast operators can cover a multitude of sins! I too recommend that you do what Luc says here and expose an actual Exception (i.e. System.Exception, something that you can use throw on) if you want that information available in a status object. There is precedent for this in the Task Parallel Library and BackgroundWorker. Then you can check for

        if(status.Exception != null){ ... }

        L 1 Reply Last reply
        0
        • B BobJanova

          It is actually possible (but note, OP, this is a flight of intellectual fancy and don't do this in real code):

          class MyExceptionThing {
          public string Message { get; set; }
          public string StackTrace { get; set; }

          public static implicit operator bool(MyExceptionThing thing) { return Message != null; }
          }

          class Status {
          public MyExceptionThing Exception { get; private set; }
          }

          Custom implicit cast operators can cover a multitude of sins! I too recommend that you do what Luc says here and expose an actual Exception (i.e. System.Exception, something that you can use throw on) if you want that information available in a status object. There is precedent for this in the Task Parallel Library and BackgroundWorker. Then you can check for

          if(status.Exception != null){ ... }

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

          Thanks Bob. I didn't know that, however I never felt a need for it either. I don't think I'll ever use it... :)

          Luc Pattyn [My Articles] Nil Volentibus Arduum

          1 Reply Last reply
          0
          • G Goalie35

            Ok, I'm sure this is a simple question but how would I create subobjects within an object? I want to create an object that lists the "status" of my application, to determine if it ran ok or not. So I want to have validation code like the following:

            Status status = RunReport();
            if(status.Exception) //boolean
            {
            string exceptionMessage = status.Exception.Message;
            string stackTrace = status.Exception.StackTrace;
            }

            Or....perhaps this:

            Status status = RunReport();
            if(status.Success) //boolean
            {
            //Ran ok. Hooray!!!
            }

            So my question is, how could I setup my "Status" object to have examples like the following: Status.Success (boolean) Status.Exception (boolean) Status.Exception.Message (string) Status.Exception.StackTrace (string) Thanks.

            T Offline
            T Offline
            TheGreatAndPowerfulOz
            wrote on last edited by
            #5

            if you had a status object with an Exception field, then you'd test for an exception as follows:

            if (status.Exception != null) // oops, something bad
            {
            ...
            }

            alternatively, you could have a "computed" property Success as follows

            public class Status
            {
            ...
            public Exception Exception { get; set; }
            ...
            public bool Success { get { return Exception == null; } }
            ...
            }

            then use it as follows:

            if (status.Success) // cool, all good!
            {
            ...
            }

            If your actions inspire others to dream more, learn more, do more and become more, you are a leader.-John Q. Adams
            You must accept one of two basic premises: Either we are alone in the universe, or we are not alone in the universe. And either way, the implications are staggering.-Wernher von Braun
            Only two things are infinite, the universe and human stupidity, and I'm not sure about the former.-Albert Einstein

            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