473,657 Members | 2,411 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 1189
* "Stephen" <St*****@johnst ontrading.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_Initializ e 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 ArgumentNullExc eption("name", "Name cannot be
nothing")
ElseIf name = String.Empty Then
Throw New ArgumentOutOfRa ngeException("n ame", 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("Herfrie d")

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*****@johnst ontrading.com> wrote in message
news:eR******** ******@TK2MSFTN GP09.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 unless I create a constructor in my two other classes like this: public ClassName(string szParameter1, string szParameter2, string szParameter3) : base(szParameter1, szParameter2, szParameter3) { }
15
2375
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 do "delete xp", the delete procedure (not sure if that's the right terminology) obviously knows how many objects were allocated by the corresponding "new" call. So, why can't it just know whether you did "new x" or "new x"?
1
1691
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, Subsection 2: Class Hierarchies, pages 38-40 implemented in C99. There are four classes: 1.) Point, 2.) Color, 3.) Shape and
10
2161
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 is conversion of coordinates from one frame of reference to another: spherical coordinates in the ecliptic frame of reference, spherical coordinates in the equatorial frame of reference, 3D rectangular coordinates in either, galactic coordinates,...
1
1049
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: 'Vector3' : multiple copy constructors specified 2. error C2668: 'Vector3::set' : ambiguous call to overloaded function I get the second one twice, once from each of these lines of code: u.set(up.cross(n));
3
1341
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 didn't override nor rewrite any of its constructors. When I try to compile I get the error : No overloaded method for "MyDerivedClassConstructor" takes 1 argument.
12
1768
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 ) { S = aS;
2
1924
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 higher than the lower corner. I write a code as below class Domain { private:
10
2103
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 #these are the operations + = , - = , * = , 1/ = only if 0 not in .
0
8399
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8312
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8827
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8504
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7337
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
2732
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1959
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1622
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.