|
-
Nov 5th, 2003, 10:57 PM
#1
Thread Starter
Hyperactive Member
Functions or subs?
When writing vb.net classes, I'm told that the only "things" that should be accessed by "the public" are properties and methods.
ie there shouldn't be any
Public Sub MySub()
declarations in a class. There might be private sub's within a VB class but the theory explained to me is that any public method should return a success/fail flag indicating that only public functions should be used (along with property set/get).
Is this best practice?
Do ppl use Public Subs? Is that considered bad form?
Is it better form for every method to be in the form of a public sub with parameters (including error codes). If a sub needs to return anything, it does so via parameter or by setting a property that can be read after calling the actioning sub? If that were so, then Public Functions wouldn't be needed within a class?
I bet someone will say "depends on the problem".
Last edited by Foxer; Nov 5th, 2003 at 11:20 PM.
Rate my response if I helped
Go Hard Or Go Home
-
Nov 6th, 2003, 12:11 AM
#2
I don't know if this is just a symantical error but -'When writing vb.net classes, I'm told that the only "things" that should be accessed by "the public" are properties and methods.' - the term methods applies to functions and subs.
I use public subs all the time even the framework does, I've never heard that it was bad either. Where did you hear this?
-
Nov 6th, 2003, 12:22 AM
#3
Frenzied Member
I think he may be mixing them up. Functions, Subroutines, Subs and Methods all the same.
-
Nov 6th, 2003, 12:24 AM
#4
Thread Starter
Hyperactive Member
If one takes the opinion that every call should return a success/fail flag as a minimum, one can argue that there is no place for Public Subs in classes.
This guy argues that only Public Functions should be used that :-
- receive parameters
- modify properties internal to the class
- return a success/fail flag
You can do all that with a Sub and parameters but functions are designed expressly for this purpose.
Is his arguement correct? Are public subs redundent if you take this line of thinking?
Code:
Public Class MyClass
Public Sub MySub (byVal inputparam, byVal Succeed as boolean)
...
End Sub
Public Function MyFunction (byVal inputparam) as boolean
'Returns success/fail flag
...
End Sub
End Class
Am I wrong to use MySub when MyFunction is "better"?
Last edited by Foxer; Nov 6th, 2003 at 12:33 AM.
Rate my response if I helped
Go Hard Or Go Home
-
Nov 6th, 2003, 12:29 AM
#5
Yes IF you follow his thinking then it would make Subs redundent, but why make everything return a Fail/Success bit? As I stated before this is not the pattern that the .NET framework itself follows.
-
Nov 6th, 2003, 12:55 AM
#6
PowerPoster
And, if you implement the sub right, it should work. Now, if there is an error in the sub, you should be throwing an exception.
The normal expectations of a sub is that a code block is executed completely and successfully. If it isn't successful, it should throw an exception. This eliminates the need to return a true/false value. This is why when you call System.IO.File.Delete("myfile.txt"), the file is deleted. Now, if there was an error deleting the file, and IOException is thrown.
It is is up to the user of your sub to check for errors, but it is you that is supposed to throw exceptions when something isn't right.
-
Nov 6th, 2003, 12:57 AM
#7
PowerPoster
The sub in this:
Code:
Public Sub MySub (byVal inputparam, byVal Succeed as boolean)
...
End Sub
Public Function MyFunction (byVal inputparam) as boolean
'Returns success/fail flag
...
End Sub
in my opinion is not good. If you want to know success or failure, you should use the function way. Using arguments as output variables are not looked good upon by my development team. Of course, there are times when you need them (that is why they are there), but generally there is a better way to do things.
-
Nov 6th, 2003, 01:54 AM
#8
Thread Starter
Hyperactive Member
Ta HellsWraith.
I tend to agree with you. There are oodles of ppl using Public Subs in classes and this guy reckons its bad form. Phooey to him I reckon.
Rate my response if I helped
Go Hard Or Go Home
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
|