Scope

Coding + IDE + Code-Editor + Syntax + User-Functions + Anything else

Scope

Postby Henning » Mon Jul 18, 2011 12:12 am

Hi,

The main reason for this is that I've run into inconsistent behavior in my app.
My opinion on Scope. Plz argue your opinion. :idea:

Scope, or "the lifetime of variables, constants, subs, functions and UDT's (User Defined Types)".

    Global.QObject:
    Public Dim
    Public Const
    Public Sub
    Public Function
    Public UDT variable
    For Application-wide access by Name.

    Any Module (not sure about Forms...):
    Global Sub
    Global Function
    Access from anywhere by Name
    IMO. (I have to think/test a little more on Global Dim, Global Const and Global UDT variables. Their natural 'home' should be in Global.QObject as Public. But again for readability...)
    The only difference btw Global Sub/Function and Public Sub/Function is that Global is accessable by Name from anywhere, just as if declared Public in Global.QObject.
    The reason I once requested it is code readability. Keeping Subs/Functions called from more than one Form/Module easily accessible in one place (Module).

    Any Form/Module:
    Public Dim
    Public Const
    Public Sub
    Public Function
    Public UDT variable
    Access by Name within the same Form/Module
    Access from anywhere by Form/Module-name.Name

    Dim
    Const
    Sub
    Function
    UDT variable
    Access only within same Form/Module by Name

    IMO Global or Public inside Subs/Functions shoul generate a compiler error.

    Why I say UDT variable:
    I never use, or have seen, except in very simple apps Public Type MyType.
    I use them in Global.QObject as:
    Type MyType
    ID As Integer
    Name As String
    Address As String
    End Type

    Public Dim inType As MyType
    Public Dim outType As MyType

    Access from anywhere as inType.ID.

Just ignore this if you don't care ;)

/Henning
Henning
 
Posts: 523
Joined: Fri Aug 13, 2010 6:29 pm

Re: Scope

Postby berndnoetscher » Mon Jul 18, 2011 9:45 am

Looks good.

Except for globals you may also use "Access from anywhere by Form/Module-name.Name" even there are private.
berndnoetscher
Site Admin
 
Posts: 344
Joined: Thu Mar 25, 2010 9:57 am

Re: Scope

Postby Slowdown » Mon Jul 18, 2011 7:29 pm

Hi,
Just ignore this if you don't care

Can't ignore this because i'm fighting with this (perhaps i don't understand it correctly so don't hit me)
Question 1
Why must Public be used if placed in Global ?
As the word Global say's it should be used from everywhere in the code without the need to use Public.
Question 2
Why use Public or Global in a module ?
If i go for readability than i choose for ModuleName.sub/function/variable.
why, if something is going wrong debugging will be a lot more easy than looking for a declared variable 'somewhere'

When i use modules all subs/functions and variables are declared not Public and/or Global.
Even declared as private the can be used as module[dot]. . . . .
At this way i keep it readable for myself.
But again this is my way of doing things and yes must learn a lot of things but that is the fun ;)
Kind regards,
Slowdown

OSX 10.8.x, VBox Xp, Ubuntu 32/64 bit, EOS 64 bit
Slowdown
 
Posts: 465
Joined: Sat May 01, 2010 7:11 pm
Location: Netherlands

Re: Scope

Postby Henning » Mon Jul 18, 2011 11:28 pm

Hi,

Why must Public be used if placed in Global ?

Not sure, but the word Public may be a key to visability, and/or easier creating the compiler.

If i go for readability than i choose for ModuleName.sub/function/variable.

Fortunately programming gives a lot of freedom in how to code. If 10 programmers are given the same task, there will be at least 11 different codings... ;)

Even declared as private the can be used as module[dot]. . . . .

This is NOT normal behavior. Private Sub or just Sub should NOT be visible outside the Form/Module it is declared in. The same goes for Form/Module level Dim. Private is supposed to be just that, Private. It should need a Public/Global to be visible outside the Form/Module.

At this way i keep it readable for myself.

And that is what counts, when not member of a team, wich we are not. ;)

/Henning
Henning
 
Posts: 523
Joined: Fri Aug 13, 2010 6:29 pm

Re: Scope

Postby berndnoetscher » Tue Jul 19, 2011 6:54 am

Why must Public be used if placed in Global ?
Not sure, but the word Public may be a key to visability, and/or easier creating the compiler.



Global.QObject is just another class and all classes have the publib/private feature, which means public is accessable outside + private NOT.
If I would change it for Global, that all are automatically public I would add another language rule/exception.

