|
-
Jun 27th, 2011, 06:06 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Best way to tell that the function failed
I am building a custom class with some functions. One of the function is a public Arraylist. The question I am thinking about now is how to best return something to the caller that shows that it failed in a specific way.
What is best:
1. Change the function to a public object or something and return whatever I want.
2. Return an ArrayList containing something that shows how and that it failed.
3. Any other solution?
I'm just too tired to think about this myself
-
Jun 27th, 2011, 07:33 PM
#2
Re: Best way to tell that the function failed
1. I don't like this idea, it means any caller has to check the type returned making calling code more cumbersome.
2. I'm not sure I'm too keen on this either. Again the caller has to check the type(s) returned in the returned ArrayList. It's also a bit obtuse.
3. Throw an error describing the specific failure.
4. Return the Arraylist as an out parameter and have the method return an apt type indicating the success or (type of) failure of the call. (like TryParse())
Just out of interest, why an ArrayList? If ultimately the collection contains only one type then one of the generic collections such as List<T> would be more appropriate.
-
Jun 27th, 2011, 08:49 PM
#3
Thread Starter
Hyperactive Member
Re: Best way to tell that the function failed
I actually thought about 3 but I couldn't come up with a good way to do it (for example should I create my own exception or just use one that exist?).
I don't really get what you mean about 4, not thaaat awesome at C# (yet ).
You are so true about the arraylist. I will replace it with a List instead, it is as you said only stuffed with one type anyway (my own created).
-
Jun 27th, 2011, 11:12 PM
#4
Re: Best way to tell that the function failed
 Originally Posted by Cyb3rH4Xter
I don't really get what you mean about 4, not thaaat awesome at C# (yet  ).
You've never used a TryParse method, e.g. double.TryParse? The method returns true or false to indicate success or failure and, if successful, the value is passed back via the second parameter, declared 'out'.
The only other reasonable options would be to return null when it fails or throw an exception.
-
Jun 28th, 2011, 07:54 AM
#5
Thread Starter
Hyperactive Member
Re: Best way to tell that the function failed
 Originally Posted by jmcilhinney
You've never used a TryParse method, e.g. double.TryParse? The method returns true or false to indicate success or failure and, if successful, the value is passed back via the second parameter, declared 'out'.
The only other reasonable options would be to return null when it fails or throw an exception.
Yeah I have actually never used TryParse, I have wondered for some time what the difference between parse and tryparse was But now I see what you mean.
As the function can fail in multiple ways I think the exception bit will be the one that fits the most so I made a custom exception and now everything works as it should. Thx again jmcilhinney (and Milk!)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|