Thursday, October 21, 2004

VJs Tip Of The Day - October 21st 2004

Structure Vs Classes - The finer ones

This one is a double tip as yesterday there was none... This is information + TODO exercise... Spend some time here to copy the below code in VS.Net and try running it.. It will run out of the box you won't have to trouble yourself a lot... Btw, its VB again... Don't ask me why, the silly reason is that I made a VB project by mistake and did not want to delete the folders...

Well the information below talks subtle difference between classes and structure and the way it effects Reflection...

Setting Properties via Reflection


Make a class library and paste the below code in the default class created...

Imports System.Reflection


Namespace VJ
Public Class GoodBoy
Private _setMeIfYouCan As String
Public Property SetMeIfYouCan() As String
Get
Return _setMeIfYouCan
End Get
Set(ByVal Value As String)
_setMeIfYouCan = Value
End Set
End Property
End Class

Public Structure BadBoy
Private _setMeIfYouCan As String
Public Property SetMeIfYouCan() As String
Get
Return _setMeIfYouCan
End Get
Set(ByVal Value As String)
_setMeIfYouCan = Value
End Set
End Property
End Structure

Public Class Setter
Public Function SetClass(ByVal newString As String) As Boolean
Dim gb As New GoodBoy
Dim gbType As Type = GetType(GoodBoy)
Dim propInfo As PropertyInfo = gbType.GetProperty("SetMeIfYouCan")
propInfo.SetValue(gb, newString, Nothing)
If (newString.Equals(gb.SetMeIfYouCan)) Then
Return True
End If
End Function

Public Function SetStructure(ByVal newString As String) As Boolean
Dim bb As New BadBoy
Dim bbType As Type = GetType(BadBoy)
Dim propInfo As PropertyInfo = bbType.GetProperty("SetMeIfYouCan")
propInfo.SetValue(bb, newString, Nothing)
If (newString.Equals(bb.SetMeIfYouCan)) Then
Return True
End If
End Function

End Class
End Namespace


Make a windows application... On the form drag and drop a button... Paste following code for the button... Remember to reference your class library into the windows app...

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim setter As New VJ.Setter
Dim flag As Boolean
flag = setter.SetClass("I am a GoodBoy")
flag = setter.SetStructure("Life Sucks")
End Sub



Observations: The first time flag will become true next time it will be false... i.e. Even though your reflection calls on structure will execute sucessfully (without throwing any exception) the properties will not get set...
The values within the structure in this case do not get modified by reflection and so if you want to use reflection anytime for your entities make sure you declare them as classes...

There is a candy and special mention for a person who will give explanation behind the occurrance... (ofcourse more detailed that value type/reference type one... :-))

PS: Most common pets people have are either cats or dogs... Usually, people who love dogs don't like to have cats as pets and probably so is true visa versa also... Now, this "Pet Phenomenon" is slowly engulfing us to such an extent that I have heard people saying "I am DogMan... I hate cats"... and so on... Well that "DogMan" thing struck me right away... DogMan, CatWoman, DogWoman, CatMan... believe me if I were a dog and I heard a man saying that he was a 'DogMan' I would really feel proud... I think pets can have comeptition in their gatherings as to how many humans did they convert into DogMans, DogWomans and so on... Once they achieve considerable success in this mission, they will eventually move up the quality ladder and will have targets like converting DogMans into BullDogMans, GreyHoundMans and so on... :-) So the next time you want to see a smile on the face of your pet, stand in public with the pet (especially when other people's pets are around) and annonce to everyone that you are a "DogMan" or ..... :-) Btw, I am a DogMan... :-)

No comments: