I’m a beginner in vb.net programming and I’m slightly confused with creating controls. It seems that when I’m online the following code I see is used interchangeably:
Dim x As Button
Dim y As New Button()
or even
Dim z As New System.Windows.Forms.Button()
Does it matter how I declare the variable? Thanks in advance.
System.windows.forms.Buttonis the same as declaringButton. You just already have theSystem.windows.formsnamespace imported. This is unless you have created your own button class in a different namespace, but I doubt you have.The brackets after the name of the class will create an array of undefined size of that class. No brackets is creating one instance of that class.
The
newkeyword is required when creating new instances of a control. Without it, the variable you created will need to be assigned to an existing object of the same type. Seeing as you are wanting to create buttons programmatically, you should use thenewkeyword.Have a look at these tutorials for a basic introduction to creating classes
http://www.homeandlearn.co.uk/net/nets11p2.html
http://visualbasic.about.com/od/quicktips/qt/shared_member.htm