|
-
Feb 16th, 2006, 07:51 AM
#1
Thread Starter
Shared Member
[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:
'In general declarations
Dim objTemp As Object
'in a procedure
If SomeCondition Then
Set objTemp = New Class1
Else
Set objTemp = New Class2
End If
'After some processing
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 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
-
Feb 16th, 2006, 08:42 AM
#2
Lively Member
Re: Problem with one object using two Classes
Couldn't you just use different objects?
TempTimer and TempWhatever...
-
Feb 16th, 2006, 08:55 AM
#3
Thread Starter
Shared Member
Re: Problem with one object using two Classes
 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
-
Feb 16th, 2006, 08:56 AM
#4
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
-
Feb 17th, 2006, 09:29 AM
#5
Thread Starter
Shared Member
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:
'in a procedure
If SomeCondition Then
Set objTemp = New Class1
'Do some processing
Set objTemp = Nothing
Set objTemp = New Class2
'Do Some processing
Else
Set objTemp = New Class2
'Do some processing
Set objTemp = Nothing
Set objTemp = New Class1
'Do Some processing
End If
'After some more processing
Set objTemp = Nothing
Any help or comments are greatly appreciated.
Use [code] source code here[/code] tags when you post source code.
My Articles
-
Feb 27th, 2006, 06:17 AM
#6
Thread Starter
Shared Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|