Click here to Skip to main content
Click here to Skip to main content
Go to top

Declarations Generator

, 29 Jun 2010
Rate this:
Please Sign up or sign in to vote.
A tool which helps in generating codes for events/properties, so that users of your classes can use events/properties of embedded classes

Introduction

If you're developing a [User Control] (or [Class]),
If your [User Control] contains other [Controls] (Button, TextBox, Label, .......etc.)
If you wanted to expose Events/Properties of embedded controls,

You have two choices:

  1. Re-write a declaration of every Event you need, and raise them, one by one. If you want to expose all Events/ Properties, that's going to take forever. What if you have more than one Embedded Control, then you start and your student will complete it.
  2. Make the "Accessible Level" (Modifier) of the embedded controls so they're permitted to be seen out of your [User Control], like "Public".
    Then users of your [User Control] will have to write something like this to use Events:
    Dim WithEvents EmbeddedControl As New EmbeddedControlType
    
    Private Sub SomeSub()
    
        'now " UserControl1" is an object of Your UserControlType
        EmbeddedControl = UserControl1.EmbeddedControl
    End Sub
    
    Private Sub HandlerSub(ByVal Sender As Object, ByVal e As EventArgs) _
            Handles EmbeddedControl.EventName
    
            'do your thing 
    End Sub

    Notice that the user has to be aware of every single thing he is doing, because IDE won't help him creating the "Sub" "HandlerSub",………… besides that he made unnecessary things.

So …………..
Here's a tool which helps you in Generating Declarations of Events/Properties for Embedded Controls (Option number 1), so users of your [User Control] will have to write something like this to use Events:

Private Sub UserControl1_EmbeddedControlEventName( _
    ByVal Sender As Object,  ByVal e As System.EventArgs) _
    Handles UserControl1. EmbeddedControlEventName

        'do your thing
    End Sub

Besides that, the user will use the IDE to create the "Sub" "Tool_EmbeddedToolEventName".

Here is a screenshot of the tool:

DeclarationsGenerator.gif

Future Ideas

  1. Generate Properties/Events Attributes and XML-Commenting (process has started, but results are not satisfying at all in the current version).
    I could wait till the tool is ready, but I chose not to wait, because I'm not going to develop this idea anytime soon, and I wanted people to use now it if they liked it (not to wait for me).
  2. Searching in other Assemblies by allowing user to add Assemblies to search for Data Types into, it won’t take much time to program, but I didn’t support it yet.

Background

Obviously, the tool is all about Reflection, and it needs the Namespace System.Reflection to be imported:

Imports System.Reflection

One more thing, I used Extension Methods to make the code architecture more convenient and more robust, and it needed the Namespace System.Runtime.CompilerServices to be imported:

Imports System.Runtime.CompilerServices

Using the Code

For example, the code that is responsible for generating the Declaration of an Event is:

#Region "Extending the 'EventInfo' Type"
    <Extension()> _
     Private Function GetEventDeclaretion(ByVal eve As EventInfo, _
                                          ByVal ControlName As String _
                                          ) As String

        Dim DelegateType As Type = eve.EventHandlerType
        Dim Invoke As MethodInfo = DelegateType.GetMethod("Invoke")
        Dim Pars As ParameterInfo() = Invoke.GetParameters()

        Dim EventName As String = ControlName & eve.Name

        Dim ParamsNamesArr = (From p In Pars Select p.Name).ToArray
        Dim ParamsNamesStr As String = " ( " & String.Join(" , ", ParamsNamesArr) & " )"

        Dim paramsDeclarationsArr = (From p In Pars _
                                     Select (p.Name & " As " & p.ParameterType.ToString) _
                                    ).ToArray
        Dim paramsDeclarationsStr As String = " ( " & String.Join(" , ", _
			paramsDeclarationsArr) & " )"

        Dim EventInvokeMethodeName As String = String.Format("{0}{1}{2}", _
                                    ControlName, eve.Name, paramsDeclarationsStr)

        Dim RaiseEventStatment As String = vbTab & "RaiseEvent " & _
				EventName & ParamsNamesStr

        Dim EventDeclaration As String = "Public Event " & _
				EventInvokeMethodeName & vbCrLf
        EventDeclaration &= String.Format("Private Sub m_{0} _
				Handles {1}.{2}{4}{3}{4}End Sub{4}", _
                                      EventInvokeMethodeName, ControlName, eve.Name, _
                                      RaiseEventStatment, vbCrLf)
        Return EventDeclaration
    End Function
#End Region

So later, you can use it as next:

Dim Eve As EventInfo = GetType(SomeType).GetEvent("SomeEvent")
Dim Declaration As String = Eve.GetEventDeclaretion()

History

The idea was created a while ago when I wanted to make a UserControl which contains the famous combination (BrowseButton + PathTextBox). I wanted users to have access to embedded controls members. I searched all over the internet, everybody said that I have to hardcode them, so I made this tool, and used it. It was like magic to me.

Some days ago, I wanted to share this tool with you guys, so some future ideas came to my mind, but because I don't intend to develop it anytime soon, I put it the way it was.

Version 0.0.0.3 generates Events and Properties (recommended, stable). I started to support Attributes generating in Version (0.0.0.4), but results was not satisfying at all (choosing the right constructor isn't working).

I hope it helps somebody. Thanks for reading.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

Share

About the Author

vbnetskywalker
Software Developer
Syrian Arab Republic Syrian Arab Republic
The more I learn the more I see my ignorance.
To see all programs and tools I've made head to my Shared Folder

Comments and Discussions

 
General.. cannot download .. Link broken Pinmembertridex29-Jun-10 3:06 
.. cannot download .. Link broken
 
Please fix Smile | :)
GeneralRe: .. cannot download .. Link broken Pinmembervbnetskywalker29-Jun-10 6:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 29 Jun 2010
Article Copyright 2010 by vbnetskywalker
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid