|
-
Feb 2nd, 2000, 03:05 PM
#1
Thread Starter
Member
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
-
Feb 2nd, 2000, 03:46 PM
#2
So Unbanned
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.
-
Feb 2nd, 2000, 05:27 PM
#3
Frenzied Member
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]
-
Feb 2nd, 2000, 08:14 PM
#4
Lively Member
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).]
-
Feb 2nd, 2000, 10:04 PM
#5
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|