Announcement

Collapse
No announcement yet.

What happens in this situation...

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • What happens in this situation...

    Hi guys,


    I was hoping somebody could explain to me what exactly happens when you have a global variable, we'll call it SESSION, and you use the same name in a functions parameter list.


    Code:
    type CSession
        m_Number as long
    end type
    
    global Session as CSession
    
    function OpenSession(Session as CSession) as long
        Session.m_Number = 2048
    end function
    Is this even legal or is the compiler just missing this?


    does the parameter act as a local variable inside the function and not even see the global variable?

    ------------------
    Cheers

    [This message has been edited by mark smit (edited February 06, 2001).]

  • #2
    Here is a better example of what I mean...
    Code:
    rem test
    rem
    #compile exe
    
    
    type CPerson
        Age as long
        FirstName as asciiz * 64
        LastName as asciiz * 64
    end type
    
    
    global Person as CPerson
    
    function MakePerson(Person as CPerson,byval Age as long,byval FirstName as string,byval LastName as string) as long
        Person.Age = Age
        Person.FirstName = FirstName
        Person.LastName = LastName
    end function
    
    
    function pbmain as long
        dim Other as CPerson
        
        MakePerson Person,38,"Bill","Gates"
        MakePerson Other,32,"John","Carmack"
        
        msgbox Person.FirstName & $CRLF & Other.FirstName
    end function

    The variable "Person" is a global and also used as a parameter in the function MakePerson. It seems to work but I'm not clear on why.

    ------------------
    Cheers

    Comment


    • #3
      It works because you're passing Person as a parameter. This effectively makes Person a local variable. Local varaibles override globals when they are named the same.
      --Don


      ------------------
      www.basicguru.com/dickinson
      Don Dickinson
      www.greatwebdivide.com

      Comment


      • #4
        Be careful though. Trying to put ID software's John Carmack and
        Bill Gates in same room may result in some real serious GPF's..


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

        Comment


        • #5
          HeHeHe!!

          I was wondering if anybody else was gonna notice those names Borje

          ------------------
          Cheers

          Comment

          Working...
          X