Results 1 to 5 of 5

Thread: [RESOLVED] Best way to tell that the function failed

  1. #1

    Thread Starter
    Hyperactive Member Cyb3rH4Xter's Avatar
    Join Date
    May 2009
    Location
    Sweden
    Posts
    449

    Resolved [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

  2. #2
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    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.
    W o t . S i g

  3. #3

    Thread Starter
    Hyperactive Member Cyb3rH4Xter's Avatar
    Join Date
    May 2009
    Location
    Sweden
    Posts
    449

    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).

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Best way to tell that the function failed

    Quote Originally Posted by Cyb3rH4Xter View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member Cyb3rH4Xter's Avatar
    Join Date
    May 2009
    Location
    Sweden
    Posts
    449

    Re: Best way to tell that the function failed

    Quote Originally Posted by jmcilhinney View Post
    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
  •  



Click Here to Expand Forum to Full Width