Parameter passing to a method 
Author Message
 Parameter passing to a method

Hi,

perhaps my question is stupid, but I don't see the solution, when trying
to learn RealBasic.

Problem: I want to change the code in StaticFields from global methods.

Situation:
Let's assume I've five windows (W1-W5), each one with an *equal named*
StaticField (F).

Accesing the field would be

        W1.F.Text = "Text to appear"

That's clear, but how to access the field by an global method, when the
parameter should be the window or the name of the window.

        Method (w as window)
                w.F.Text = "Text to appear"
        end

won't work.

Or with other words: What's the easiest way to compose the objects name
by passing the windows name/reference.

thx for help
Andreas



Sun, 26 Sep 2004 17:15:34 GMT  
 Parameter passing to a method
Andreas:
Is this what you need?

1. In the window you can have a button with action:
     setfield(self)

2. In the global module you have:

sub setfield(w as window)
  dim i as integer
   For i = 1 to w.ControlCount
    If w.control(i) isa StaticText and StaticText(w.control(i)).name =
"statictext1"  then
      StaticText(w.control(i)).text = "Any Text"
    end if
  next
end sub

3. If you want it to work for different fields you can send the
fieldname as a parameter. You can do it for all windows in the following
way:

Dim i as Integer
Dim WindowTitle as String
If WindowCount > 1 then
 For i=0 to WindowCount-1
   // Do something like, e.g., setfield(window(i))
 Next
End If

I hope this helps. Ask again if you have problems. I don't know very
much myself so I learn by studying other guy's problems.

Boris



Sun, 26 Sep 2004 17:49:24 GMT  
 Parameter passing to a method

Quote:

> 1. In the window you can have a button with action:

Not what I need, is an automatic action depending on the contents of a
field (calculation).

Quote:
> 2. In the global module you have:
> 3. If you want it to work for different fields you can send the

OK, withj slithly modifications they are doing what I need.

But I've still a question:
1.) A field is an object.
2.) An Object has a name.
3.) The name of the object is well known, just the window changes.
(In my example: w1.F.Text, w2.F.Text.....)

I thought there is a way to pass a name of the window as a refrence and
with this reference I can access to the objects of the window. Your
solution 2 is searching for the name of the object which I already know.
I just need the possibilty to tell it.

Quote:
> I hope this helps. Ask again if you have problems. I don't know very

Anyhow if there is no way as I thought I'll do it in the proposed way.
Perhaps you are right, since I've found nothing about my problem in the
doku and the book from Matt Neuburg

Thx

Quote:
> much myself so I learn by studying other guy's problems.



Sun, 26 Sep 2004 20:29:52 GMT  
 Parameter passing to a method

Quote:

> Accesing the field would be

>         W1.F.Text = "Text to appear"

Yup, that'll work where W1 is the specific window type, or a variable
declared to be that window type.

Quote:
> That's clear, but how to access the field by an global method, when the
> parameter should be the window or the name of the window.

>         Method (w as window)
>                 w.F.Text = "Text to appear"
>         end

This doesn't work because w is just a plain old Window, and Windows
don't have any controls.  You can create a window and add controls, but
then it's a MyCoolWindow, or a W1, or whatever you named it.  It is
also a Window, but remember that if you treat it as a Window, you only
get properties/methods the base Window class has.

If you have multiple window types called W1 through W5, there's no easy
way to do this.  The quickest way in this case would be to modify your
method to look like this:

Sub Method(w as Window)
  if w isa W1 then
    W1(w).F.text = "something"
  elseif w isa W2 then
    W2(w).F.text = "something"
  elseif w isa W3 then
    ...  I think you get the idea
  end if
End Sub

Of course, a more object-oriented approach would be to have each of
these windows "register" the StaticText object with whatever class
contains your method, so that it doesn't have to know anything about
what windows contain that object.



Mon, 27 Sep 2004 00:16:56 GMT  
 Parameter passing to a method

Quote:

> Problem: I want to change the code in StaticFields from global methods.

> Situation:
> Let's assume I've five windows (W1-W5), each one with an *equal named*
> StaticField (F).

> Accesing the field would be

>         W1.F.Text = "Text to appear"

> That's clear, but how to access the field by an global method, when the
> parameter should be the window or the name of the window.

>         Method (w as window)
>                 w.F.Text = "Text to appear"
>         end

> won't work.

No, it won't, because your five different windows are different
classes.  The fact that they happen to each have a static text with the
same name is just a coincidence and of no interest to the compiler.
Remember, RB is a strongly typed, fully compiled language, not a
scripting language.

You'd have to do something like this:

  if w IsA W1 then
     W1(w).F.text = "text to appear"
  elseif w IsA W2 then
     W2(w).F.text = "text to appear"
  // etc.
  end if

This works because you're typecasting w (a generic window reference) to
a more specific type of window, and that more specific type *does* have
an element "F" in a meaningful way.

But the fact that you're having to do something like this indicates a
suboptimal design.  Consider passing in the static text instead, or
passing in some object which represents the data, and code up each
window to make its display match that object.

HTH,
- Joe

--
,------------------------------------------------------------------.
|    Joseph J. Strout         Check out the Mac Web Directory:     |

`------------------------------------------------------------------'



Mon, 27 Sep 2004 00:41:53 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. OCXs: Parameter passing and calling method...

2. Retrival of parameters passed by POST method

3. Passing Parameters to an OCX method

4. parameter passing methods

5. Passing parameters to methods

6. Python scope and parameter passing method

7. Passing a parameter Rexx to JCL and return a parameter to JCL

8. language rules question: using a (passed in) parameter to define a parameter

9. Methods as parameters to methods?

10. Are method args pass-by-value or pass-by-reference

11. APL+Win and array parameter passing from OCX

12. passing parameter from awk back to korn shell

 

 
Powered by phpBB® Forum Software