473,473 Members | 1,901 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

calling New with arguments - Syntax question

Hi, I have a class with lots of constructors - one is shown below

Public Sub New(ByVal rows As Integer, ByVal columns As Integer)
InitializeComponent()
'And code in here that sets instance variables,
'and does calculations based on rows and columns passed
'in in the constructor, and lots more.
End Sub

Another of my constructors has no parameters, and there are various
other
constructors that receive, for example, Point etc. But what I want to
do is
call one constructor New from another constructor New so I don't have
to reproduce the same code many times, or put it in a separate
procedure and call it from each New(..)

Here is what I am wanting to do, but the compiler is having none of it
!

Public Class test
Inherits System.Windows.Forms.Form
Public Sub New(ByVal rows As Integer, ByVal columns As Integer)
InitializeComponent()
'And code in here that sets instance variables,
'and does calculations based on rows and columns passed
'in in the constructor, and lots more.
End Sub

Public Sub New()
Dim defaultRows As Integer=2
Dim defaultColumns As Integer=10
Call New(defaultRows,defaultColumns)
End Sub

'More methods/code etc
End Class

Can someone cast their eye over this and help me along with the
correct syntax (the line "Call New(defaultRows,defaultColumns" is the
line the compiler balks at).

Thank you
Colin
Nov 20 '05 #1
4 1115
Colin,
Can someone cast their eye over this and help me along with the
correct syntax (the line "Call New(defaultRows,defaultColumns" is the
line the compiler balks at).


Public Sub New()
MyClass.New(2, 10)
End Sub

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 20 '05 #2
Colin,
In addition to Mattias example.

The call to MyClass.New or MyBase.New needs to be the first statement in the
constructor, before any other statements, or variable or constant
declarations.

Also you need to include the MyClass or MyBase so the compiler knows that
you are chaining to another constructor and that constructor is either in
the base class (MyBase) or the current class (MyClass).

And as Mattias showed, Call is optional hence I tend to omit it.

Hope this helps
Jay

"Colin McGuire" <co***********@lycos.co.uk> wrote in message
news:ab*************************@posting.google.co m...
Hi, I have a class with lots of constructors - one is shown below

Public Sub New(ByVal rows As Integer, ByVal columns As Integer)
InitializeComponent()
'And code in here that sets instance variables,
'and does calculations based on rows and columns passed
'in in the constructor, and lots more.
End Sub

Another of my constructors has no parameters, and there are various
other
constructors that receive, for example, Point etc. But what I want to
do is
call one constructor New from another constructor New so I don't have
to reproduce the same code many times, or put it in a separate
procedure and call it from each New(..)

Here is what I am wanting to do, but the compiler is having none of it
!

Public Class test
Inherits System.Windows.Forms.Form
Public Sub New(ByVal rows As Integer, ByVal columns As Integer)
InitializeComponent()
'And code in here that sets instance variables,
'and does calculations based on rows and columns passed
'in in the constructor, and lots more.
End Sub

Public Sub New()
Dim defaultRows As Integer=2
Dim defaultColumns As Integer=10
Call New(defaultRows,defaultColumns)
End Sub

'More methods/code etc
End Class

Can someone cast their eye over this and help me along with the
correct syntax (the line "Call New(defaultRows,defaultColumns" is the
line the compiler balks at).

Thank you
Colin

Nov 20 '05 #3
You can also call Me.New(etc, etc)

--
Michael Caputo
Programmer/Database Administrator
Simon Economic Systems Ltd.

"Colin McGuire" <co***********@lycos.co.uk> wrote in message
news:ab*************************@posting.google.co m...
Hi, I have a class with lots of constructors - one is shown below

Public Sub New(ByVal rows As Integer, ByVal columns As Integer)
InitializeComponent()
'And code in here that sets instance variables,
'and does calculations based on rows and columns passed
'in in the constructor, and lots more.
End Sub

Another of my constructors has no parameters, and there are various
other
constructors that receive, for example, Point etc. But what I want to
do is
call one constructor New from another constructor New so I don't have
to reproduce the same code many times, or put it in a separate
procedure and call it from each New(..)

Here is what I am wanting to do, but the compiler is having none of it
!

