|
-
Jun 11th, 2005, 12:25 PM
#1
Thread Starter
Frenzied Member
2 questions about VB
Hi guys, I have 2 questions here.
1. How do we comment the codes in multiple lines. I know we use this ' symbol to comment but only one line?
2. When do we know that we are supposed to code up Function and Property?
Your help is highly appreciated.
-
Jun 11th, 2005, 12:29 PM
#2
Banned
Re: 2 questions about VB
There is no way to use the ' on multiple lines :'(
-
Jun 11th, 2005, 12:30 PM
#3
Re: 2 questions about VB
1. VB doesn't have a multiline comment, however there is a Comment Block feature you can use. Right click the Toolbar and make sure you have the Edit toolbar. One of those buttons are called Comment Block (and another Uncomment Block). You can now select several lines and use this command to add/remove comments.
2. I'm not 100% sure what you mean. Do you want to know when it's more appropriate to use a function and when to use property procedures?
-
Jun 11th, 2005, 12:31 PM
#4
Re: 2 questions about VB
 Originally Posted by vbbit
Hi guys, I have 2 questions here.
1. How do we comment the codes in multiple lines. I know we use this ' symbol to comment but only one line?
2. When do we know that we are supposed to code up Function and Property?
Your help is highly appreciated. 
1. There is no method for using bouble or ultiple lined comments.
2. What do you mean by that?
Functions are basically like a sub but they can return values and a property is usually used in custom components 
Cheers,
RyanJ
-
Jun 11th, 2005, 12:35 PM
#5
Re: 2 questions about VB
 Originally Posted by vbbit
2. When do we know that we are supposed to code up Function and Property?
The best answer I, or anyone else, can give for this question is: experience.
-
Jun 11th, 2005, 12:46 PM
#6
Thread Starter
Frenzied Member
Re: 2 questions about VB
Thanks, guys. I think the question 1 is solved by using the comment block from Toolbar menu.
As far as #2, sorry for confusion there. Let me give it another try. When do we know that "oh, that part we should use Property to code up." And "Oh, that part we should use Function to code up." Does this make sense? Like, oh that is command button, so it should be Sub there.
Like this code
Public Function NextEmployeeCode() As String
'Find next available code
Static intEmployeeCode
intEmployeeCode = intEmployeeCode + 1
NextEmployeeCode = Trim(Str(intEmployeeCode))
End Function
Why it's used with Function? Neither Sub nor Property?
Thanks.
-
Jun 11th, 2005, 12:52 PM
#7
Re: 2 questions about VB
because it is returning a value, Public Function NextEmployeeCode() As String
subs dont return values
-
Jun 11th, 2005, 12:53 PM
#8
Re: 2 questions about VB
Because functions return values, and subs don't, unless you are using ByRef in the arguments in a sub, but that's another story. And user created properties are used when you create objects with Class modules, so you don't need to worry bout that.
-
Jun 11th, 2005, 12:54 PM
#9
Thread Starter
Frenzied Member
Re: 2 questions about VB
Thanks, but please don't just focus on that code only. I mean in general. When do we know Function, Property should be coded up?
-
Jun 11th, 2005, 12:55 PM
#10
Re: 2 questions about VB
Well, the example you posted returns a string and as soon as you return anything you wouldn't use a Sub. When it comes to property procedures so are they used in classes and components. Property procedures often comes in pair one Let and one Get. The Get sort of works like a function since it returns a value while you set that value using the Let property procedure.
It can sometimes be hard to know which one to use but in general Properties are used to store some data values while functions are used to calculate a value. If you look at the Caption property for a Label or CommandButton for example, that is a typical property since it used to store the text value that is displayed on the control.
-
Jun 11th, 2005, 12:56 PM
#11
Re: 2 questions about VB
Because in the code, you call it with a location to return a value.
VB Code:
newnum = NextEmployeeCode()
This returns the nexxt number in to the newnum variable.
VB Code:
Option Explicit
Private Sub Form_Load()
Dim x As Boolean
x = test
MsgBox x
End Sub
Function test() As Boolean
test = False
End Function
If it didn't return a value, it would be a sub, and you would call it like this.
Call NextEmployeeCode()
or this:
VB Code:
Option Explicit
Private Sub Form_Load()
Dim x As Boolean
Call test
End Sub
Sub test()
Unload Me
End Sub
-
Jun 11th, 2005, 01:03 PM
#12
Re: 2 questions about VB
Let's give another example. Let's say you're writing a Notepad clone application, and deside to write a class module for each document. A document class could for example have a FileName property, that's a good example of a property however you might want this property to be read-only since it's stored when you open or save a document. You could then create methods (methods are either Sub or Functions within a class) for opening or saving the document, maybe call them OpenDoc and SaveDoc for example. These would execute an action (read or write to the disk) so these are not good examples of properties, they don't have an obvious return value either (but you might return True or False for success or failure). You would then pass the file name as an argument to these methods. The methods then populates the read-only FileName property that you in the application only would read.
Now other properties that could be useful for this class could be a Dirty property (it could of course be called something else) that simply returns True if the document has changed without being saved. Again this is pure data and it simply returns or sets a value no other actions is done. This document class would of course also contain a Text property which would be the content of the document itself.
-
Jun 11th, 2005, 01:15 PM
#13
Thread Starter
Frenzied Member
Re: 2 questions about VB
Thanks, man. I like this sentence "Properties are used to store some data values while functions are used to calculate a value." This does make sense to me in addition to your example about that notepad.
Many thanks to all of you that tried to help me. Have a great day!
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
|