Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Properties
#1
Properties are essential if you are creating a custom object. Essentially, properties are the things that can be edited in the "visual designer" - all of the things that are accessible via the properties window in the lower right hand corner of Visual Studio.

Adding basic properties to your form is easy. All you need is an instance variable and a property block. Then you're all set. For example, here is a sample property arrangemenet:
[code2=vbnet]Private _messageString As String
Public Property MessageString As String
Get
Return _messageString
End Get
Set(value As String)
_messageString = value
End Set
End Property[/code2]

Of course, in this example we use a String object. In the real world, any object can be used. After this is added to your object, the user will be able to edit the properties via the properties Window and give this variable a specific value.

What if you wanted to give the user a list of options to choose from, though? This can be doing by combining properties with enumerations. Thus:
[code2=vbnet]Private _messageString As messageIdentifier
Public Property MessageString As messageIdentifier
Get
Return _messageString
End Get
Set(value As messageIdentifier)
_messageString = value
End Set
End Property

Enum messageIdentifier As Integer
Greetings = 1
Goodbyes = 2
Warning = 3
Declaration = 4
End Enum[/code2]

Although the above code snippet may seem rather ridiculous since the Enumerations don't actually correspond to anything, this is a way of offering the user a list of options to choose from. In this case, the list will contain "Greetings", "Goodbyes", "Warning", and "Declaration". You can then work with this choice by processing the enumeration in some way, as such:
[code2=vbnet]Private Function GetMessageString() As String
If (_messageString = messageIdentifier.Declaration) Then
Return "Of Independence"
ElseIf (_messageString = messageIdentifier.Goodbyes) Then
Return "It has been a fun time, but I must leave you."
ElseIf (_messageString = messageIdentifier.Greetings) Then
Return "Salutations, my good man/woman"
ElseIf (_messageString = messageIdentifier.Warning) Then
Return "Back off! You have no business in this area."
Else
Return ""
End If
End Function[/code2]


Thus, it is really easy to give the user the ability to edit properties when they add your custom control to their form. The only downside occurs when you update a property to be a different object, but do not change the value of the property. Massive errors ensue.

View the video here: <!-- m --><a class="postlink" href="http://www.youtube.com/watch?v=JAK6veamu8o&feature=youtu.be">http://www.youtube.com/watch?v=JAK6veam ... e=youtu.be</a><!-- m -->

Feel free to download a sample project:
[attachment=0]<!-- ia0 -->PropertyTutorial.zip<!-- ia0 -->[/attachment]


Attached Files
.zip   PropertyTutorial.zip (Size: 74.13 KB / Downloads: 610)
My Blog | My Setup | My Videos | Have a wonderful day.


Messages In This Thread
Properties - by brandonio21_phpbb3_import2 - 04-11-2013, 05:20 PM
Re: Properties - by brco900033 - 04-27-2013, 05:19 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)