Pass a class to a variable
Posted
#1
(In Topic #950)
Trainee
I have a project that uses a class to pass data back to a from and load data based on the class index = record number. The problem is that there are several classes. One for each datatable. I want to create one form that will use any one of the classes to locate data. I would like to pass the class form form one and use it in form two and work with it in form two.
Thanks
Posted
Enthusiast

Posted
Regular

Running multiple forms and passing data is possible is several ways, the easiest being declaring your variables 'Public' and referencing them directly from another form.
I've always preferred single form applications, going way back. I would recommend a TabPanel instead of multiple forms. That way all your data declarations are within the scope of your main form.
It also sounds like it may be a database application. In which case data bound controls may be a good answer, depending.
A 'Collection' is an excellent way to hold a set of objects for which you have a string key, or if it is numeric, in may not be contiguous. Simply use the 'Str()' function on your numeric key.
All the quoted items can be found in the help.
.... and carry a big stick!
Posted
Regular

Well. Yes this is possible, we here have done it for over a decade. BUT it is not as simple as a-d-c
Perhaps if you explained in simple everyday language:
What's the goal
Where's the data
Why does more than one data source cause you a headache
… continuing to watch
b
Posted
Guru

Ham13 said
Hello,
I have a project that uses a class to pass data back to a from and load data based on the class index = record number. The problem is that there are several classes. One for each datatable. I want to create one form that will use any one of the classes to locate data. I would like to pass the class form form one and use it in form two and work with it in form two.
Thanks
Aah , my guess is you are fairly new to Gambas.
Sounds like what you require is pretty standard stuff you will pick up with a bit of experience in the gambas class systems.
and easily done when "instancing" a class.
Non static classes need to be "instanced" or created by code.
For example if i have a class called Myclass.class and i create a Static function like this…
Then I can access MyClass.MyFunction()
But we want the class functions to be attached to the calling form yes?
So if the function is not Static , Ie…
then i have to create an Instance of the class from my forms class like this…
when you instance a class it uses the _new() method and you can pass data to it
If your class is set up like this…
Then you instance it from each form like this…
So when i use this to instance the class..
Dim hThisFormsMC as New MyClass(Me)
the classes _new() method sets the Parent form of that class instance
hThisFormsMC is now linked to this form
that is just a simple (useless) example but could be the sort of thing you are looking for.
Posted
Trainee
- I have several classes that organize data based on a key.
- The key field is different for each data table
- the main form uses class to organize the data so we know the record order and can move to a record based on record number
- The second form uses the same class organize the data in the same order. Then a select query, based on the class finds the record number of the requested record
- The record number is passed back to the first form and the record pointer is moved to that record on the first from.
I know that this may be hard to understand but it works without problem. ( I may have gotten this solution from another one of my post)
OK here is the problem - The parent form may use any class. I want one child form to be able to deal with the class used in the parent.
Therefore I must tell the child form which class to use. How do I tell the child which class was used in the parent to generate the data.
Can I set a variable = to the class and pass that to the child form? Be nice if I could clone the parents datasource then I could use the clone to get the record number.
Hope this helps.
Posted
Regular

Maybe it'll help, maybe others will find it amusing.
.... and carry a big stick!
Posted
Trainee
Thanks
Posted
Regular

Or define a public variable in the other form, and whenever you set it in the one, you also set it in the other. The routine in the other form then has a local value to use. (Or use a "Setter routine")
They are called "state variables." They are how multi-threaded applications coordinate processing. By using two forms, you are simulating multiprocessing, even though Gambas is single threaded.
Better yet, put all your "business logic" down in a class file and have both forms call into it upon events.
There are lots of different ways to do things.
.... and carry a big stick!
Posted
Regular

https://forum.gambas.one/viewtopic.php?p=2698
(If you have your music in files, you'll want to download this. Hidden features, look at the code.)
It is a music file player. There are also many other projects in the forum, and on the farm, many with multiple forms. Find one of those that is similar in structure and then you can study how they did it.
.... and carry a big stick!
Posted
Guru

/comp/gb/class - Gambas Documentation
/comp/gb/classes - Gambas Documentation
Things like
Class.Name
Object.Parent
Surely if your form manually loads a specific class it can make a note of the class name and store it somewhere that the other form can see?
Posted
Trainee
I'm going to try Cedric's approach to sending the collection over using a function an see what I can do with that idea.
Regards,
Marty
1 guest and 0 members have just viewed this.


