Click here to Skip to main content
15,885,182 members
Home / Discussions / Visual Basic
   

Visual Basic

 
Questionhow to insert logged in user name automatically to dbtable Pin
rasheed.rahman18-Feb-07 23:53
rasheed.rahman18-Feb-07 23:53 
AnswerRe: how to insert logged in user name automatically to dbtable Pin
Colin Angus Mackay19-Feb-07 0:25
Colin Angus Mackay19-Feb-07 0:25 
AnswerRe: how to insert logged in user name automatically to dbtable Pin
Duncan Edwards Jones19-Feb-07 1:01
professionalDuncan Edwards Jones19-Feb-07 1:01 
GeneralRe: how to insert logged in user name automatically to dbtable Pin
rasheed.rahman19-Feb-07 4:08
rasheed.rahman19-Feb-07 4:08 
QuestionInterface Pin
Navneet Hegde18-Feb-07 23:38
Navneet Hegde18-Feb-07 23:38 
AnswerRe: Interface Pin
Christian Graus18-Feb-07 23:59
protectorChristian Graus18-Feb-07 23:59 
GeneralRe: Interface Pin
Navneet Hegde19-Feb-07 1:01
Navneet Hegde19-Feb-07 1:01 
GeneralRe: Interface Pin
Mark0619-Feb-07 1:45
Mark0619-Feb-07 1:45 
Here's an example for you. Imagine the following 2 classes:

<br />
public class Switch<br />
<br />
public myLight as Light<br />
<br />
public Sub New(aLight as Light)<br />
myLight = aLight<br />
End Sub<br />
<br />
public Sub SwitchOn()<br />
myLight.TurnOn()<br />
end Sub<br />
End Class<br />


and

<br />
Public Class Light<br />
private isOn as boolean<br />
<br />
public Sub New()<br />
End Sub<br />
<br />
Public Sub TurnOn()<br />
isOn = true<br />
' And some other code to shine brightly<br />
End Sub<br />
<br />
End Class<br />


The Switch class contains an object called Light, and a method to turn the Light on. The classes work perfectly, they do what they should do.

But they are too coupled with eachother.

What if, you want to use the Switch class to turn on a Bell? Creating the bell class...
<br />
Public Class Bell<br />
private isOn as boolean<br />
<br />
public Sub New()<br />
End Sub<br />
<br />
Public Sub TurnOn()<br />
isOn = true<br />
' And some other code to ring loudly<br />
End Sub<br />
<br />
End Class<br />

is not a problem. But now integrating the new Bell class with the Switch class is going to be a pain.

Enter Interfaces.

Create the following Interface

Public Interface ISwitchableObject<br />
    Sub TurnOn()<br />
End Interface


Now your Light and Bell classes can inherit the interface, modify the following lines in each class:

public class Light Inherits ISwitchableObject
and
public class Bell inherits ISwitchableObject

Finally, modify your switch class to the following:

<br />
public class Switch<br />
<br />
public myObj as ISwitchableObject<br />
<br />
public Sub New(anObject as ISwitchableObject)<br />
myObj = anObject<br />
End Sub<br />
<br />
public Sub SwitchOn()<br />
myLight.TurnOn()<br />
end Sub<br />
End Class<br />


There, now no matter whether you pass a Bell or Light to the Switch object, it will still handle it exactly the same way. Your classes are now decoupled

And dont forget, we should all be programming to interfaces, not programming to implementation!

Mark
GeneralRe: Interface Pin
Navneet Hegde19-Feb-07 21:51
Navneet Hegde19-Feb-07 21:51 
QuestionAutosizing Combo Dropdown width (vs2005) Pin
Mark0618-Feb-07 23:23
Mark0618-Feb-07 23:23 
AnswerRe: Autosizing Combo Dropdown width (vs2005) Pin
Krish - KP18-Feb-07 23:36
Krish - KP18-Feb-07 23:36 
GeneralRe: Autosizing Combo Dropdown width (vs2005) Pin
Mark0618-Feb-07 23:56
Mark0618-Feb-07 23:56 
GeneralRe: Autosizing Combo Dropdown width (vs2005) Pin
Marcus J. Smith19-Feb-07 3:23
professionalMarcus J. Smith19-Feb-07 3:23 
QuestionWrite inot word document using the textbox created by com add-in Pin
t6.test118-Feb-07 22:47
t6.test118-Feb-07 22:47 
Questioninsertion of binary data 2 database Pin
lopashree18-Feb-07 21:58
lopashree18-Feb-07 21:58 
AnswerRe: insertion of binary data 2 database Pin
Colin Angus Mackay18-Feb-07 22:26
Colin Angus Mackay18-Feb-07 22:26 
AnswerRe: insertion of binary data 2 database Pin
Colin Angus Mackay18-Feb-07 23:17
Colin Angus Mackay18-Feb-07 23:17 
QuestionError while opening a form Pin
Maira K18-Feb-07 21:56
Maira K18-Feb-07 21:56 
AnswerRe: Error while opening a form Pin
GoodID19-Feb-07 21:23
GoodID19-Feb-07 21:23 
GeneralRe: Error while opening a form Pin
Maira K20-Feb-07 19:38
Maira K20-Feb-07 19:38 
QuestionError while opening a form Pin
Maira K18-Feb-07 21:54
Maira K18-Feb-07 21:54 
AnswerRe: Error while opening a form Pin
A*****18-Feb-07 22:23
A*****18-Feb-07 22:23 
GeneralRe: Error while opening a form Pin
Maira K18-Feb-07 22:34
Maira K18-Feb-07 22:34 
AnswerRe: storin 2 the database Pin
Colin Angus Mackay18-Feb-07 22:22
Colin Angus Mackay18-Feb-07 22:22 
GeneralRe: storin 2 the database [modified] Pin
lopashree19-Feb-07 1:11
lopashree19-Feb-07 1:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.