Click here to Skip to main content
15,914,416 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: Pass parameter to webusercontrol Pin
Marcus J. Smith19-Feb-07 3:59
professionalMarcus J. Smith19-Feb-07 3:59 
QuestionCreate an object array and load it at runtime with VB 2005? Pin
zinedinew19-Feb-07 2:54
zinedinew19-Feb-07 2:54 
AnswerRe: Create an object array and load it at runtime with VB 2005? Pin
nlarson1119-Feb-07 3:28
nlarson1119-Feb-07 3:28 
GeneralRe: Create an object array and load it at runtime with VB 2005? Pin
zinedinew19-Feb-07 3:44
zinedinew19-Feb-07 3:44 
QuestionRandom Access Files Pin
JonCox19-Feb-07 2:50
JonCox19-Feb-07 2:50 
AnswerRe: Random Access Files Pin
Marcus J. Smith19-Feb-07 7:40
professionalMarcus J. Smith19-Feb-07 7:40 
GeneralRe: Random Access Files Pin
JonCox19-Feb-07 8:03
JonCox19-Feb-07 8:03 
GeneralRe: Random Access Files Pin
Marcus J. Smith19-Feb-07 8:08
professionalMarcus J. Smith19-Feb-07 8:08 
GeneralRe: Random Access Files Pin
JonCox19-Feb-07 8:20
JonCox19-Feb-07 8:20 
QuestionHow do i fix the toolbar button width? Pin
.NetRams19-Feb-07 2:34
.NetRams19-Feb-07 2:34 
QuestionVB.NET CODE Pin
Segigo19-Feb-07 1:31
Segigo19-Feb-07 1:31 
AnswerRe: VB.NET CODE Pin
Mark0619-Feb-07 1:57
Mark0619-Feb-07 1:57 
AnswerRe: VB.NET CODE Pin
sathesh pandian20-Feb-07 19:18
sathesh pandian20-Feb-07 19:18 
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 

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.