Variable scope

Post

Posted
Rating:
#1 (In Topic #1365)
Trainee
 I'm new to Gambas (but have experience of other programming languages) and have just started experimenting with it. I've written a simple piece of code with a main class FMain that creates two buttons hButton1 and hButton2, and I'm trying to use the hButton1_Click event handler to change one of the attributes of hButton2. However, when I try to run it I keep getting the error "Unknown identifier: hButton2 (FMain.class:34) within the event handler. I assume this means that hButton2 is out of scope within sub hButton1_Click but can't find any information on how to fix this. I am running Gambas from Linux Mint on a desktop PC.
Online now: No Back to the top

Post

Posted
Rating:
#2
Regular
vuott is in the usergroup ‘Regular’
It is always a good rule to show the part of code that produces the error.


CharlesStearman said

…when I try to run it I keep getting the error "Unknown identifier: hButton2 (FMain.class:34) within the event handler. I assume this means that hButton2 is out of scope within sub hButton1_Click…
Uhmmm… did you formally declare hButton2 as "<COLOR color="#800000">Global</COLOR> " ?

Exemplum:

Code (gambas)

  1. Private hButton2 As Button
  2.  
  3.  
  4. Public Sub Form_Open()
  5.  
  6.  Dim hButton1 As Button
  7.  
  8.  With hButton1 = New Button(Me) As "hButton1"
  9.    .W = 100
  10.    .H = 50
  11.    .X = 20
  12.    .Y = 20
  13.  
  14.  With hButton2 = New Button(Me) As "hButton2"
  15.    .W = 100
  16.    .H = 50
  17.    .X = 20
  18.    .Y = 100
  19.  
  20.  
  21.  
  22.  
  23. Public Sub hButton1_Click()
  24.  
  25.   hButton2.W = 200
  26.  

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#3
Trainee
Thank you. I understand now. I was basing my code on an example program in which all the variable declarations were inside the form_open subroutine, and was under the impression that this made them global by default.
Online now: No Back to the top
1 guest and 0 members have just viewed this.