Public Class test
Inherits System.Windows.Forms.Form
Public Sub New(ByVal rows As Integer, ByVal columns As Integer)
InitializeComponent()
'And code in here that sets instance variables,
'and does calculations based on rows and columns passed
'in in the constructor, and lots more.
End Sub

Public Sub New()
Dim defaultRows As Integer=2
Dim defaultColumns As Integer=10
Call New(defaultRows,defaultColumns)
End Sub

'More methods/code etc
End Class

Can someone cast their eye over this and help me along with the
correct syntax (the line "Call New(defaultRows,defaultColumns" is the
line the compiler balks at).

Thank you
Colin

Nov 20 '05 #4
Mike,
That's right! I was thinking there was a third syntax. Thanks for the
additional info.

I normally use either MyBase or MyClass in the constructor so I "forgot"
that Me was supported.

Thanks
Jay

"Mike Caputo" <mi************@radarwire.com> wrote in message
news:e%***************@tk2msftngp13.phx.gbl...
You can also call Me.New(etc, etc)

--
Michael Caputo
Programmer/Database Administrator
Simon Economic Systems Ltd.

"Colin McGuire" <co***********@lycos.co.uk> wrote in message
news:ab*************************@posting.google.co m...
Hi, I have a class with lots of constructors - one is shown below

Public Sub New(ByVal rows As Integer, ByVal columns As Integer)
InitializeComponent()
'And code in here that sets instance variables,
'and does calculations based on rows and columns passed
'in in the constructor, and lots more.
End Sub

Another of my constructors has no parameters, and there are various
other
constructors that receive, for example, Point etc. But what I want to
do is
call one constructor New from another constructor New so I don't have
to reproduce the same code many times, or put it in a separate
procedure and call it from each New(..)

Here is what I am wanting to do, but the compiler is having none of it
!

Public Class test
Inherits System.Windows.Forms.Form
Public Sub New(ByVal rows As Integer, ByVal columns As Integer)
InitializeComponent()
'And code in here that sets instance variables,
'and does calculations based on rows and columns passed
'in in the constructor, and lots more.
End Sub

Public Sub New()
Dim defaultRows As Integer=2
Dim defaultColumns As Integer=10
Call New(defaultRows,defaultColumns)
End Sub

'More methods/code etc
End Class

Can someone cast their eye over this and help me along with the
correct syntax (the line "Call New(defaultRows,defaultColumns" is the
line the compiler balks at).

Thank you
Colin


Nov 20 '05 #5

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

Similar topics

6
by: dw | last post by:
Hello all, I'm having a dickens of a time calling a stored procedure on a connection. Every time I do, it generates an error "Arguments are of the wrong type, are out of acceptable range, or are in...
3
by: Yitzhak | last post by:
I am having "Permission denied" error while calling LogEvent method of WScript.Shell component. Basically, ASP page calls Windows Script Host Shell component to log events to the OS Application...
5
by: Alex Lyman | last post by:
Does anyone have any code handy (or know what a good direction for me to head in), to call functions, if you have an address of the function, its declspec (for my app, it's limited to _stdcall and...
5
by: deko | last post by:
I'd like to use a bit of code in the OnOpen event of a report: =rptOpen(Me.ReportName), (Me.Tag) --this doesn't work This does work: Private Sub Report_Open(Cancel As Integer)...
10
by: headware | last post by:
I know that you can call the method of one from from inside another form by doing something like this Forms("MyForm").MyFunction(12, 34) However, you have to know that MyForm has a function...
6
by: Melkor Ainur | last post by:
Hello, I'm attempting to build an interpreter for a pascal-like language. Currently, I don't generate any assembly. Instead, I just build an abstract syntax tree representing what I've parsed...
20
by: lars.uffmann | last post by:
I have to work on a rather big project that a bunch of people wrote and that has no useful documentation at all, my C++ has rusted in a bit, now I stumbled over a constructor that looks something...
3
by: Jason | last post by:
I have an ASP.NET application in which I would like to call my button click event (imgSubmitSearch_Click) on the page load if certain criteria are met. Is this possible? What is the correct...
6
Soniad
by: Soniad | last post by:
Hello, I am excecuting a stored procedure in my ASP page , it has one out parameter (@confirm) . after executing the procedure i want to retreive this out parameter and assign it to variable...
0
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
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
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,...
0
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...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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.