MDI - Using Proc multiple Children or Multiple Calls 
Author Message
 MDI - Using Proc multiple Children or Multiple Calls

I have several public procedure in a module that use static variables.

I would like to call each routine multiple times both from a single child
and/or from multiple children with different parameters.  Currently, because
of the static variables, the return value(s) get corrupted.

Would this procedure work properly if created as a class and then
made part of a collection??

Thanks
David



Thu, 16 Jun 2011 10:52:52 GMT  
 MDI - Using Proc multiple Children or Multiple Calls


Quote:
> I have several public procedure in a module that use static variables.

> I would like to call each routine multiple times both from a single child
> and/or from multiple children with different parameters.  Currently, because
> of the static variables, the return value(s) get corrupted.

> Would this procedure work properly if created as a class and then
> made part of a collection??

Why part of a collection?  Why not new one up and store it in each child?

If I understand your situation, you have several methods that use state-like
variables and are trying to use them from different MDI forms, where each
need to have their own version of those state-like values.

You could either put those methods in a class where each MDI child
would have their own instance with its own unique values.  Or, you
could put those methods in a module and pass in the MDI child where
the state-like variables are stored in the MDI child module.

The first method is like having a chart control on each MDI child, that
needs to display its own data.  Each child gets one, and it has its own
set of data.

The second method is like writing generic routines exposed publicaly
for all to use, much like a CenterMe routine that accepts a form as
a parameter and centers the form on its container.  In this case the
static-like variable are in the form (Left and Top properties).

In both of those cases the state-like variables were pretty much
isolated from eachother.  The data for one doesn't effect any others.
But there might also be the case where you need a state-like
variable for the entire application, for example a public CascadeMe
routine that would accept a form and place it at a certain position
so as to overlap previously cascaded forms.  In that case the
routine has to remember where the previous form went, so it can
correctly position any current form.

So with those in mind, which one best describes your situation?

LFS



Thu, 16 Jun 2011 16:39:01 GMT  
 MDI - Using Proc multiple Children or Multiple Calls
Larry thanks for response.

Currently I do NOT use a base child form and make a copy of it.

Even If I went with a base child, I believe I would have the same problem as
I have the ability to call each proc (that contain the static variables)
multiple times by dynamically adding these procs to a "child" list which
then calls the procs the child wants.

Not sure what passing in the child object to the proc would do to isolate
the static variables to each child,  Even if this isolated the static
variable for the 1st usage, I believe the proc would fail on the second  and
corrupt the 1st because of the dynamic list call.

So the answer to your question I would say is --> generic routines exposed
publicly.  IMHO this is why I have the problem.

========  MAYBE ========
I think I could (???) create a class with one method (the module proc) and
create an instance of the class which would be added to the list to be
called.

I do NOT want (???) one class with all routines as then each child would
bring in every class method, even though i may only use one or two.


Quote:


>> I have several public procedure in a module that use static variables.

>> I would like to call each routine multiple times both from a single child
>> and/or from multiple children with different parameters.  Currently,
>> because
>> of the static variables, the return value(s) get corrupted.

>> Would this procedure work properly if created as a class and then
>> made part of a collection??

> Why part of a collection?  Why not new one up and store it in each child?

> If I understand your situation, you have several methods that use
> state-like
> variables and are trying to use them from different MDI forms, where each
> need to have their own version of those state-like values.

> You could either put those methods in a class where each MDI child
> would have their own instance with its own unique values.  Or, you
> could put those methods in a module and pass in the MDI child where
> the state-like variables are stored in the MDI child module.

> The first method is like having a chart control on each MDI child, that
> needs to display its own data.  Each child gets one, and it has its own
> set of data.

> The second method is like writing generic routines exposed publicaly
> for all to use, much like a CenterMe routine that accepts a form as
> a parameter and centers the form on its container.  In this case the
> static-like variable are in the form (Left and Top properties).

> In both of those cases the state-like variables were pretty much
> isolated from eachother.  The data for one doesn't effect any others.
> But there might also be the case where you need a state-like
> variable for the entire application, for example a public CascadeMe
> routine that would accept a form and place it at a certain position
> so as to overlap previously cascaded forms.  In that case the
> routine has to remember where the previous form went, so it can
> correctly position any current form.

> So with those in mind, which one best describes your situation?

