Page 1 of 2

Pass a class to a variable

Posted: Monday 5th December 2022 8:24pm
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

Re: Pass a class to a variable

Posted: Monday 5th December 2022 11:40pm
by PJBlack
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

Posted: Tuesday 6th December 2022 12:28am
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.

Re: Pass a class to a variable

Posted: Tuesday 6th December 2022 12:16pm
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

Re: Pass a class to a variable

Posted: Tuesday 6th December 2022 2:03pm
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.

Re: Pass a class to a variable

Posted: Tuesday 6th December 2022 3:06pm
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.

Re: Pass a class to a variable

Posted: Tuesday 6th December 2022 4:46pm
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.

Re: Pass a class to a variable

Posted: Wednesday 7th December 2022 2:41pm
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

Re: Pass a class to a variable

Posted: Wednesday 7th December 2022 3:06pm
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.

Re: Pass a class to a variable

Posted: Wednesday 7th December 2022 3:21pm
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.