Recent Posts

Monday, July 14, 2008

How can we use the same class obj for sending and receiving the data from one function in VB

1. Open a project in vb and in the form1 put two text boxes and one command button
name them txtOldDog,txtNewDog and cmdSend

2. Add one class named Dog.cls with the following code:

'local variable(s) to hold property value(s)
Private mvarDogName As String 'local copy
Private mvarDogColor As String 'local copy

Public Property Let DogColor(ByVal vData As String)
'used when assigning a value to the property, on the left side of an assignment.
'Syntax: X.Barks = 5
mvarDogColor = vData
End Property


Public Property Get DogColor() As String
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.Barks
DogColor = mvarDogColor
End Property



Public Property Let DogName(ByVal vData As String)
'used when assigning a value to the property, on the left side of an assignment.
'Syntax: X.DogName = 5
mvarDogName = vData
End Property


Public Property Get DogName() As String
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.DogName
DogName = mvarDogName
End Property


3. Add a module name modPublic.bas and add the following code:

Option Explicit


Public Sub CheckIfDogReturns(ByRef obj As Dog, ByRef msg As String)
msg = "The name of Dog is "
With obj
msg = msg + .DogName
msg = msg + " and its color is "
msg = msg + .DogColor

.DogName = "NewName Pony"
.DogColor = "New color yello"
End With

End Sub

4. Now in your form1 double click cmdSend button and add the following code:

Private Sub cmdSend_Click()
Dim objDog As New Dog
Dim showMsg As String


objDog.DogName = "Tony"
objDog.DogColor = "Red"

Call CheckIfDogReturns(objDog, showMsg)
Me.txtOldDog = showMsg
Me.txtNewDog = objDog.DogName & " " & objDog.DogColor


End Sub


5. Now you can see that the objDog is carrying Tony and red and getting back Pony and yello so that
means in your application you can carry the object to data logic and update the database and
come back with the updated record on the same object and display it on your User interface.

Related Posts by Categories




No comments:

Post a Comment