I All
I have the following conversion problem. In TrueBasic (an old structured Basic) nested routine were allowed. Look, for instance, the the following
Module main
Sub XXX()End Module In the above fragment the variable "a" and also "b" are seen from both XXX and ZZZ( this last is the nested procedure) but not from YYY. I do not succeed in reproducing this behavior in VB.net but I know that with all that power ( class, structure , module,etc) it is surely possible. Please, can somebody help me thank in advance Federico
Dim aEnd Sub Sub YYY()
Sub ZZZ()
Dim b
End Sub
End Sub

migration from TrueBasic
gxo
To answer the OP question: yes it can be done. You can encapsulate the functionality within classes.
The reason the origional language used this nested subroutine method was to provide encapsulation - it's unneccessary, today, since we have object orientation and fine control of encapsulation (whic by no means eliminates the need for straight-forward functions...).
For example (note this simply replicates the functionality defined in the OP - hopefully...):
Module < xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />Main < xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
Public Class SubFunction
' This var is only visible
' to This Class and any Sub classes
Private Shared A As Integer
Public Shared Sub XXX()
' Call the Embedded function
Call Embedded.ZZZ()
End Sub
Private Class Embedded
' This Var is not exposed
' in any way
Private Shared B As Integer
Public Shared Sub ZZZ()
' This can ONLY be called from
' SubFunction
B = 55
A = 6
End Sub
End Class
End Class
Sub YYY()
' No variables are exposed
SubFunction.XXX()
End Sub
End Module
YorickPeterse
ThePatrickP
thank you very much
it works
Federico
WolfgangEngel
OK
Well first of all the variables declared at the top are not Global, they are member variables or form level variables or module level variables and possibly several other names I've forgotten. They are declared with Dim which makes them Private so they can be accessed only from anywhere within that class.
In Sub1 you have declared two local variables with the same names as the member variables but that does not mean they are the same variables - they are not. These variables can only be accessed from within Sub1
In Sub2 you have declared two parameters with the same names as the member variables. Parameters, delcared ByVal, behave in the same way as local variables with the major difference that they are set when the procedure is called by supplying arguments in the procedure call.
Your confusion has arisen because you used breakpoints to determine what was going on. If you pause the program in the middle of Sub1 or Sub2 and then point to the variables a and b you will be told the value of that variable in the current sub. This applies even if you point at the member variables at the top or even to the variables in the other sub, which at the time are not even in scope so have no value. This is a peculiarity of the debugger which you need to be aware of.
I have modified my timer routine to show more clearly what is going on. I strongly suggest you try it out. Paste this lot into a wide form with a button and an enabled timer. It is based on your code but without the comments.
Public Class Form1
Dim a As Integer = 4
Dim b As Integer = 5
Dim MeText As String
Public Sub Sub1()
Dim a As Integer = 6
Dim b As Integer = 7
MeText = "Sub1 variables: a = " & a.ToString & ", b = " & b.ToString
MsgBox("a= " & a.ToString)
MsgBox("b= " & b.ToString)
MeText = ""
End Sub
Public Sub Sub2(ByVal a As Integer, ByVal b As Integer)
MeText = "Sub2 variables: a = " & a.ToString & ", b = " & b.ToString
MsgBox("a= " & a.ToString)
MsgBox("b= " & b.ToString)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call Sub2(2, 3)
Call Sub1()
MsgBox("a= " & a.ToString)
MsgBox("b= " & b.ToString)
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Static count As Integer
If count = 0 Then Me.Text = ""
count += 1
Me.Text = MeText & " Member variables - a = " & a.ToString & ", b = " & b.ToString
End Sub
End Class
vkmurari
Hi,
'This may be a bad example but i hope it highlights some issues 'GLOBAL VARIABLES a and b set to 4 and 5 respectively.Dim a As Integer = 4
Dim b As Integer = 5
Public Sub Sub1()
Dim a As Integer = 6
Dim b As Integer = 7
MsgBox("a= " & a.ToString)
MsgBox("b= " & b.ToString)
End Sub
'Notice when this Sub is called the values 2 & 3 are passed to a & b. Public Sub Sub2(ByVal a As Integer, ByVal b As Integer)MsgBox("a= " & a.ToString)
MsgBox("b= " & b.ToString)
End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Call Sub2(2, 3) ' GLOBAL variables a gets assigned 2 , b gets assigned 3 'As soon as Sub2() exits they revert back to 4 and 5 respectively. Call Sub1() ' a gets 6 and b gets 7 'As soon as Sub1() exits they revert back to 4 and 5 respectively.MsgBox("a= " & a.ToString)
MsgBox("b= " & b.ToString)
End SubCheck out the keywords; PRIVATE , PUBLIC and SHARED on.>>
http://msdn.microsoft.com/library/default.asp
Regards,
S_DS
Sergei Dorogin
It certainly is.
The member variables a and b, the local variables a and b in Sub1 and the parameters a and b in Sub2 are all independent.
To see this try adding a timer to your form and the following code. You will then see that the member variables, which are the only ones the timer.tick event can access do not change as you call the other routines.
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Static count As Integer
count += 1
Me.Text = count.ToString & " - a = " & a.ToString & ", b = " & b.ToString
End Sub
rene schrieken
Dave299,
Hi,
Well at least i admit it.... ( LOL ). :-)
I know the two GLOBAL a and b are independent, i don't need a timer to show me.
I put a breakpoint in various parts of this to see what was going on, hence my comments in the code.
How about an example from you then illustrating GLOBAL, PRIVATE and PUBLIC variables then please
I very rarely use any GLOBAL variables myself.
I prefer to use a seperate STRUCTURE or a CLASS or a FUNCTION and pass variables ByVal or ByRef.
I rarely use ByRef too. I occasionally use the OPTIONAL keyword.
Regards,
S_DS