|
-
Apr 13th, 2004, 02:41 PM
#1
Thread Starter
Frenzied Member
returning true or false [RESOLVED]
Are these the same? which is preferred?
VB Code:
public Function SomethingToDo() as Boolean
try
trying something
catch
Error
Return False
end try
Return True
End Function
VB Code:
public Function SomethingToDo() as Boolean
try
trying something
catch
Error
Return 0
end try
Return 1
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.
-
Apr 13th, 2004, 03:26 PM
#2
Frenzied Member
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.
-
Apr 13th, 2004, 04:02 PM
#3
Thread Starter
Frenzied Member
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:
if dosomething() then 'if the function reurns true, keep moving
do more
'if the function returns false, stop and exit
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.
-
Apr 13th, 2004, 05:02 PM
#4
Frenzied Member
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"
-
Apr 13th, 2004, 05:51 PM
#5
Thread Starter
Frenzied Member
I'd like to learn more about this. Where are some good threads or sites of tutorials to read?
-
Apr 13th, 2004, 07:55 PM
#6
Sleep mode
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|