> LFS



Thu, 16 Jun 2011 22:05:51 GMT  
 MDI - Using Proc multiple Children or Multiple Calls


Quote:
> Currently I do NOT use a base child form and make a copy of it.

> Even If I went with a base child, I believe I would have the same problem as
> I have the ability to call each proc (that contain the static variables)
> multiple times by dynamically adding these procs to a "child" list which
> then calls the procs the child wants.

I think a concrete example would better explain the situation.  It would
also help identify if you are using VB6 or VB.Net.  Can you post a small
example to demonstrate what you mean?

And, if you are using VB.Net, you should post to a .Net group as
the different platforms use different algorithms/commands.  This group's
primary focus is on VB6 and prior versions.

LFS



Thu, 16 Jun 2011 22:45:11 GMT  
 MDI - Using Proc multiple Children or Multiple Calls
Using VB5 actually.  Haven't migrated to NET -- from what I've seen of it
appears cumbersome -- and I heard slow  (but can't understand this as all
VB, C#, C compile to same if I'm correct)

==========================

Tough putting together a  complete example as things so interrelated.

Its fairly simple (1) A modal form is called from MDI Menu,  (2)  The list
of all available procedure names as well as parameters are obtained from the
modal form  (3)  Upon exit of the modal form, the required information is
added and saved to a string array.

I don't use AutoRedraw, Paint everything from Paint event -- so in the Paint
event I:

Sub Paint
        Call DoTech
        Call DoDrawing
End Sub

Sub DoTech
'Calculate and load the drawing array with values

   For LBound to UBound

          Grab procedure name and parameters from string array
          Call DoProc (name, parameters)

    Next

End Sub

Sub DoDrawing
      execute the drawing array
End Sub

Hope that gives some insight?


Quote:


>> Currently I do NOT use a base child form and make a copy of it.

>> Even If I went with a base child, I believe I would have the same problem
>> as
>> I have the ability to call each proc (that contain the static variables)
>> multiple times by dynamically adding these procs to a "child" list which
>> then calls the procs the child wants.

> I think a concrete example would better explain the situation.  It would
> also help identify if you are using VB6 or VB.Net.  Can you post a small
> example to demonstrate what you mean?

> And, if you are using VB.Net, you should post to a .Net group as
> the different platforms use different algorithms/commands.  This group's
> primary focus is on VB6 and prior versions.

> LFS



Fri, 17 Jun 2011 02:01:03 GMT  
 MDI - Using Proc multiple Children or Multiple Calls


Quote:

> Tough putting together a  complete example as things so interrelated.

> Its fairly simple (1) A modal form is called from MDI Menu,  (2)  The list
> of all available procedure names as well as parameters are obtained from the
> modal form  (3)  Upon exit of the modal form, the required information is
> added and saved to a string array.

<snipped for brievity>

Quote:

> Hope that gives some insight?

Where do your static variables fit in?

It almost seems like you've made three posts, and described three different
things.  A major portion of finding a solution is to understand the problem....

If I were to take a stab in the dark, you have some drawing commands, and
you gather those into a user generated list from your MDI modal form, and
then use that list to draw on some other MDI child.

If that sums it up, you might think of using a class to hold your list and do
the drawing.  For robustness, you might even keep the list of commands
in that class and make the MDI modal form use it to build its list of
commands for the user.  The class would then contain both the list of
commands, and the code to draw them out when needed (easier to
maintain).

So as your user builds a list of commands, that list is reflected in your
class (as a collection of strings, an array of strings, whatever) and
when its time to bring up the drawing, you create a new MDI form
and simply pass that class to the form.  In its Paint event it calls on
that class to do the drawing.

In that way neither the MDI modal form, nor the drawn form actually
know what the commands are, (nor do they care) the commands are
all contained in the class, built in the class, and drawn from the class.

What I don't see is how your 'static variables' fit into all this....

LFS



Fri, 17 Jun 2011 03:39:07 GMT  
 MDI - Using Proc multiple Children or Multiple Calls

Quote:
> What I don't see is how your 'static variables' fit into all this...

Using my previous SudoCode (below)  the call to DoProc (see arrows) contains
a Select/Case of all the procedures by name.
Each procedure is called from this Select/Case

Consequently, if the procedure contains static variables, when it is called
more than once with different parameters (i.e. in the list twice)
results returned are in error.

 -------- Previous SudoCode -----------

I don't use AutoRedraw, Paint everything from Paint event -- so in the Paint
event I:

Sub Paint
        Call DoTech
        Call DoDrawing
End Sub

Sub DoTech
'Calculate and load the drawing array with values

   For LBound to UBound

          Grab procedure name and parameters from string array

Quote:
>>>   Call DoProc (name, parameters)

    Next

End Sub

Sub DoDrawing
      execute the drawing array
End Sub

===== Response to  Class Suggestion =======

Quote:
> So as your user builds a list of commands, that list is reflected in your
> class (as a collection of strings, an array of strings, whatever) and
> when its time to bring up the drawing, you create a new MDI form
> and simply pass that class to the form.  In its Paint event it calls on
> that class to do the drawing.

So each child would receive a copy of the class with all the procedures
(methods).

But even if I went with a class instead of a common module--
if one of the class methods contains static variables,
a call more than once to that method should error,
 just as if it were in a module
 -- or am I missing something?


Quote:


>> Tough putting together a  complete example as things so interrelated.

>> Its fairly simple (1) A modal form is called from MDI Menu,  (2)  The
>> list
>> of all available procedure names as well as parameters are obtained from
>> the
>> modal form  (3)  Upon exit of the modal form, the required information is
>> added and saved to a string array.

> <snipped for brievity>

>> Hope that gives some insight?

> Where do your static variables fit in?

> It almost seems like you've made three posts, and described three
> different
> things.  A major portion of finding a solution is to understand the
> problem....

> If I were to take a stab in the dark, you have some drawing commands, and
> you gather those into a user generated list from your MDI modal form, and
> then use that list to draw on some other MDI child.

> If that sums it up, you might think of using a class to hold your list and
> do
> the drawing.  For robustness, you might even keep the list of commands
> in that class and make the MDI modal form use it to build its list of
> commands for the user.  The class would then contain both the list of
> commands, and the code to draw them out when needed (easier to
> maintain).

> So as your user builds a list of commands, that list is reflected in your
> class (as a collection of strings, an array of strings, whatever) and
> when its time to bring up the drawing, you create a new MDI form
> and simply pass that class to the form.  In its Paint event it calls on
> that class to do the drawing.

> In that way neither the MDI modal form, nor the drawn form actually
> know what the commands are, (nor do they care) the commands are
> all contained in the class, built in the class, and drawn from the class.

> What I don't see is how your 'static variables' fit into all this....

> LFS



Fri, 17 Jun 2011 10:32:39 GMT  
 MDI - Using Proc multiple Children or Multiple Calls
Just a  thought --

What if I make my own static variables.  In other words
set up a structure, make it an array, and pass in the instance (based on its
appearance in the listbox).  This way each set of variables would remain
static and specific to the order in the list.


Quote:
>> What I don't see is how your 'static variables' fit into all this...

> Using my previous SudoCode (below)  the call to DoProc (see arrows)
> contains a Select/Case of all the procedures by name.
> Each procedure is called from this Select/Case

> Consequently, if the procedure contains static variables, when it is
> called more than once with different parameters (i.e. in the list twice)
> results returned are in error.

> -------- Previous SudoCode -----------

> I don't use AutoRedraw, Paint everything from Paint event -- so in the
> Paint
> event I:

> Sub Paint
>        Call DoTech
>        Call DoDrawing
> End Sub

> Sub DoTech
> 'Calculate and load the drawing array with values

>   For LBound to UBound

>          Grab procedure name and parameters from string array
>>>>   Call DoProc (name, parameters)

>    Next

> End Sub

> Sub DoDrawing
>      execute the drawing array
> End Sub

> ===== Response to  Class Suggestion =======
>> So as your user builds a list of commands, that list is reflected in your
>> class (as a collection of strings, an array of strings, whatever) and
>> when its time to bring up the drawing, you create a new MDI form
>> and simply pass that class to the form.  In its Paint event it calls on
>> that class to do the drawing.

> So each child would receive a copy of the class with all the procedures
> (methods).

> But even if I went with a class instead of a common module--
> if one of the class methods contains static variables,
> a call more than once to that method should error,
> just as if it were in a module
> -- or am I missing something?




>>> Tough putting together a  complete example as things so interrelated.

>>> Its fairly simple (1) A modal form is called from MDI Menu,  (2)  The
>>> list
>>> of all available procedure names as well as parameters are obtained from
>>> the
>>> modal form  (3)  Upon exit of the modal form, the required information
>>> is
>>> added and saved to a string array.

>> <snipped for brievity>

>>> Hope that gives some insight?

>> Where do your static variables fit in?

>> It almost seems like you've made three posts, and described three
>> different
>> things.  A major portion of finding a solution is to understand the
>> problem....

>> If I were to take a stab in the dark, you have some drawing commands, and
>> you gather those into a user generated list from your MDI modal form, and
>> then use that list to draw on some other MDI child.

>> If that sums it up, you might think of using a class to hold your list
>> and do
>> the drawing.  For robustness, you might even keep the list of commands
>> in that class and make the MDI modal form use it to build its list of
>> commands for the user.  The class would then contain both the list of
>> commands, and the code to draw them out when needed (easier to
>> maintain).

>> So as your user builds a list of commands, that list is reflected in your
>> class (as a collection of strings, an array of strings, whatever) and
>> when its time to bring up the drawing, you create a new MDI form
>> and simply pass that class to the form.  In its Paint event it calls on
>> that class to do the drawing.

>> In that way neither the MDI modal form, nor the drawn form actually
>> know what the commands are, (nor do they care) the commands are
>> all contained in the class, built in the class, and drawn from the class.

>> What I don't see is how your 'static variables' fit into all this....

>> LFS



Fri, 17 Jun 2011 11:08:00 GMT  
 MDI - Using Proc multiple Children or Multiple Calls


Quote:
> > What I don't see is how your 'static variables' fit into all this...

> Using my previous SudoCode (below)  the call to DoProc (see arrows) contains
> a Select/Case of all the procedures by name.
> Each procedure is called from this Select/Case

> Consequently, if the procedure contains static variables, when it is called
> more than once with different parameters (i.e. in the list twice)
> results returned are in error.

I guess what I am getting at is, if static variables are going to be a
problem, then don't use them.  There are very few reasons to use a
static variable anyway, which is why I am having trouble seeing how
they fit in.  What are they used for that would make you want to use them?

Quote:
> But even if I went with a class instead of a common module--
> if one of the class methods contains static variables,
> a call more than once to that method should error,
>  just as if it were in a module
>  -- or am I missing something?

There is no 'If'...  To make a variable static, you have to declare
it as static, so you know which are static and which are not.  Again I
would say, don't use them if you don't need them....

LFS



Fri, 17 Jun 2011 20:49:48 GMT  
 MDI - Using Proc multiple Children or Multiple Calls

Quote:
> I guess what I am getting at is, if static variables are going to be a
> problem, then don't use them.  There are very few reasons to use a
> static variable anyway, which is why I am having trouble seeing how
> they fit in.  What are they used for that would make you want to use them?

As with any code sometimes you just need a static.
A simple example would be the calculation of an exponential moving average
where the previous value must be preserved.

So if you want to nest exponential moving averages -- or call the routine
more than once with different parameters, than a problem arises.

I've seen exponential moving average used this way (nesting) in several
programs, just not sure how they accomplish the calculation??


Quote:


>> > What I don't see is how your 'static variables' fit into all this...

>> Using my previous SudoCode (below)  the call to DoProc (see arrows)
>> contains
>> a Select/Case of all the procedures by name.
>> Each procedure is called from this Select/Case

>> Consequently, if the procedure contains static variables, when it is
>> called
>> more than once with different parameters (i.e. in the list twice)
>> results returned are in error.

> I guess what I am getting at is, if static variables are going to be a
> problem, then don't use them.  There are very few reasons to use a
> static variable anyway, which is why I am having trouble seeing how
> they fit in.  What are they used for that would make you want to use them?

>> But even if I went with a class instead of a common module--
>> if one of the class methods contains static variables,
>> a call more than once to that method should error,
>>  just as if it were in a module
>>  -- or am I missing something?

> There is no 'If'...  To make a variable static, you have to declare
> it as static, so you know which are static and which are not.  Again I
> would say, don't use them if you don't need them....

> LFS



Fri, 17 Jun 2011 22:07:47 GMT  
 MDI - Using Proc multiple Children or Multiple Calls


Quote:
> > I guess what I am getting at is, if static variables are going to be a
> > problem, then don't use them.  There are very few reasons to use a
> > static variable anyway, which is why I am having trouble seeing how
> > they fit in.  What are they used for that would make you want to use them?

> As with any code sometimes you just need a static.

I don't buy that.  When you say static, what you imply is state.  While it
maybe easier to code if you allow different states, there will be a way to
go stateless.  In other words, you can find ways to code without using
static variables.

Quote:
> A simple example would be the calculation of an exponential moving average
> where the previous value must be preserved.

A simple solution would be to return the average, and pass it back in
again for the next iteration.

Quote:
> So if you want to nest exponential moving averages -- or call the routine
> more than once with different parameters, than a problem arises.

> I've seen exponential moving average used this way (nesting) in several
> programs, just not sure how they accomplish the calculation??

I'm no mathematician, so I don't have the formula for exponential moving
averages off the top of my head, but here is an example of simple weighting
using one routine to build two different averages:

Private Sub Form_Load()
Dim Y1, Y2, V1, V2, X
  AutoRedraw = True
  ScaleMode = vbPixels
  V1 = Rnd * 50 + 25
  V2 = Rnd * 100 + 50
  Y1 = 200
  Y2 = 400
  For X = 0 To 800 Step 8
    Plot X, Y1, V1, Rnd * 50
    Plot X, Y2, V2, Rnd * 100
  Next
End Sub

Sub Plot(X, Y, V1, V2)
  Circle (X, Y - V2), 3, vbWhite
  PSet (X, Y - V1), BackColor
  V1 = (V1 + V2 + V2) / 3
  Line -(X + 8, Y - V1), vbBlack
End Sub

In that case, the value held in V1 is passed back and forth to
build successive averages.  No static variables required.

There are other ways to do that, but the point is, if you need
a stateless design, then use a stateless design.....

LFS



Sat, 18 Jun 2011 01:41:25 GMT  
 MDI - Using Proc multiple Children or Multiple Calls


Quote:
> I don't buy that.  When you say static, what you imply is state.  While it
> maybe easier to code if you allow different states, there will be a way to
> go stateless.  In other words, you can find ways to code without using
> static variables.

I really should amend that to read there will often be a way to go stateless.
There are a few things (like a mutex) where state is required....

<g>
LFS



Sat, 18 Jun 2011 01:48:29 GMT  
 MDI - Using Proc multiple Children or Multiple Calls

Quote:

> I'm no mathematician, so I don't have the formula for exponential
> moving averages off the top of my head, but here is an example of
> simple weighting using one routine to build two different averages:

> Private Sub Form_Load()
> Dim Y1, Y2, V1, V2, X
>   AutoRedraw = True
>   ScaleMode = vbPixels
>   V1 = Rnd * 50 + 25
>   V2 = Rnd * 100 + 50
>   Y1 = 200
>   Y2 = 400
>   For X = 0 To 800 Step 8
>     Plot X, Y1, V1, Rnd * 50
>     Plot X, Y2, V2, Rnd * 100
>   Next
> End Sub

> Sub Plot(X, Y, V1, V2)
>   Circle (X, Y - V2), 3, vbWhite
>   PSet (X, Y - V1), BackColor
>   V1 = (V1 + V2 + V2) / 3
>   Line -(X + 8, Y - V1), vbBlack
> End Sub

> In that case, the value held in V1 is passed back and forth to
> build successive averages.  No static variables required.

The usual way to calculate a moving average is to use the last N
values, either weighted or unweighted. It seems like what you show
carries all previous values -- it might be called a running average,
but it's not a moving average.

To get a moving average, you have to subtract the oldest of the N
values, then add in the newest. I don't think you could do that
without maintaining some state -- namely, the last N values.

--
        Jim



Sat, 18 Jun 2011 02:48:18 GMT  
 MDI - Using Proc multiple Children or Multiple Calls


Quote:
> > I'm no mathematician, so I don't have the formula for exponential
> > moving averages off the top of my head, but here is an example of
> > simple weighting using one routine to build two different averages:
<...>

> The usual way to calculate a moving average is to use the last N
> values, either weighted or unweighted. It seems like what you show
> carries all previous values -- it might be called a running average,
> but it's not a moving average.

> To get a moving average, you have to subtract the oldest of the N
> values, then add in the newest. I don't think you could do that
> without maintaining some state -- namely, the last N values.

It would seem to me then, that what is needed is an array of values
to be iterated over, such that you have access to all the data points,
and can pick and choose those that you need.  You could then pass
a reference to the entire array indicating what index is the current index.

But if that is not the case, if all the data has not been obtained yet,
some sort of que is needed to keep track of the last N values.  In
that case, you still don't need to keep static values inside the averaging
procedure.  Instead of just passing back and forth the last calculated
average, the routine could also pass back and forth the que of the last
N values.

I'm just saying that if there are a set of procedures that need to be
called by some number of objects, at undetermined times, they
could very possibly be designed to be stateless.  If being stateless
is going to solve a particularly tricky problem, then it would probably
be worth looking into....

LFS



Sat, 18 Jun 2011 03:52:32 GMT  
 MDI - Using Proc multiple Children or Multiple Calls
Stateless -- Interesting concept to pass the return value back to the same
proc for use again.

LETS ME START OVER and POSE MY QUESTION A LITTLE DIFFERENTLY

---------------------------------------------------

Using an exponential moving average (EMA).

Some of the programs I've seen where they've implement something similar use
a nesting

structure for plotting an EMA of

EMA(EMA(Price, Length Avg), Price, Length Avg)

This is what is known as a double smoothed EMA average and the final result
is loaded into an array for plotting.

If the inside loop EMA values (I assume the inside loop value are put in an
array for using with the outer loop ?) then the return value for each inside
loop Must be retained during each iteration of the inside loop.

'FWIW the formula for an exponential moving average is:

' X = (K x (C - P)) + P

' X = Current EMA
' C = Current Price
' P = Previous period's EMA* <<< Static variable
' K = Smoothing constant
' (*A SMA is used for first period's calculation)
' The smoothing constant applies the appropriate weighting to
' the most recent price relative to the previous exponential
' moving average. The formula for the smoothing constant is:

