class to return an object

false74

Well-known member
Joined
Aug 4, 2010
Messages
76
Programming Experience
Beginner
I have a class i've called "buttons", which passes in 1 variable, the button text, and then returns a new button with that text. however, i do not know how to get the class, when called to return a button.

similar to how 'toString' works, except it would return a button object instead of a string. (im not sure how toString works in vb.net. in java it is automatically called when you call the new class, so newclass and newclass.toString(); return the same)

VB.NET:
Dim newbutton= New buttons("TEST")   ' this creates a button, and i want to be able to return a button when called

Me.Controls.Add(newbutton)

is this possible?
 
Last edited:
What you're asking for is nothing like ToString. ToStringreturns a String representation of the object it's called on. How the String representation is created differs from type to type. What you're talking about is a "factory". You could create a ButtonFactory class like so:
VB.NET:
Public Class Buttonfactory

    Public Shared Function Create(ByVal text As String) As Button
        Return New Button With {.Text = text}
    End Function

End Class
You can then use it like so:
VB.NET:
Dim newButton = ButtonFactory.Create("TEST")
 
ah I see. OK. that's what I thought would be the best approach, but I wasn't sure if there was an alternative to do this. Thanks!
 
Back
Top