Even declared as private the can be used as module[dot]. . . . .

This is NOT normal behavior. Private Sub or just Sub should NOT be visible outside the Form/Module it is declared in. The same goes for Form/Module level Dim. Private is supposed to be just that, Private. It should need a Public/Global to be visible outside the Form/Module.


I meant that it can be accessed within the same class. Private ones are not accessable from outside.
berndnoetscher
Site Admin
 
Posts: 344
Joined: Thu Mar 25, 2010 9:57 am

Re: Scope

Postby Slowdown » Tue Jul 19, 2011 7:17 pm

I meant that it can be accessed within the same class. Private ones are not access-able from outside.

Code: Select all
In TestModule:
Private Dim TestModPrivateStr As String = "declared as private in TestModule"

In MainWindow:
  Print TestModule.TestModPrivateStr & "\n"
  TestModule.TestModPrivateStr = "New value in MainWindow"
  Print TestModule.TestModPrivateStr & "\n"

Works like a charm :shock:
Also a Private Sub is working on the same way, or is this not what you both mean ?
Fortunately programming gives a lot of freedom in how to code. If 10 programmers are given the same task, there will be at least 11 different codings...

True, Henning and all 11 way's are probably the right way :)

btw B what are you doing here, if i'm right you have a vacation 8-)
Kind regards,
Slowdown

OSX 10.8.x, VBox Xp, Ubuntu 32/64 bit, EOS 64 bit
Slowdown
 
Posts: 465
Joined: Sat May 01, 2010 7:11 pm
Location: Netherlands

Re: Scope

Postby Henning » Tue Jul 19, 2011 10:41 pm

Hi,

In TestModule:
Private Dim TestModPrivateStr As String = "declared as private in TestModule"

In MainWindow:
Print TestModule.TestModPrivateStr & "\n"


This is NOT supposed to work. TestModule and MainWindow are separate Classes. The point in declaring it Private is to hide it from the outside evil world. The only ways to indirectly read/write from/to it should be thru:
Public Property
Public Sub
Public Function.

/Henning
Henning
 
Posts: 523
Joined: Fri Aug 13, 2010 6:29 pm

Re: Scope

Postby berndnoetscher » Wed Jul 20, 2011 7:37 am

Henning wrote:Hi,

In TestModule:
Private Dim TestModPrivateStr As String = "declared as private in TestModule"

In MainWindow:
Print TestModule.TestModPrivateStr & "\n"


This is NOT supposed to work. TestModule and MainWindow are separate Classes. The point in declaring it Private is to hide it from the outside evil world. The only ways to indirectly read/write from/to it should be thru:
Public Property
Public Sub
Public Function.

/Henning


In my version the compiler gives a compilation error with your examle.

@slowdown:
Not on vacation, but working on on Q7Basic and upgrading my house :-)
The rest of my family is on vacation.
berndnoetscher
Site Admin
 
Posts: 344
Joined: Thu Mar 25, 2010 9:57 am

Re: Scope

Postby Henning » Wed Jul 20, 2011 2:06 pm

Hi,

In my version the compiler gives a compilation error with your examle.


And that is the correct bahavior. :!:

/Henning
Henning
 
Posts: 523
Joined: Fri Aug 13, 2010 6:29 pm

Re: Scope

Postby Slowdown » Wed Jul 20, 2011 6:57 pm

In my version the compiler gives a compilation error with your examle.

in that case you ow me a update 8-)

And that is the correct bahavior.

I never said that it was the right behavior, i'm trying to understand the world of 'Gobal', 'Public' and 'Modules' so i tried some code and concluded
that certain things works (perhaps not as it should).
You are indeed correct "private' means private and nothing else so a private declared variable,Sub and function must only be visible in
the module, form declared in.
That is why i posted the code above, i did tested it and it works.
Perhaps i translate the words Global and Public to strickt from English to my native language and is that sometimes my problem ;)
But i'm glad to be here on the forum both you and B are helping me to understand these things.
Have even to omit that i use 99.99% Q7B and nearly left KBasic (sorry Bernd).
@Bernd,
The rest of my family is on vacation.

Party time :D
Kind regards,
Slowdown

OSX 10.8.x, VBox Xp, Ubuntu 32/64 bit, EOS 64 bit
Slowdown
 
Posts: 465
Joined: Sat May 01, 2010 7:11 pm
Location: Netherlands

Next

Return to Questions & Answers

Who is online

Users browsing this forum: No registered users and 0 guests

cron