Pass a class to a variable

Post

Posted
Rating:
#1 (In Topic #950)
Trainee
 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
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Enthusiast
PJBlack is in the usergroup ‘Enthusiast’
 maybe i'm too old for that shit but i did not understand one single word you wrote …
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Regular
Cedron is in the usergroup ‘Regular’
 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!
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
 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

Online now: No Back to the top

Post

Posted
Rating:
#5
Guru
BruceSteers is in the usergroup ‘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…

Code (gambas)

  1. Static Public Sub MyFunction()
  2.  
  3.  
  4.  

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…

Code (gambas)

  1.  
  2. Public Sub MyFunction()
  3.  
  4.  

then i have to create an Instance of the class from my forms class like this…

Code (gambas)

  1. Dim mc As New MyClass
  2. ' Now i access MyFunction from the instance but not directly from the class
  3. mc.MyFunction()
  4.  
  5.  

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…

Code (gambas)

  1.  
  2. Private hParentForm As Form
  3.  
  4. Public Sub _new(ParentForm As Form)
  5.  
  6.   hParentForm = ParentForm
  7.  
  8.  
  9. Public Sub GetFormName() As String
  10.  
  11.   Return hParentForm.Name
  12.  
  13.  
  14.  

Then you instance it from each form like this…

Code (gambas)

  1. Dim hThisFormsMC as New MyClass(Me)
  2.  
  3. ' Now consider this command with the above code..
  4. Print hThisFormsMC.GetFormName()
  5.  
  6.  

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.
Online now: No Back to the top

Post

Posted
Rating:
#6
Trainee
 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.
Online now: No Back to the top

Post

Posted
Rating:
#7
Avatar
Regular
Cedron is in the usergroup ‘Regular’
 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.

Attachment

.... and carry a big stick!
Online now: No Back to the top

Post

Posted
Rating:
#8
Trainee
 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
Online now: No Back to the top

Post

Posted
Rating:
#9
Avatar
Regular
Cedron is in the usergroup ‘Regular’
 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!
Online now: No Back to the top

Post

Posted
Rating:
#10
Avatar
Regular
Cedron is in the usergroup ‘Regular’
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.

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!
Online now: No Back to the top

Post

Posted
Rating:
#11
Guru
BruceSteers is in the usergroup ‘Guru’
I assume you have studied the wiki
/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?
Online now: No Back to the top

Post

Posted
Rating:
#12
Trainee
 I tried a new tack. The object is to get a sequential list of data records so that the record's  position in the list is equivalent to the record number. The class is the method used to generate this list. So why not make a collection from the list and pass the collection to the other form? In that way the list no longer needs the class to be used on the other form. After some tinkering with the code I got it working.  Maybe unconventional but, it works for now.

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
Online now: No Back to the top
1 guest and 0 members have just viewed this.