Digital ink
Digital ink

Reputation: 303

VB.net function that returns an object that is specified by the input

I am trying to write a function that returns a newly created object ( a form ) that is specified by the input. I'm having trouble with how to work out the concept of giving a type as an input then creating an object of that type in the body of the function. Here is an outline of what I'm working on.

Public Function MakeMyForm(ByVal frmType as Form) as Form
  Dim NewObj as New frmType
  Return NewObj
End Function

I'd like to be able to call the function in this way:

Dim myform as CustomFormType
myform = MakeMyForm(CustomFormType)

Can my concept be accomplished in VB.net?

Upvotes: 0

Views: 244

Answers (4)

This is usually done using generics in a function such as:

Public Function GetItem(Of T)(key As String) As T

Usage:

myIntVar = myFoo.GetItem(Of Int32)(bar)

The purpose of which is for the code calling it to specify how it needs the return. In the above a whole bunch of data has been serialized and the original Type lost, so when fetching it back, the Of T helps convert it rather than using Object as the return.

For forms, it is more problematic:

Public Function MakeAForm(Of T)() As Form   ' cant do As T

You'd have to add more code to cast Form to Form1 or frmCust to avoid tbName is not a member of System.Windows.Forms.Form errors. Even the correct way as shown by Mr Dokjnas present problems trying to do more with the form:

Public Function MakeAForm(Of T As {New, Form})() As T

    Dim frm As New T
    If frm.GetType Is frm8088.GetType Then

        frm.textbox1.text = "ziggy"       ' error
    End If
    Return frm

Here, it is 'TextBox is not a member of T`. If your forms were compiled to a ClassLib so the IDE could know more about the Types (forms) you could get it to work. But the first sign of futility is revealed in using it:

    Dim frm As Form = MakeAForm(Of frm8100VI)()
    frm.Show()

It takes more code to call the FormMaker than to just create an instance.

Upvotes: 0

senthilkumar2185
senthilkumar2185

Reputation: 2566

 Public Function Makemyform(ByVal frmType As Form) As Form
        Dim obj As Form
        obj = newfunc(frmType)
        Return obj
    End Function
    Public Function newfunc(ByVal mytype As Form) As Form
        Return New Form
    End Function

Upvotes: 0

Dave Doknjas
Dave Doknjas

Reputation: 6542

Ok, if I understand you, you just want a generic method:

Public Function MakeMyForm(Of T As {New, Form})() As T
    Return New T()
End Function

and call it like this:

Dim myform As CustomFormType = MakeMyForm(Of CustomFormType)()

of course, why wouldn't you just use:

Dim myform As New CustomFormType()

Upvotes: 2

Afnan Makhdoom
Afnan Makhdoom

Reputation: 654

Well you can try this:

Dim frmnew() As Form
Dim createdforms As Integer = 0

Private Sub createform(wintext As String, height As Integer, width As Integer, backcolor As Color, topmost As Boolean, formborderstyle As FormBorderStyle, winstate As FormWindowState, opacity As Decimal, startposition As FormStartPosition, enabled As Boolean) 'add as many properties as you like
    ReDim Preserve frmnew(createdforms)
    frmnew(createdforms) = New Form
    With frmnew(createdforms)
        .Text = wintext
        .Height = height
        .Width = width
        .BackColor = backcolor
        .TopMost = topmost
        .FormBorderStyle = formborderstyle
        .WindowState = winstate
        .Opacity = opacity
        .StartPosition = startposition
        .Enabled = enabled
    End With
    frmnew(createdforms).Show()
    createdforms += 1
End Sub

and you can test it with the code below:

createform("Afnan Makhdoom", 500, 700, Color.Aqua, False, Windows.Forms.FormBorderStyle.Fixed3D, FormWindowState.Normal, 0.9, FormStartPosition.CenterScreen, True)

Upvotes: 0

Related Questions