Results 1 to 13 of 13

Thread: 2 questions about VB

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,170

    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.

  2. #2
    Banned ThaRubby's Avatar
    Join Date
    Apr 2005
    Location
    127.0.0.1
    Posts
    356

    Re: 2 questions about VB

    There is no way to use the ' on multiple lines :'(

  3. #3
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    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?

  4. #4
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: 2 questions about VB

    Quote 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
    My Blog.

    Ryan Jones.

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: 2 questions about VB

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

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,170

    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.

  7. #7
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: 2 questions about VB

    because it is returning a value, Public Function NextEmployeeCode() As String

    subs dont return values

  8. #8
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    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.

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,170

    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?

  10. #10
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    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.

  11. #11
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: 2 questions about VB

    Because in the code, you call it with a location to return a value.
    VB Code:
    1. newnum = NextEmployeeCode()
    This returns the nexxt number in to the newnum variable.
    VB Code:
    1. Option Explicit
    2. Private Sub Form_Load()
    3.   Dim x As Boolean
    4.   x = test
    5.   MsgBox x
    6. End Sub
    7.  
    8. Function test() As Boolean
    9.   test = False
    10. 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:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.   Dim x As Boolean
    5.   Call test
    6. End Sub
    7. Sub test()
    8.   Unload Me
    9. End Sub

  12. #12
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    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.

  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,170

    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
  •  



Click Here to Expand Forum to Full Width