Results 1 to 6 of 6

Thread: returning true or false [RESOLVED]

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    returning true or false [RESOLVED]

    Are these the same? which is preferred?

    VB Code:
    1. public Function SomethingToDo() as Boolean
    2.  
    3. try
    4.    trying something
    5.  
    6. catch
    7.    Error
    8.    Return False
    9. end try
    10.  
    11. Return True
    12.  
    13. End Function

    VB Code:
    1. public Function SomethingToDo() as Boolean
    2.  
    3. try
    4.    trying something
    5.  
    6. catch
    7.    Error
    8.    Return 0
    9. end try
    10.  
    11. Return 1
    12.  
    13. End Function

    Just curious if returning 0 or 1 is more accepted among coder's. I am pretty sure there is no difference but please correct me if I'm wrong.

    Oh yeah, am I using the boolean functions in the 'intended' way? to check whether or not a function was successful?
    Last edited by Andy; Apr 19th, 2004 at 08:51 AM.

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    I use True or False, easier to read. Maybe C programmers are more accustomed to 0 or 1.
    You're using a boolean function ok to indicate whether the function succeeded. It's basically a sub that gives an indication of whether or not it ran ok. You're not really returning a value that's used for more processing in the calling sub.
    You could also have a sub that checks something (maybe a value in a database, whether a file exists, if a number is odd or even, etc) that returns True if the number is odd, the file exists, etc, False otherwise. The True/False value isn't determined by whether the function raised an exception.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    ok. What I did initially was create a sub that extracted data from a data base. I realized that if the data didn't exist, I got an error. So, I turned the sub into a boolean function and in the catch block, returned false.

    I call it by saying
    VB Code:
    1. if dosomething() then 'if the function reurns true, keep moving
    2. do more
    3.  'if the function returns false, stop and exit
    4. end if

    That works fine actually. In my case, to check whether or not there is data to extract, should I go for a different method or is what I've done perfectly "legal"?

    I figured I killed two birds with one stone: run the function and if it croaks, no biggie because that indicates that the data wasn't there so the function itself actually checks for true or false.

    While this works, I'd still like to do it the "proper" way so that when my app grows, scaling won't be an issue.

  4. #4
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    You can do that. Ideally, you'd want to deal with the error in the function. No existing data may throw an error, but it could be thrown for some other reason as well (lost connection, e.g.), and you wouldn't know that. You could examine the exception thrown and take some action based on that. If you know an error can, or will, occur at some point, you should probably code to handle it, something like

    If ds.tables.rows.count < 0 then
    crylikeababy()
    End if

    or

    catch ex as sqlexception
    processsqlexception()
    catch ex as someotherexception
    processsomeotherexception

    Your way's ok, but except for trivial code probably wouldn't be considered "professional"

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    I'd like to learn more about this. Where are some good threads or sites of tutorials to read?

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Well , if it's a matter of preference then APIs use integral value to return true and false . I think in .NET it can work the same way .

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