Dynamic class declaration

voiD

Registered User.
Local time
Today, 16:39
Joined
Sep 9, 2005
Messages
37
Hello,

I am writing an application and I have a lot of custom classes. I want to create a function which retrives with a new object, but I want to determine the class of the object in runtime as a parameter of the function. Is that possible somehow to declare a new object on an other way than the usual?:confused:
Dim obj as Object
Set obj = New CustomClass

Many thanks in advance for any piece of help!

VoiD
 
To determine the type of an existing object at runtime you can use the VBA.Typename() function. This returns a string that is the name of type you passed to the function. Consider the following code...

Code:
[SIZE="1"][FONT="Verdana"]Function GetTypeFromType(FromType As Variant) As Variant
'  This function determines the datatype of its input parameter
'  and performs custom actions based on that information
   
   Select Case VBA.TypeName(FromType)
      
      Case "cMyCustomer"
         ' if this function receives a custom cMyCustomer class instance
         ' it returns a DAO.recordset of tContact records related to that customer
         Set GetTypeFromType = CurrentDb.OpenRecordset( _
            "SELECT * " & _
            "FROM tContact " & _
            "WHERE CustomerID = " & FromType.CustomerID)
      
      Case "Integer"
         ' if this function receives an Integer it converts it to a Long
         GetTypeFromType = CLng(FromType)
         
      Case "Recordset"
         ' if this function recieves a recordset it returns the recordcount
         GetTypeFromType = FromType.RecordCount
         
      Case "Long"
         ' if this function receives a long it presumes it is a CustomerID
         ' and creates and loads and returns a new instance of the cMyCustomer class
         Set GetTypeFromType = New cMyCustomer
         GetTypeFromType.Load FromType
         
   End Select

End Function[/FONT][/SIZE]
 
Thanks Lagbolt for the suggestion, but this is not exactly what I am looking for. I wasn't exact enough in my initial post.I want to create a function which can retrive various types of custom objects, in basis of the function's parameter. Eg.:

Code:
Function CreateObject(pClass as String)
  
  Select Case pClass
  Case "Class1"
    Set CreateObject = New Class1
  Case "Class2"
    Set CreateObject = New Class2
    .
    .
    .
  End Select
End Function

My aim is to avoid a long select-case statement, because I have many custom classes. That's why I am interested whether is there any other way of declaration of an object.
 
I want to create a function which can retrive various types of custom objects [based on] the function's parameter.
If you take another look at my code, this is exactly what it does. If you pass in a cMyCustomer object it returns a DAO.Recordset. If you pass in a long integer, it returns a new instance of the custom cMyCustomer class. It does other things too.
In your original post you wrote...
I want to create a function which retrives [...] a new object, but I want to determine the class of the object [at] runtime as a parameter of the function.
This sounds like a select case statement similar to the code in your second post, but if you don't want that, then just instantiate your custom classes as you need them. Certainly there will be procedures where it is known at design time what class is called for in that context.
Is this helping?
Cheers,
 
My only problem with this solution is that, I don't want pass object to the function, only string. But in this case, only the select-case statement seems to me a solution and with a lot of custom classes it results a quite long code.
 
It's still not clear to me why you would need a generic function that would handle more than one custom class.

If your intention is to have a common functionality among various classes, then I would look at those possibilities:

1) Instead of having several custom classes, reduce to one custom class that can be then adapted by setting the appropriate properties at runtime.

2) See if you actually can create a empty class and use Implements keyword and thus use the empty class (which should technically be an interface but VB doesn't know about such thing) as your argument instead of the actual custom class and you should be guaranteed that it will work for any custom class that Implements the empty class.


It's also possible there exists a simpler solution -- if you don't mind being a bit more specific in what you are doing with your custom classes we might be able to make more concrete suggestions.

HTH.
 
Well, I am developing an application for a productive plant. There are six modules used in the production, each module at different machine types. These modules used to collect data about the production, mainly filled by the machine operators, but some data are coming from external ERP system. There are other modules used to evaluate the collected data and make reports, queries, etc. The application covers the whole production process of the factory. For each module, there are custom classes which responsible for one or more function of the given module. For example a custom class is responsible to open a module for a machine.
Till now, only one module was able to be opened in one time, because the instances of custom classes were defined in a separate module. According to a new demand, one module should be opened in more than one instance in one time on the same machine and with the current logic it is not possible. To rewrite the whole application would be a huge work and now I am seeking for alternative solution.
My idea is to use a global collection in which I store all custom class instances and I refer to the instance by its index in the collection. As a part of this solution I want to use the mentioned function to create new object and add to the collection. To do this, I planned to give the class name to the function as a string parameter and in basis of it add a new object to the collection. With a quite long select-case it is no problem, I just want to shorten and make simpler the code.
Hope it is now clearer what is my intention and what I want with the function.

VoiD
 
I have to confess confusion here.

I thought we were talking about class modules, but now you seem to be talking about user interface and machine operator needing to use more than one "module" at same time. By "module", do you mean a file that contain VBA code, or maybe you mean a part of an application... like a snap-in? Or maybe you mean a VBA module is actually responsible for opening and reading a data source from different sources and put them into Access table... automating the import/export of data?

More clarifications, please...
 
Hm, you are right Banana, I scrambled the things a little bit. In my previuous post under module I've meant a part of the application and not a VBA module. There is only one .mdb file and depended on the user logged in, the assigned part of the appliction will be started, that's what I called module.
 
Okay, that helps bit.

Is it at all possible to at least show a bit of code that handles the loading of "module" and illustrating why it is not easy to change so it can load more than one "module"?
 

Users who are viewing this thread

Back
Top Bottom