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