' k = 2 / (1 + N)
' N = Number of periods for EMA

-----------------

QUESTION:
Any ideas how they handle the return value so that:

1) The inner return value is retained for its use?


Quote:


>> > I'm no mathematician, so I don't have the formula for exponential
>> > moving averages off the top of my head, but here is an example of
>> > simple weighting using one routine to build two different averages:
> <...>

>> The usual way to calculate a moving average is to use the last N
>> values, either weighted or unweighted. It seems like what you show
>> carries all previous values -- it might be called a running average,
>> but it's not a moving average.

>> To get a moving average, you have to subtract the oldest of the N
>> values, then add in the newest. I don't think you could do that
>> without maintaining some state -- namely, the last N values.

> It would seem to me then, that what is needed is an array of values
> to be iterated over, such that you have access to all the data points,
> and can pick and choose those that you need.  You could then pass
> a reference to the entire array indicating what index is the current
> index.

> But if that is not the case, if all the data has not been obtained yet,
> some sort of que is needed to keep track of the last N values.  In
> that case, you still don't need to keep static values inside the averaging
> procedure.  Instead of just passing back and forth the last calculated
> average, the routine could also pass back and forth the que of the last
> N values.

> I'm just saying that if there are a set of procedures that need to be
> called by some number of objects, at undetermined times, they
> could very possibly be designed to be stateless.  If being stateless
> is going to solve a particularly tricky problem, then it would probably
> be worth looking into....

> LFS



Sat, 18 Jun 2011 04:02:08 GMT  
 
 [ 23 post ]  Go to page: [1] [2]

 Relevant Pages 

1. How to close multiple MDI child windows

2. ? VB3-Multiple Forms w/o MDI children ?

3. Polling Headers on multiple MDI child forms??

4. Accessing data on multiple MDI Child forms -Please Help

5. Accessing data on multiple MDI child forms?

6. Multiple MDI children

7. Multiple MDI children

8. ? Trapping KeyCodes on MDI App with multiple Children

9. Multiple MDI children

10. Call a proc using a string variable for the proc name

11. Calling MDI child code from MDI parent menu???

12. Multiple MDI parents in VB using VBXes?

 

 
Powered by phpBB® Forum Software