|
-
May 3rd, 2004, 03:00 PM
#1
Impliments
I don't really understand impliments so I was wondering if someone could explain it a little. I don't really understand it in VB6 or .NET
I program in VB6 right now but I am reading some books on .NET to get up to speed with it. I have never had the need for impliments in my code so I never bothered to really figure it out.
From what I can tell it is sort of like inheritence but it is to inherit an interface? When you impliment at interface, you aren't getting any code with it? just a list of subs/functions that your class must use in order to comply with the interface???
what am I missing??
I guess I just don't really see the point of them although I am sure they exist with good reason
-
May 3rd, 2004, 03:07 PM
#2
You got it. The point is for something like plugins when you want to make sure the 3rd party developers implement the functions YOUR app expects to see and will try to call in the plug-ins.
You are then able to check if the plugin is implementing the correct interface before you load it so you dont have something crash trying to call functions in a plugin that are not there.
-
May 3rd, 2004, 03:10 PM
#3
ok so for example, why do you need to impliment the IDisposable interface to call the dispose method? couldn't you simply just make a dispost method and write the code for it without Implimenting the IDisposable interface? Or when you impliment that interface does that tell something else to the .NET framework that is needed?
-
May 3rd, 2004, 03:26 PM
#4
Because your app is like a plugin to the Garbage Collector and Just In Time Compiler. By implemetning IDisposable, the GC will know to clean up your app when it gets around to it. Just consider your .NET application as a plugin to the .NET framework and you can probably visualize it better.
-
May 3rd, 2004, 03:30 PM
#5
gotcha... ok so what use is it in VB6 then?
-
May 3rd, 2004, 03:32 PM
#6
Same thing. The only place I ever used Implements in VB6 was for COM+ MTS objects, in which case, the same scenario. The dll was a 'plugin' to MTS. And the implements was for the 3 or so functions it expected in your dll.
-
May 3rd, 2004, 04:34 PM
#7
gotcha.. seems to have a lot more use when it comes to .net... but then again i guess that is because it is a form of inheritence and as we know VB6 was lacking in that area
-
May 3rd, 2004, 05:32 PM
#8
Fanatic Member
Investigate Polymorphism. There are plenty of good articles in MSDN on the subject. To understand the implements key word, you must have an understanding of polymorphism.
I have found countless uses for polymorphism in my apps, and they work brilliantly. When you figure out how to use the concept correctly, you will feel like you're on a whole other level of programming.
-
May 3rd, 2004, 07:40 PM
#9
-
May 3rd, 2004, 11:28 PM
#10
Here is a code example to show you sort of how polymorphing works.
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'make two instances of different kinds of objects
Dim objOfOneType As New Car("Dodge Stratus", 2002, Color.Blue)
Dim objOfAnotherType As New Person("Ed", 27)
'now cast the specific type to the interface it implements
Dim generic As IGenericName = objOfOneType
'run interface members regardless of what they are actually called in the class itself
generic.Display() 'will show "Dodge Stratus" in msgbox
'now for the other object
generic = objOfAnotherType
generic.Display() 'will show "Ed Marquez" in msgbox
'See how both can be used as IGenericName even though they are actually of other types.
'This could also be useful with method as you can have IGenericName as the parameter type
'then anything that implements it regardless of actual type can be passed in.
'This is the basics of polymorphic behavior, since the types can polymorph or change into other types.
End Sub
'example interface provides a required structure
Public Interface IGenericName
Property Name() As String
Sub Display()
End Interface
Public Class Car
Implements IGenericName
Private _Make As String = String.Empty
Private _Year As Integer = 2000
Private _Color As Color = Color.Blue
Public Property Make() As String Implements IGenericName.Name
Get
Return _Make
End Get
Set(ByVal Value As String)
_Make = Value
End Set
End Property
Public Property Year() As Integer
Get
Return _Year
End Get
Set(ByVal Value As Integer)
_Year = Value
End Set
End Property
Public Property Color() As Color
Get
Return _Color
End Get
Set(ByVal Value As Color)
_Color = Value
End Set
End Property
Public Sub ShowMake() Implements IGenericName.Display
MsgBox(Me.Make, , "Car")
End Sub
Public Sub New(ByVal make As String, ByVal year As Integer, ByVal color As Color)
Me.Make = make
Me.Year = year
Me.Color = color
End Sub
End Class
Public Class Person
Implements IGenericName
Private _Name As String = String.Empty
Private _Age As Integer = 25
Public Property Name() As String Implements IGenericName.Name
Get
Return _Name
End Get
Set(ByVal Value As String)
_Name = Value
End Set
End Property
Public Property Age() As Integer
Get
Return _Age
End Get
Set(ByVal Value As Integer)
_Age = Value
End Set
End Property
Public Sub SayName() Implements IGenericName.Display
MsgBox(Me.Name, , "Person")
End Sub
Public Sub New(ByVal name As String, ByVal age As Integer)
Me.Name = name
Me.Age = age
End Sub
End Class
-
May 4th, 2004, 11:44 AM
#11
yay gay
As a side not it's Implements
\m/  \m/
-
May 4th, 2004, 09:38 PM
#12
I wonder how many charact
-
May 6th, 2004, 11:34 AM
#13
lol well at least VB is good at correcting my bad spelling
-
May 6th, 2004, 11:45 AM
#14
Originally posted by nemaroller
A 'side not'?
lol
Oh the irony.
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
|