Results 1 to 6 of 6

Thread: [RESOLVED] Problem with one object using two Classes

  1. #1

    Thread Starter
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Resolved [RESOLVED] Problem with one object using two Classes

    I have an application where in I have created two class modules. Both the class modules have same properties and same methods but the implementation is different. As both the classes have same methods and properties I am using the same Object to instantiate the classes like this
    VB Code:
    1. 'In general declarations
    2. Dim objTemp As Object
    3.  
    4. 'in a procedure
    5. If SomeCondition Then
    6.     Set objTemp = New Class1
    7. Else
    8.     Set objTemp = New Class2
    9. End If
    10. 'After some processing
    11. Set objTemp = Nothing
    After instantiating the object I will use objTemp in different methods. This instantiation happens in a timer event.

    This does not give any Compile or Run Time Errors, but the application stops working either when it is before the Instantiation code or just at this line
    VB Code:
    1. Set objTemp = Nothing
    Although I am not sure if this is causing any problem but I would like to know are there any drawbacks of using this type of code.

    Any comments and suggestions are welcome.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  2. #2
    Lively Member
    Join Date
    Sep 2005
    Posts
    105

    Re: Problem with one object using two Classes

    Couldn't you just use different objects?
    TempTimer and TempWhatever...

  3. #3

    Thread Starter
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: Problem with one object using two Classes

    Quote Originally Posted by xensyria
    Couldn't you just use different objects?
    TempTimer and TempWhatever...
    Well the problem is that other methods use the object to call method on it. As Class1 and Class2 have same members and different implementation so I thought it would be better to have one object used across the application. The calling method doesn't have to know what type of object it is. And if I use two objects then I will have to re-write the routines which use the objTemp object.

    Actually this code was written by someone else and now the application stops at regular intervals. It doesn't show up any error or even when you open Task Manager the application is shown as running.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Problem with one object using two Classes

    OK, so they have the SAME methods and properties... only the implementation code is different? Sounds like the perfect candidate for an interface.....
    What you should do is create an interface class that has the methods & properties signatures... then in each of your classes, implement that interface. THEN you dim your var as your interface type .... then you can instanciate it as one class or the other. Advantages, you are guaranteed that both classes will have the same signature (methods & properties) and you get early binding - what you are doing now is late binding.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: Problem with one object using two Classes

    Well actually there is more to it now during some maintenance there have been different methods added to the classes (it sux when you are working on a project where so many people have changed code from time to time). I thought both the classes were same, but actually one class has some methods which are not present in the other class.

    Also the above code that I had written is not complete. The code in the Time Event lloks like this
    VB Code:
    1. 'in a procedure
    2. If SomeCondition Then
    3.    Set objTemp = New Class1
    4.    'Do some processing
    5.    Set objTemp = Nothing
    6.    Set objTemp = New Class2
    7.    'Do Some processing
    8. Else
    9.    Set objTemp = New Class2
    10.    'Do some processing
    11.    Set objTemp = Nothing
    12.    Set objTemp = New Class1
    13.    'Do Some processing
    14. End If
    15. 'After some more processing
    16. Set objTemp = Nothing

    Any help or comments are greatly appreciated.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  6. #6

    Thread Starter
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: Problem with one object using two Classes

    Actually the interface did solve the problem.

    Thanks for the help.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width