I have a string property in a Module which value has to set the .text value for a label on all the other windows. However the label on the other windows stays empty.
The property gets its value from a selected row in a Listbox/name column and it remembers it correctly as long as it stays on the Window where the value is set, but not on all the other windows, even though the property is part of a Module. Shouldn’t it be remembered throughout the full app?
It is possible to have a property in a window with a name like MyProperty. And also have a property in a module with the same name. If the window sets the property, it’s setting the one in the window, not the one in the module, due to the scope of the properties.
Good. Now you need to consider the sequence of events that determine when the label text values are set and make sure they happen after the first window sets the property. In particular, in which event do you set the label text value in the second window?
It would be a good idea to set a break point in the event where you set the label text value in the second window and examine the module’s property value.
If ‘the other windows’ are created once (implicit instantiation)
then the open event only happens once
So if you set the label in the open event, any changes wont be seen.
Set the label text in the activate event if it is not already there.
In the Opening Event and now… it works because this line was Commented out
But I didn’t know that I should only have the property declared once in the Module AND I found it being declared again As String on another spot, so maybe it was set to empty again there as well. Thanks for your help, Tim!
So the problem was scope. The closer scope is the one that is used.
A variable in a method is only available in the method.
A property of a window is only available in that window. (Unless you prefix it with the window reference.)
A property of a module can be available everywhere.
The precedence is
Method/Event
Window
Module
The compiler allows same named variables/properties, but sets the one that is closest in scope. The rest remain unchanged.
I was able to solve the label problem, but now I have a similar problem when trying to read the Database Columns into Textfields which are placed inside a Container which is placed on a Second window. I am trying to solve this since 3 days and with all kind of possible paths, but I keep on getting a NilObjectException, so it’s looking for something that doesn’t exist. I am using the same path as when Updating info from the textfield to the DB which works great. However I am not able to get that db Data being SELECTED back into the Textfields.
The code for the UpdateSQL for this text field for example is: var antagonistIsSmart as string = ActionAntagonistQuestions.CCAntagonistQuestionList3Applied.Text_AntagonistIsSmart.text //Here I created a variable to make the long path shorter. app.db.ExecuteSQL("UPDATE Characters SET Text_AntagonistIsSmart = ? WHERE ID = ?", antagonistIsSmart, CharacterID_Selected)
The code to read that same data from the DB column back into the Textfields should from what I expect to be: ActionAntagonistQuestions.CCAntagonistQuestionList3Applied.Text_AntagonistIsSmart.text = rs.Column("Text_AntagonistIsSmart").StringValue //Text field
However it gives me a NilObjectException on the whole line.
ActionAntagonistQuestions is a Global property which is placed in a Module.
Sorry, that was above the code, this is the complete script:
//To populate popupmenu and all the other fields:
if ListBoxCharacters.SelectedRowIndex >= 0 then
Var rs As RowSet
Try
rs = app.DB.SelectSQL("SELECT * FROM characters WHERE ID = ?", ListBoxCharacters.RowTagAt(ListBoxCharacters.SelectedRowIndex).StringValue)
Catch e As DatabaseException
MessageBox("Error loading data: " + e.Message)
Return
End Try
If rs <> Nil Then
txtCharacterInfo.Text = rs.Column("Character_Information").StringValue //Text field
popArch.SelectRowWithValue(rs.Column("Character_Type").StringValue) //Popup menu
PopupMenuGender.SelectRowWithValue(rs.Column("Character_Gender").StringValue) //Popup menu
txtCharacter_Age.Text = rs.Column("Character_Age").StringValue //Text field
DateofBirth.SelectedDate = rs.Column("Character_Date").DateTimeValue //Calendar birthdate
//Send db columns data to Question Fields on seperate Windows
//Window.Container.TextField = DB Column
ActionAntagonistQuestions.CCAntagonistQuestionList3Applied.Text_AntagonistIsSmart.text = rs.Column("Text_AntagonistIsSmart").StringValue //Text field
btnUpdateCharInfo.Enabled = false
End If
// close RowSet
rs.Close
end if
It’s only this line that gives the error: NilObjectException
ActionAntagonistQuestions.CCAntagonistQuestionList3Applied.Text_AntagonistIsSmart.text = rs.Column("Text_AntagonistIsSmart").StringValue //Text field
Thanks. It is the Window, ActionAntagonistQuestions (which is in the Module as a Global property), that cannot be reached and is Nil. How do I solve this kind of nil problem? Shouldn’t it be seeing throughout the app because it’s in a Module? I checked if this property maybe was changed somewhere else, but it isn’t.
Perhaps you’re not setting it even though you might think you are. The program compiles, so evidently it looks like the compiler sees it OK. You’re not re-using that name are you?
No, I checked all over with the search field in the debugger. Unless this is seen as a replacement?
ActionAntagonistQuestions.CCAntagonistQuestionList3Applied.Text_AntagonistUnusualMannerisms.text = rs.Column("Text_AntagonistUnusualMannerisms").StringValue//Text field
I added a message box check:
if ActionAntagonistQuestions = nil then
MessageBox("ActionAntagonistQuestions object is Nil")
end if
Do you have code that assigns the Window to ActionAntagonistQuestions?
Where did you put the code?
The code includes Var ActionAntagonistQuestions ... ?
Is that window your main window?
Are you sure that window opening event fired before you try to use ActionAntagonistQuestions?
Usually, I see the code in App Opening event and then that Global property is used instead of the window itself. This is the first time I see the code in the Opening event of the window.