Results 1 to 14 of 14

Thread: Variables Scope (Easy!)

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    178

    Cool

    I know this is a simple question....

    If you declare a public variable in the declarations section you can access it throught the program. Is there any way that you can declare a variable for use in just one particular module?

    Cheers for any help
    Steve

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Sure! Declare the variable in the General Declaration section as Private.
    Code:
    Private iCanOnlyBeUsedInThisModule As Integer
    Public iCanBeUsedEverywhereInTheProgram As Integer
    Good luck!

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    178

    Cool Joacim

    Yeah, that's what I thought too...

    The only reason I asked was because I have just used this technique in a program that I am writing at the moment and it doesn't seem to work !!!

    At least now I know that something else is the cause !!!!

    Cheers Joacim
    Steve

  4. #4
    Junior Member
    Join Date
    Sep 2000
    Location
    Poland
    Posts
    26

    Re: Joacim

    hello,
    what was wrong about your declaration? I'm just wondering...

    Best wishes,
    Syl

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    178

    Question My problem (Programming ones) !!

    I think it is to do with the fact that I am passing a string to a procedure within the module that my private variable is located and technically not naming it within the procedure (If that makes sense).

    Something like

    Module 1

    Call Myprocedure(Mystring)

    Module 2

    Private Mystringhere as string


    Sub Myprocedure(Mystringhere)
    msgbox Mystringhere
    end sub

    Any body else have any input with this...

    steve

  6. #6
    Junior Member
    Join Date
    Sep 2000
    Location
    Poland
    Posts
    26

    Re: My problem (Programming ones) !!

    you are partly right.

    When U have a module scope variable Mystringhere and than U declare
    Sub Myprocedure(Mystringhere)
    msgbox Mystringhere
    End sub

    the msgbox doesn't affect the module variable, but the local one. I want to say that in module 2 U have two different variables with the same name. As far as I know all calls to the variable's name in the procedure call the local variable.

    But I think that's not the reason of your problem.
    Your Myprocedure procedure is private for the module (I guess private is by default, but I'm not sure) and that's why it's invisible in another module.

    When U declare

    PUBLIC Sub Myprocedure(Mystringhere)
    msgbox Mystringhere
    End sub

    should it work properly (I hope).

    Not only variables can be public or private.

  7. #7
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Procedures are public by default, Syl

    If Myprocedure appears as a function property or public variable in a standard module otherwere it's possible that that one is called instead, in that case you should call it by modulename.procedurename ...


    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  8. #8
    Junior Member
    Join Date
    Sep 2000
    Location
    Poland
    Posts
    26

    Smile

    thanks for correcting me. I always use
    public sub...
    or private sub...
    and couldn't remember the default

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Now i forgot the problem sorry,
    Private Mystringhere as string

    Sub Myprocedure(Mystringhere)
    msgbox Mystringhere
    end sub
    The Mystringhere argument in the procedure is procedure scope but the private Mystringhere is modulescope, hope you don't interfere these anyhow, because you shouldn't.

    If you want to change the value of mystringhere, the one with modulescope you pass a different named argument, or you use modulename.mystringhere to access the modulescope variable because the procedure scope will be the default variable to access. Hope i got your point here


    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    178

    Wink Problem fixed

    Thanks for all this,

    Basically the variable passed as the argument into the Myprocedure, forget this I will paste the real code.....

    Public Sub Create_Monthreport()
    Dim Mysheets As Byte, i As Byte, Default_Sheets As Byte

    'Create workbook with required number of sheets and correct names
    With Application
    Default_Sheets = .SheetsInNewWorkbook
    .DisplayAlerts = False
    .SheetsInNewWorkbook = 1
    End With

    Workbooks.Add
    MonthReport = ActiveWorkbook.Name

    With Workbooks(MonthReport)
    ActiveSheet.Name = "MAIN"
    Call Print_Settings(0)
    For i = 1 To 15
    .Sheets.Add After:=Sheets(.Sheets.Count)
    .ActiveSheet.Name = Workbooks(Template_File).Sheets(i + 2).Name
    Call Print_Settings(i)
    Next i
    End With

    With Application
    .DisplayAlerts = True
    .SheetsInNewWorkbook = Default_Sheets
    End With
    Call Copypaste_report
    End Sub


    the Template_File variable is a publicly defined variable in another module which I can access no problem. What I was originally trying to do (And looking back have no idea why !!!) was pass this public variable to the create_monthreport procedure. My problem was that I also wanted to use this variable within the same module as the Create_monthreport (but obviously in another procedure), I thought that declaring the passed argument as private would allow me to do this..... and that is how my discussion started..... What I have done now is to just use the public defined variable and not pass any arguments.

    God knows why I didn't do this originally (Mind block or something !!!)

    Cheers all anyway
    Steve

    P.S. How do I put the code in so it is formatted like in VB (Is is Code tags or something)

  11. #11
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    [code]
    You type the code here
    [/code]

    note i used bold [ so that it doesn't interpret them as code tags like this:
    Code:
    
    
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    178

    Thumbs up Cheers

    Thanks for that,

    Steve

    lets test it

    Code:
    Dim Mycode is now formatted as string
    and the bold technique....

    [code]

    Cool....

  13. #13
    Guest
    I wish I understood the code thing...

    Code:
    Public Function Fun(S as string) as string
       Fun=StrRev(S)
    End Function
    Do I understand the smilies yet ?

    And what about the bold thing?
    underlined and italic

    OK, I'm learning... Gimme another minute....
    A url: www.limburg.demon.nl
    and another type of url: My Homepage

    Bullet lists:
    • <-Cool a bullet
    • <-.... and another one!


    Numbered lists
    [list=1][*] First Item[*] Second Item[/list=1]

    And Another list:
    [list=A][*] I'm gettin' the hang o'it![*] Yeah![/list=A]

    Now for the big show: A Quote:
    P.S. How do I put the code in so it is formatted like in VB (Is is Code tags or something)
    OK, learned them all... Only the [img] tag doesn't work... bummer....
    Sorry for disturbing your posts.... But I'm soooooo happy now....

  14. #14
    Guest
    There are two ways to declare variables. 'Private' is for a module only. 'Public' is for the whole program.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width