Scope etc

I having problems with Scope. I trying to “translate” an multi window VB6 app to Xojo. In my software I have declared a Structure, but it can’t be seen from other parts of the program. I’m suppose it has somthing to do with the scope. Any idears?

Best HS.

When you say a Structure, what do you mean? Are you talking about a literal structure with members etc or are you talking about a window design. If you are talking about structure you are often better off using a class as it is far more controllable and configurable. The general exception that have being if you need to pass a structure to an API call.

In terms of scope items that are Public are accessible outside of their declared area. For example public properties are accessible from ourside of that Class or Window. They do, however, require a reference or object to gain access to. For example if you have a public Control on a Window you must have a reference to that Window in order to access that control. Some example code would be:

Var oWindow as New Window1
oWindow.MyControl.Text = "Hello world"
// etc

The scope is “Window” based.
App. and Module. can be visibles from everywhere.

So if you need to use an object (Property…) in many windows, declare it in either App or in a Module.

A look at the Documentation will gave you far more details.

Thanks for your quick answer. I send a smal piece of software where I’m trying to initiate some part of the struct but it fails!; the end program will be rather big.

Best HS.

Add a Class to your Project:
insert_class

then add Propertys, Methods and so on to this Class:
property

Make sure Class and Propertys Scopes needed outside of the Class (Structure) are not Private:
scope

Now you can use the Class like a Structure in your Code. You can even use it via Drag&Drop within other Objects (Scopes), like within a Window f.e…

I’m in a hurry and unfortunately I don’t have the time to be more detailed and precise. I’m sorry. :slight_smile:

2 Likes

Are you getting errors reported? If no what are they. Please paste some problem code into a message and highlight it and press the </> button. It would help us help you.

Once again when you stay structure are you talking about something that looked like this:

Or are you talking about a window:

Screenshot 2024-02-22 at 14.07.03

How can one send a litle test-program?

Best HS.

A key question is where is your Structure declared. Is is a property of a Window? Of App? Of a Module? Of a Method?

It is easier is you make better questions, thay way you are going to have less nonsensical answers. For your question, you need to provide at least, WHAT are the “structure” used for, WHERE are you declaring it and WHERE do you want to use it.

Most use cases of a VB “Structure” are better suited in a xojo class, like Sascha pointed but.

Do you mean upload a sample (test-program) on the forum?
You need to use dropbox, onedrive, google drive or other file sharing service and put the share link here.

So having seen the example program I can point out a few things. First you are using the Window class name to access properties of the window, this is causing “Implicit Instance” problems. Click on each window in your project and click on the Cog in the inspector. Disable “Implicit Instance” checkbox. It will only cause you problems. Do not use the Class name of the Window to reference the Window. For example:

Select Case SKift
Case  0
  Window1.BackGroundColor=Color.RGB(0,0,0) //this line seems to be necessarry!
  Window1.BackGroundColor=Color.RGB(255,0,0) //red color
Case  1 
  Window1.BackGroundColor=Color.RGB(0,0,0) //this line seems to be necessarry!
  Window1.BackGroundColor=Color.RGB(0,255,0) //green color
Case Else
  Window1.BackGroundColor=Color.RGB(0,0,0) //this line seems to be necessarry!
  Window1.BackGroundColor=Color.RGB(0,0,255) //blue color
End Select

if skift<2 then
  Skift=Skift+1
else
  Skift=0
End if

Instead of Window1 you should use Self as the window you are accessing is the current one that this timer is on. The code becomes:

Select Case SKift
Case  0
  Self.BackGroundColor=Color.RGB(0,0,0) //this line seems to be necessarry!
  Self.BackGroundColor=Color.RGB(255,0,0) //red color
Case  1 
  Self.BackGroundColor=Color.RGB(0,0,0) //this line seems to be necessarry!
  Self.BackGroundColor=Color.RGB(0,255,0) //green color
Case Else
  Self.BackGroundColor=Color.RGB(0,0,0) //this line seems to be necessarry!
  Self.BackGroundColor=Color.RGB(0,0,255) //blue color
End Select

if skift<2 then
  Skift=Skift+1
else
  Skift=0
End if

In terms of this line:

  Self.BackGroundColor=Color.RGB(0,0,0) //this line seems to be necessary!

It isn’t required on the Mac, I can’t say for Windows or Linux.

Secondly the commented out item in the button Pressed event:

Label1.Text = PanelData(0).PanelSMS //I supposed it's a scope problem?

I can’t really tell from this example what the issue is because PanelData is not defined anywhere in the example. If PanelData is defined outside of the Window then it can be good practice to use the reference to that location. For example if it is in a Module and is Public then this reference could work. However you could also try:

Label1.Text = Module1.PanelData(0).PanelSMS

Especially if PanelData is defined in more than one place.

For other people trying to help this is truly a Structure. It is defined as follows:

Structure defPanelData
   PanelVoltage as String * 6
   PanelCurrent As String * 6
   PanelSMS as String *  8
End Structure

I suspect that this is used in an API call so is best left as a Structure in this case.

Hello Julia.

Thanks, but tre problem is solved, Best HS.

Hello AlbertoD.

Thanks, but tre problem is solved, Best HS.

Hello Ivan.

Thanks, but tre problem is solved, Best HS.