mandelbrot Posted August 4, 2006 Posted August 4, 2006 Dear All, Because of the way my current app works I've used an interface to supply basic details between objects. Obviously the data is common to all and key to the execution of the system. If an object implements the interface then is it possible to cast an interface variable to an implementing object type? For instance: Interface IPerson Property Name() As System.String Property Age() As Int32 Property Born() As System.DateTime End Interface Class Employee Implements IPerson ... Public Property Id() As Int32 Get Return _Id End Get Set(ByVal Value As Int32) _Id = Value End Set End Property Public Property Name() As System.String Implements IPerson.Name Get Return _Age End Get End Property ... End Class ... Dim manager As IPerson = New Employee() ... [color=red]MessageBox.Show("Employee " & DirectCast(manager, Employee).Id & " has been updated.", "Employee Updated")[/color] I've tried this (or something very similar) and cannot get it to work... Any advice would be appreciated. Regards, Paul. Quote
Administrators PlausiblyDamp Posted August 4, 2006 Administrators Posted August 4, 2006 (edited) Edit: Ignore this - I didn't read the quesion properly :( Not really an answer this - why are you needing to cast the interface back into a particular class? Couldn't you just do MessageBox.Show("Employee " & manager.Id & " has been updated.", "Employee Updated") Edited August 4, 2006 by PlausiblyDamp Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
mandelbrot Posted August 4, 2006 Author Posted August 4, 2006 Well strictly, no, I suppose, as I've casted manager as IPerson, technically IPerson doesn't have an Id... Basically, I've got several different types of class all reliant on the same data, but I do need to know other data from the specific class types besides that available in the interface itself. For instance, I could define a customer class which implements IPerson and give them a customerId which is a string. Obviously, from this, if I (for instance) run through an array of IPersons I may wish to obtain their employee or customer id, both of which are different underlying types, etc etc... (Hope I'm making myself clear...). Paul. Quote
MrPaul Posted August 4, 2006 Posted August 4, 2006 DirectCast... yes thats fine! I'm interested as to why the employee's Name property returns their age, but thats academic. ;) DirectCast should work fine. I just tried your code and it works as expected. Note that with Option Strict On, you will need to call the Id.ToString() method when appending Id to a string. What error(s) are you getting? Good luck :cool: Quote Never trouble another for what you can do for yourself.
mandelbrot Posted August 4, 2006 Author Posted August 4, 2006 LOL! Typo!!! Hmmm... Well that's rather odd! I've got a tree like data class that that implements a supporting interface which passes data to it. Other objects and controls require access to similar information, and obviously, by identifying the parent of your tree as being an interface, you've got the ability to create a root for your tree. So, I've created the interface to manage the fact that my tree has to grow from something. The interface itself passes the required data... Public Interface IOccurrenceSupport ReadOnly Property User() As NTLogin ReadOnly Property AllCodes() As sysCode ReadOnly Property AllOccurrences() As sysCode ReadOnly Property AllEvents() As sysCode ReadOnly Property AllActions() As sysCode ReadOnly Property AllCausalities() As sysCode ReadOnly Property StartOfDay() As sysCode ReadOnly Property Connection() As String End Interface The tree class that I'm working with implements the IOccurrenceSupport class, as does the ultimate parent (OccurrenceContainer). However, when I try to save the data an Invalid Cast exception raises it's head at this point... localParams.Add("@pParentId", DirectCast(_Parent, Occurrence).Id) _Parent is defined as an IOccurrenceSupport interface, and the Occurrence class actually implements IOccurrenceSupport. I've tried breaking it down to individual statements; replace DirectCast with CType; and finally actually implemented the Id as part of the interface (current solution!). But like you say - I shouldn't have to do this... Quote
mandelbrot Posted August 4, 2006 Author Posted August 4, 2006 D'Oh! Excuse me for being a numpty! I've realised where the problem lies! It's in the fact that the root level occurrence itself will attempt to reference the Id of it's parent, but it won't have one, as it's parent is a different class, containing different properties! Gentlemen - many thanks for your patience! You can both beat me up later! Paul. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.