Pass a class to a variable

Post your Gambas programming questions here.
Ham13
Posts: 19
Joined: Friday 24th June 2022 2:11pm

Pass a class to a variable

Post by Ham13 »

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
User avatar
PJBlack
Posts: 184
Joined: Tuesday 9th June 2020 10:26pm
Location: Florstadt, Hessen, Germany

Re: Pass a class to a variable

Post by PJBlack »

maybe i'm too old for that shit but i did not understand one single word you wrote ...
User avatar
Cedron
Posts: 156
Joined: Thursday 21st February 2019 5:02pm
Location: The Mitten State
Contact:

Re: Pass a class to a variable

Post by Cedron »

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.
.... and carry a big stick!
User avatar
thatbruce
Posts: 161
Joined: Saturday 4th September 2021 11:29pm

Re: Pass a class to a variable

Post by thatbruce »

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
Have you ever noticed that software is never advertised using the adjective "spreadable".
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Pass a class to a variable

Post by BruceSteers »

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
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...
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
Ham13
Posts: 19
Joined: Friday 24th June 2022 2:11pm

Re: Pass a class to a variable

Post by Ham13 »

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.
User avatar
Cedron
Posts: 156
Joined: Thursday 21st February 2019 5:02pm
Location: The Mitten State
Contact:

Re: Pass a class to a variable

Post by Cedron »

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.
Attachments
DuelingForms-0.0.1.tar.gz
(11.51 KiB) Downloaded 79 times
.... and carry a big stick!
Ham13
Posts: 19
Joined: Friday 24th June 2022 2:11pm

Re: Pass a class to a variable

Post by Ham13 »

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
User avatar
Cedron
Posts: 156
Joined: Thursday 21st February 2019 5:02pm
Location: The Mitten State
Contact:

Re: Pass a class to a variable

Post by Cedron »

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.
.... and carry a big stick!
User avatar
Cedron
Posts: 156
Joined: Thursday 21st February 2019 5:02pm
Location: The Mitten State
Contact:

Re: Pass a class to a variable

Post by Cedron »

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.
.... and carry a big stick!
Post Reply