Results 1 to 5 of 5

Thread: please answer this question

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 1999
    Location
    Perth, WA
    Posts
    35

    Post

    I know they are the fundamental parts of a program and this is gunna sound rather stupid, but can someone explain subs and functions and the private keyword

    i dont understand how they are used

    please answer

  2. #2
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111

    Post

    A sub does functions but you can't input objects when in use i.e.
    Code:
    Public Sub Stuff()
    Text1.Text = "Some stuff."
    End Sub
    with functions:
    Code:
    Public Function Stuff(TxtBox As TextBox, Txt As String)
    TxtBox.Text = Txt
    End Function
    
    then in a command button put:
    
    Stuff Text1, "Some Stuff."
    here it takes Text1.Text = "Some Stuff."
    Private is for the Dimmed object or sub/function etc. so it can only be used by that module/form etc.
    Public is the oppisite.


    ------------------
    DiGiTaIErRoR
    VB, QBasic, Iptscrae, HTML
    Quote: There are no stupid questions, just stupid people.

  3. #3
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670

    Post

    Subs and Functions are pretty similar - the main difference being a function can return information.

    For example;

    Sub MySub(InputStuff as String)
    .. process InputStuff in some way...
    End Sub

    This would be called by;

    MySub [inputstuff]


    A function example;.
    Function MyFunc(InputStuff as String) As String
    .. process InputStuff in some way...
    MyFunc=[returned information]
    End Function

    In code;

    Dim Returned as String
    Returned=MyFunc([input stuff])


    Use the Private keyword before either a sub or a function to keep it 'private' to the module or form - ie, you cannot 'see' it or run it from another form or module.


    Example; To centre a form (note that we don't need any returned information so we use a sub)

    Sub CentreWindow(ThisForm as Form)
    ThisForm.Left=(screen.width-thisform.width)/2
    thisform.top=(Screen.height-thisform.height)/2
    End Sub

    So, if you wanted to centre a form you could then call it like this;

    CentreWindow [form name]

    Example 2; To return the version number of the program (we need a returned value so we use a function) However, we don't need to send any information (Arguments) to the function.

    Function GetVer() As String
    GetVer = App.Major & "." & App.Minor & "." & App.Revision
    End Function

    In the code I might use it;

    lblVersion.Caption = GetVer


    Hope this helps.


    ------------------
    Mark "Buzby" Beeton
    VB Developer
    [email protected]



  4. #4
    Lively Member Maartin's Avatar
    Join Date
    Jan 2000
    Location
    Benoni, Gauteng, South-Africa
    Posts
    99

    Post

    Very good explanation Buzby.

    Only one thing i think the last bit should read.
    Example; To centre a form (note that we don't need any returned information so we use a sub).

    Public Sub CentreWindow(ThisForm as Form)
    ThisForm.Left=(screen.width- thisform.width)/2
    Thisform.top=(Screen.height-thisform.height)/2
    End Sub

    So, if you wanted to centre a form you could then call it like this (in Private or Public use);

    CentreWindow [form name]

    Example 2; To return the version number of the program (we need a returned value so we use a function) However, we don't need to send any information (Arguments) to the function.

    Function GetVer() As String
    GetVer = App.Major & "." & App.Minor & "." & App.Revision
    End Function

    In the code I might use it;

    lblVersion.Caption = GetVer


    [This message has been edited by Maartin (edited 02-03-2000).]

  5. #5
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670

    Post

    In the module they come from they are both Public but I was just demonstrating the use of Sub and Function and removed the Public bit to avoid confusion.

    Thanks for the compliment anyway!!



    ------------------
    Mark "Buzby" Beeton
    VB Developer
    [email protected]



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