|
-
Dec 28th, 2003, 04:36 AM
#1
Thread Starter
New Member
Sub inside a Sub?
Is this possible?
I'm assuming it is....this is what I want to do.
In one of my forms, I have a grid of textboxes.
Sub Init() zero's all of my textboxes for a fresh start when the user first uses the program.
When they click File...New...my code is
Private Sub mnuFilenew_Click()
'User wants new file
Dim Response As Integer
Response = MsgBox("Are you sure you want to start a new file?", vbYesNo + vbQuestion, "New File")
If Response = vbNo Then
Exit Sub
Else
Call Init
End If
End Sub
What I REALLY want to happen is for it to zero all the textboxes except for whats in 2 columns. (40 textboxes total to be left alone, both columns are array's with may make it easier)
What I was thinking is changing my Sub Init() section to have little private sub's inside of it, (if thats possible, it would be easiest), so that when the user first runs the program he/she still gets zero's in all the textboxes, but when he/she clicks file/new, they get the whole form zero'd except for those 2 columns.
Any ideas would be appreciated
~scratching my head~
~ Hardest working newbie on the net ~
-
Dec 28th, 2003, 06:35 AM
#2
VB Code:
Sub Init (Optional FileNew As Boolean = False)
If FileNew Then
'do thing 1
Else
'do thing 2
End If
End Sub
And now you can call
Init
or
Init True
depending on which kind of Init you want to run. It shouldn't be too hard to make it work optimally/with least possible code
-
Dec 30th, 2003, 02:58 AM
#3
The "GoSub" statement to some extent would give you the capability of having a "sub inside of a sub". "Back in the day" (i.e. the old days of BASIC), it was the only way you could provide structure to a program. Nowadays, it's use has fallen out of favor, and I believe they dropped support for it in .NET. MS discourages its use because it can cause stack issues. I am generally OK with it and have used it on occasion, but not with great regularity. It basically allows you to execute a set of statements (useful if the set of statements needs to be executed more than once) within a Sub or Function. It is not a true "Sub" or Function in that you can't pass parameters to it or get a return value from it - it basically let's you branch to a set of statements and then lets you return from where you came.
Following is a write-up on it I did some years back:
The GoSub Statement
In earlier versions of BASIC, the only way you could make your programs modular was to break your program up into subroutines (similar to paragraphs in COBOL) and use GoSub to execute that subroutine and return back to the calling statement (similar to PERFORM in COBOL). GoSub was included in VB to maintain backward compatibility; it is not a particularly "bad" construct to use, but its use is generally discouraged – it is recommended that Sub and Function procedures be used instead.
The rules for using GoSub are as follows:
• The destination of a GoSub statement must be a line label or line number that resides within the same Sub or Function procedure as the statement that issues the GoSub (i.e., you cannot "GoSub" to a line in a Sub or Function other than the one you are currently in). In effect, using GoSub lets you have "subs within a sub".
• The name of the line label follows the same rules as that for naming a variable. The line label must begin in the first column of the line and must be followed by a colon( .
• If a line number is used, it must begin in the first column of the line.
• Once the destination of the GoSub is reached, a Return statement will return control to the statement after the one that issued the GoSub.
Following is code to demonstrate the use of Gosub/Return. Note that the use of the Exit Sub statement is required to prevent "falling through". The code int this VB Sub is set up like a mini-COBOL program: the first three GoSubs are like PERFORMs that perform the high-level paragraphs of the program, and the Exit Sub statement is like a STOP RUN. Note: Without the Exit Sub, we would fall through to SubroutineA, and when the Return statement associated with SubroutineA hit, a "Return without GoSub" error would be generated.
Private Sub cmdTryIt_Click()
GoSub SubroutineA
GoSub SubroutineB
GoSub 1000
Exit Sub
SubroutineA:
Print "Hey kids, I'm in Subroutine A"
Return
SubroutineB:
Print "Hey kids, I'm in Subroutine B"
Return
1000
Print "Hey kids, I'm in Subroutine 1000"
Return
End Sub
"It's cold gin time again ..."
Check out my website here.
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
|