-
Dec 19th, 2013, 12:26 PM
#1
Class/Module Creator
I was presented with a challenge the other day where somebody wanted to create classes without having to type out all the Get/Set and variable names(lazy I know!). So this is what I came up with and I thought I'd share it:
Code:
Option Strict On
Option Explicit On
Class VbCodeGenerator
Public Enum Type
[Class]
[Structure]
End Enum
Private _type As Type = Type.Class
Public Property ObjectType() As Type
Get
Return _type
End Get
Set(ByVal value As Type)
_type = value
End Set
End Property
Private _name As String
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Private _properties() As [Object]
Public Property Properties() As [Object]()
Get
Return _properties
End Get
Set(ByVal value() As [Object])
_properties = value
End Set
End Property
Public Structure [Object]
Public Property Name As String
Public Enum PropertyType
[Boolean]
[Byte]
[Char]
[Date]
[Decimal]
[Double]
[Integer]
[Long]
[Object]
[SByte]
[Short]
[Single]
[String]
[UInteger]
[ULong]
[UShort]
End Enum
Public Property Type As PropertyType
End Structure
Public Function CreateObject() As String
Dim code As String = String.Empty
'Replace any spaces with an underscore
_name = _name.Replace(" ", "_")
'Replace any special characters
Dim special() As String = {"~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "+", "=", "{", "[", "}", "]", "|", "\", ":", ";", """", "'", "<", ",", ">", ".", "?", "/"}
For Each c As String In special
_name = _name.Replace(c, String.Empty)
Next
'Generate:
'Option Strict On
'Option Explicit On
'Public <structure or class> <name>
'
code &= String.Format("Option Strict On{0}Option Explicit On{0}Public {1} {2}{0}", Environment.NewLine, _type, _name)
For Each itm As [Object] In _properties
code &= Environment.NewLine
'Replace any spaces with an underscore
itm.Name = itm.Name.Replace(" ", "_")
'Replace any special characters
For Each c As String In special
itm.Name = itm.Name.Replace(c, String.Empty)
Next
'The variable will either be:
'_<name>
'm_<name>
Dim variable As String
'Check if the name begins with an under score
If itm.Name.Substring(0, 1) = "_" Then
variable = "m" & itm.Name
Else
variable = "_" & itm.Name
End If
'Write out:
'Private <variable> As <Type>
'Public Property <name> As <Type>
'Get
'Return <variable>
'End Get
'Set(ByVal value As <Type>)
'<variable> = value
'Me.On<name>Changed()
'End Set
'End Property
'
'Protected Overridable Sub On<name>Changed()
'RaiseEvent <name>Changed(Me, EventArgs.Empty)
'End Sub
'
'Public Event <nameChanged(ByVal sender As Object, ByVal e As EventArgs)
code &= String.Format("Private {1} As {2}{0}Public Property {3} As {2}{0}Get{0}Return{1}{0}End Get{0}Set(ByVal value As {2}){0}{1} = value{0}Me.On{3}Changed(){0}End Set{0}End Property{0}{0}Protected Overridable Sub On{3}Changed(){0}RaiseEvent {3}Changed(Me, EventArgs.Empty){0}End Sub{0}{0}Public Event {3}Changed(ByVal sender As Object, ByVal e As EventArgs)", _
Environment.NewLine, variable, itm.Type, itm.Name)
Next
code &= String.Format("{0}End {1}", Environment.NewLine, _type)
Return code
End Function
End Class
I hope that y'all enjoy it and if you see something that I can improve on it just let me know.
Last edited by dday9; Feb 3rd, 2015 at 03:59 PM.
Reason: Added Events
-
Dec 20th, 2013, 04:44 PM
#2
Re: Class/Module Creator
If you wanna have real fun, you should have a "Version" flag that indicates what version of VB it should use. Reason? VS2010+ (and the associated VB compiler) supports one-line properties
-
Dec 20th, 2013, 05:56 PM
#3
Re: Class/Module Creator
Very true. But the reason I didn't do them as one-liners is incase if they decided to create some methods that need to reference the variable that gets/sets the property.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|