473,505 Members | 15,976 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

need a little help with constructors

Im new to VB.net....Im having a problem understandign how a constructor relates to the base form and a class.

What is the function on the constructor ..does it accept variables passed to it from the form?

Steve
Nov 20 '05 #1
2 1186
* "Stephen" <St*****@johnstontrading.com> scripsit:
Im new to VB.net....Im having a problem understandign how a
constructor relates to the base form and a class.

What is the function on the constructor ..does it accept variables
passed to it from the form?


Please don't post in HTML format.

The constructor gets called when the instance of the class is created.
It contains "initialization" code. First, the base class's constructor
will be called (you can do this explicitly by calling 'MyBase.New()'),
then you can execute your own initialization code. You can parameterize
the constructor to privide data to the initialization process.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #2
Stephen,
In addition to Herfried's comments.

If you used classes in VB6 the constructor is similar to the
Class_Initialize event. In that they are both executed when the object is
created, allowing you to ensure that the class is fully initialized when it
is created.

The advantage that VB.NET's constructors have over VB6's Class Initialize
event is that you can parameterize them! Which means you cannot create an
instance of the object without supplying all the parameters to the
constructor. You can also add code to the constructor to validate the
parameters!

To define a constructor in VB.NET you add a 'Sub New' to your class or
structure. The constructor is always called 'New' in VB.NET.

For example the following Person class requires a name when you create a
person object, this name cannot be Nothing, nor can it be an empty string.
The person's name cannot be changed later.

Public Class Person

Private Readonly m_name As String

Public Sub New(ByVal name As String)
If name Is Nothing Then
Throw New ArgumentNullException("name", "Name cannot be
nothing")
ElseIf name = String.Empty Then
Throw New ArgumentOutOfRangeException("name", name, "Name
cannot be an empty")
End If
m_name = name
End Sub

Public Readonly Property Name() As String
Get
Return m_name
End Get
End Property

Public Overrides Function ToString() As String
Return String.Format("Person({0})", m_name)
End Function

End Class

Dim jay As New Person("Jay")
Dim herfried As Person
person = New Person("Herfried")

Robin A. Reynolds-Haertle's book "OOP with Microsoft Visual Basic .NET and
Microsoft Visual C# .NET - Step by Step" from MS Press covers this plus the
rest of OOP in VB.NET, in a very straight forward manner.

Hope this helps
Jay

"Stephen" <St*****@johnstontrading.com> wrote in message
news:eR**************@TK2MSFTNGP09.phx.gbl...
Im new to VB.net....Im having a problem understandign how a constructor
relates to the base form and a class.

What is the function on the constructor ..does it accept variables passed to
it from the form?

Steve
Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
318
by: Doug | last post by:
I have a public abstract class that I want to inherit from in two other classes. My public abstract one has a constructor with several parameters. For some reason I cannot get to that constructor...
15
2362
by: Roy Smith | last post by:
I understand that "delete xp" deletes a scalar object and "delete xp" deletes an array of objects, but what I don't understand is why you need to tell the compiler which you're doing. When you...
1
1685
by: E. Robert Tisdale | last post by:
Please find attached a copy of the "Shape class" example from Bjarne Stroustrup, "The C++ Programming Language: Third Edition", Chapter 2: A Tour of C++, Section 6: Object-Oriented Programming,...
10
2146
by: Tom Dacon | last post by:
I'm curious to see if anyone has an opinion on this little design question - I'm doing a computational astronomy library in C#, purely for my own use, and one of the things that happens regularly...
1
1038
by: Brett Baisley | last post by:
I got an example that works at school, and it should work here as well. The problem I think is that VS.net won't allow multiple constructors. Here are the 3 errors I get: 1. warning C4521:...
3
1337
by: craigkenisston | last post by:
Hi, I have a class with 3 constructors, one takes no arguments, other take an string and other an xml document. I want to derive another class of it, I don't need a different constructor so I...
12
1751
by: Oleg Subachev | last post by:
I am moving from Delphi to C# and hve encountered the problem: I have the following classes and form Load event handler: public class class1 { public string S; public class1( string aS ) {...
2
1915
by: shuisheng | last post by:
Dear All, Assume I have a class for a cuboid domain. The domain is defined by the cuboid's lower corner, such as (0, 0, 0), and upper corner, such as (1, 1, 1). The upper corner should be always...
10
2087
by: CuTe_Engineer | last post by:
hii, i have cs assignment i tried to solve it but i still have many errors , plzz help mee :"< it`s not cheating becuz i`ve tried & wrote the prog. i just wanna you to show me my mistakes ...
0
7218
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7103
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7307
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7021
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5035
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4701
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3188
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
409
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.