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
Pass a class to a variable
Re: Pass a class to a variable
maybe i'm too old for that shit but i did not understand one single word you wrote ...
Re: Pass a class to a variable
I think I might get the gist of what they want.
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.
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!
Re: Pass a class to a variable
Although I am a bit like PJ here, what I imagine you are trying to do is abstract some type of, for want of a better example, one of Martin Fowler's OO impedance models? (Whether your data source is rdmbs or not).
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
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
Have you ever noticed that software is never advertised using the adjective "spreadable".
- BruceSteers
- Posts: 1592
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: Pass a class to a variable
Aah , my guess is you are fairly new to Gambas.Ham13 wrote: ↑Monday 5th December 2022 8:24pm 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
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...
Static Public Sub MyFunction()
End
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...
Public Sub MyFunction()
End
then i have to create an Instance of the class from my forms class like this...
Dim mc As New MyClass
' Now i access MyFunction from the instance but not directly from the class
mc.MyFunction()
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...
Private hParentForm As Form
Public Sub _new(ParentForm As Form)
hParentForm = ParentForm
End
Public Sub GetFormName() As String
Return hParentForm.Name
End
Then you instance it from each form like this...
Dim hThisFormsMC as New MyClass(Me)
' Now consider this command with the above code..
Print hThisFormsMC.GetFormName()
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.
If at first you don't succeed , try doing something differently.
BruceS
BruceS
Re: Pass a class to a variable
First let me say I am grateful for all of your responses. I'll try to explain what I want to do again.
1) I have several classes that organize data based on a key.
2) The key field is different for each data table
3) 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
4) 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
5) 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.
1) I have several classes that organize data based on a key.
2) The key field is different for each data table
3) 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
4) 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
5) 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.
Re: Pass a class to a variable
I whipped up an incomplete example of one method of inter-form communication. It uses function calls to pass values, rather than making the controls public.
Maybe it'll help, maybe others will find it amusing.
Maybe it'll help, maybe others will find it amusing.
- Attachments
-
- DuelingForms-0.0.1.tar.gz
- (11.51 KiB) Downloaded 105 times
.... and carry a big stick!
Re: Pass a class to a variable
Thank you for the example. Unfortunately it does not answer the question. I need to know which class was used on the first form to sort the database in order to use the same class to sort the database on the second form.
Thanks
Thanks
Re: Pass a class to a variable
Alright, the simple solution is simply to add that as an extra parameter to a function call.
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.
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!
Re: Pass a class to a variable
Still, I think you should consider a TabPanel approach. It'll simplify your coding and simplify your user interface. Here is a project I posted a while ago in the showcase.
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.
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!