473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Is default constructor always created?

I want to create a class that must receive a parameter when instantiated.
In other words, I do not want it to have a "Public Sub New()".

1) Does VB.NET create a default public constructor if I do not declare one?

2) I've read that I can have a "Private Sub New()" which should take care of
my needs. Comments?

Thanks,
Andrew
Nov 20 '05 #1
19 6519
Andrew,
1) Does VB.NET create a default public constructor if I do not declare one?
VB.NET creates a default public constructor if you do not declare ANY
constructors, if you define a constructor in the class, the default public
constructor is not created. Remember constructors are NOT inherited.
2) I've read that I can have a "Private Sub New()" which should take care of my needs. Comments? You only need a Private Sub New if its the only constructor and you want to
prevent others from creating the class. Or you have a Form, Control or
Component as the VS.NET Designers require a default constructor, and you
don't want the default constructor from being used. I would consider making
it Protected in this case.

Hope this helps
Jay
"Andrew J. Marshall" <An*************@ObjectVision.net> wrote in message
news:e0**************@TK2MSFTNGP11.phx.gbl... I want to create a class that must receive a parameter when instantiated.
In other words, I do not want it to have a "Public Sub New()".

1) Does VB.NET create a default public constructor if I do not declare one?
2) I've read that I can have a "Private Sub New()" which should take care of my needs. Comments?

Thanks,
Andrew

Nov 20 '05 #2
In article <e0**************@TK2MSFTNGP11.phx.gbl>, Andrew J. Marshall wrote:
I want to create a class that must receive a parameter when instantiated.
In other words, I do not want it to have a "Public Sub New()".

1) Does VB.NET create a default public constructor if I do not declare one?

Yes, unless you add a parameterized constructor. In that case, the
compiler will not create a default constructor. So basically, all you
have to do is add the constructor you want, and no default will be
created.
2) I've read that I can have a "Private Sub New()" which should take care of
my needs. Comments?


Well, unless you want to do stuff in the private constructor there is no
need to do this.

--
Tom Shelton [MVP]
OS Name: Microsoft Windows XP Professional
OS Version: 5.1.2600 Service Pack 1 Build 2600
System Up Time: 0 Days, 21 Hours, 9 Minutes, 45 Seconds
Nov 20 '05 #3
AFAIK The default blank constructor is only provided as long as you don't
provide any other constructor. If you provide any other constructor (public,
private, with parameters etc.) then it won't automatically put the default
one in - you can still specify it yourself if you want.

It's yet another thing that VB tries to hide from the developer. As a
convention I always manually put a blank constructor in the class if it
supports it - even if it is only a call to MyBase.New() - just for clarity.

HTH,

Trev.
"Andrew J. Marshall" <An*************@ObjectVision.net> wrote in message
news:e0**************@TK2MSFTNGP11.phx.gbl...
I want to create a class that must receive a parameter when instantiated.
In other words, I do not want it to have a "Public Sub New()".

1) Does VB.NET create a default public constructor if I do not declare one?
2) I've read that I can have a "Private Sub New()" which should take care of my needs. Comments?

Thanks,
Andrew

Nov 20 '05 #4
Andrew,

* "Andrew J. Marshall" <An*************@ObjectVision.net> scripsit:
I want to create a class that must receive a parameter when instantiated.
In other words, I do not want it to have a "Public Sub New()".

1) Does VB.NET create a default public constructor if I do not declare one?
It will only be created if you don't create any other ctor.
2) I've read that I can have a "Private Sub New()" which should take care of
my needs. Comments?


If you want to forbid any instantiation, yes. If you want to forbid
instantiation by using a parameterless ctor, define a parameterized ctor
only and do not define any parameterless ctor.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #5
Trev,
It's yet another thing that VB tries to hide from the developer. I don't think the "Default constructor" being added is a trait of VB.NET per
se, as Java, C#, and C++ do the same thing.

Hope this helps
Jay

"Trev Hunter" <hu*********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl... AFAIK The default blank constructor is only provided as long as you don't
provide any other constructor. If you provide any other constructor (public, private, with parameters etc.) then it won't automatically put the default
one in - you can still specify it yourself if you want.

It's yet another thing that VB tries to hide from the developer. As a
convention I always manually put a blank constructor in the class if it
supports it - even if it is only a call to MyBase.New() - just for clarity.
HTH,

Trev.
"Andrew J. Marshall" <An*************@ObjectVision.net> wrote in message
news:e0**************@TK2MSFTNGP11.phx.gbl...
I want to create a class that must receive a parameter when instantiated. In other words, I do not want it to have a "Public Sub New()".

1) Does VB.NET create a default public constructor if I do not declare one?

2) I've read that I can have a "Private Sub New()" which should take

care of
my needs. Comments?

Thanks,
Andrew


Nov 20 '05 #6
Cheers Jay. I stand corrected.

Doesn't mean I like having to define a private or friend constructor just to
override the default behaviour of a class being publicly creatable. I'd much
rather the default behaviour was to not make the class createble unless you
explicitly defined a constructor (which is something the IDE could add in as
part of code completion maybe).

Just my 2c in my little explicit world ;)

Trev.
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Trev,
It's yet another thing that VB tries to hide from the developer. I don't think the "Default constructor" being added is a trait of VB.NET

per se, as Java, C#, and C++ do the same thing.

Hope this helps
Jay

"Trev Hunter" <hu*********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
AFAIK The default blank constructor is only provided as long as you don't
provide any other constructor. If you provide any other constructor

(public,
private, with parameters etc.) then it won't automatically put the default one in - you can still specify it yourself if you want.

It's yet another thing that VB tries to hide from the developer. As a
convention I always manually put a blank constructor in the class if it
supports it - even if it is only a call to MyBase.New() - just for

clarity.

HTH,

Trev.
"Andrew J. Marshall" <An*************@ObjectVision.net> wrote in message
news:e0**************@TK2MSFTNGP11.phx.gbl...
I want to create a class that must receive a parameter when

instantiated. In other words, I do not want it to have a "Public Sub New()".

1) Does VB.NET create a default public constructor if I do not declare

one?

2) I've read that I can have a "Private Sub New()" which should take

care
of
my needs. Comments?

Thanks,
Andrew



Nov 20 '05 #7
Trev,
I'm sorry that does not make sense! ;-)

If I'm reading you correctly, you are suggesting that you want to have to
add a public default constructor if you want a publicly created class, which
I suspect is what the majority of developers will want. As opposed adding a
private default constructor in the minority of the cases?

I'm excluding the cases where you have non-default constructors
(parameterized constructors) as you have to add them any way.

Hope this helps
Jay

"Trev Hunter" <hu*********@hotmail.com> wrote in message
news:e5**************@TK2MSFTNGP11.phx.gbl...
Cheers Jay. I stand corrected.

Doesn't mean I like having to define a private or friend constructor just to override the default behaviour of a class being publicly creatable. I'd much rather the default behaviour was to not make the class createble unless you explicitly defined a constructor (which is something the IDE could add in as part of code completion maybe).

Just my 2c in my little explicit world ;)

Trev.
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Trev,
It's yet another thing that VB tries to hide from the developer.

I don't think the "Default constructor" being added is a trait of VB.NET

per
se, as Java, C#, and C++ do the same thing.

Hope this helps
Jay

"Trev Hunter" <hu*********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
AFAIK The default blank constructor is only provided as long as you don't provide any other constructor. If you provide any other constructor

(public,
private, with parameters etc.) then it won't automatically put the default one in - you can still specify it yourself if you want.

It's yet another thing that VB tries to hide from the developer. As a
convention I always manually put a blank constructor in the class if it supports it - even if it is only a call to MyBase.New() - just for

clarity.

HTH,

Trev.
"Andrew J. Marshall" <An*************@ObjectVision.net> wrote in message news:e0**************@TK2MSFTNGP11.phx.gbl...
> I want to create a class that must receive a parameter when

instantiated.
> In other words, I do not want it to have a "Public Sub New()".
>
> 1) Does VB.NET create a default public constructor if I do not declare one?
>
> 2) I've read that I can have a "Private Sub New()" which should take

care
of
> my needs. Comments?
>
> Thanks,
> Andrew
>
>



Nov 20 '05 #8
I suspect is what the majority of developers will want.
As opposed adding a private default constructor in the
minority of the cases?
I'm not arguing that a blank public constructor shouldn't be added by
default, I'm arguing that it shouldn't be hidden. It could be added
automatically by the code editor - the same way the IDE sticks in "End If"
constructs. I realize not everyone uses an IDE, and I know this goes against
the grain of most developers creating a class and expecting it to be
creatable by default, but I tend to write code so as to make it as explicit
as possible and this is one of those things that I like to make explicit.
I'd much rather delete the default Public Constructor than implement a
private constructor (that overrides some hidden constructor) in order to
make a class non-creatable.

I don't expect that this will behaviour never be changed, but one of the
reasons I tend to make things explicit is that I've been caught out too many
times in the past by double standards in VB - for example if you use "Dim"
to declare a field at class level leaving out a scope marker - its scope is
private. However if you declare a class without a scope marker, its scope is
friend.

Sorry for rambling on a crappy technicality. I'm just in a talkitive mood
today ;)

Trev.
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:OZ**************@TK2MSFTNGP09.phx.gbl... Trev,
I'm sorry that does not make sense! ;-)

If I'm reading you correctly, you are suggesting that you want to have to
add a public default constructor if you want a publicly created class, which I suspect is what the majority of developers will want. As opposed adding a private default constructor in the minority of the cases?

I'm excluding the cases where you have non-default constructors
(parameterized constructors) as you have to add them any way.

Hope this helps
Jay

"Trev Hunter" <hu*********@hotmail.com> wrote in message
news:e5**************@TK2MSFTNGP11.phx.gbl...
Cheers Jay. I stand corrected.

Doesn't mean I like having to define a private or friend constructor just
to
override the default behaviour of a class being publicly creatable. I'd much
rather the default behaviour was to not make the class createble unless

you
explicitly defined a constructor (which is something the IDE could add in as
part of code completion maybe).

Just my 2c in my little explicit world ;)

Trev.
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in

message news:%2****************@TK2MSFTNGP12.phx.gbl...
Trev,
> It's yet another thing that VB tries to hide from the developer.
I don't think the "Default constructor" being added is a trait of VB.NET
per
se, as Java, C#, and C++ do the same thing.

Hope this helps
Jay

"Trev Hunter" <hu*********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
> AFAIK The default blank constructor is only provided as long as you

don't
> provide any other constructor. If you provide any other constructor
(public,
> private, with parameters etc.) then it won't automatically put the

default
> one in - you can still specify it yourself if you want.
>
> It's yet another thing that VB tries to hide from the developer. As

a > convention I always manually put a blank constructor in the class if

it > supports it - even if it is only a call to MyBase.New() - just for
clarity.
>
> HTH,
>
> Trev.
>
>
> "Andrew J. Marshall" <An*************@ObjectVision.net> wrote in message > news:e0**************@TK2MSFTNGP11.phx.gbl...
> > I want to create a class that must receive a parameter when
instantiated.
> > In other words, I do not want it to have a "Public Sub New()".
> >
> > 1) Does VB.NET create a default public constructor if I do not declare > one?
> >
> > 2) I've read that I can have a "Private Sub New()" which should take care
> of
> > my needs. Comments?
> >
> > Thanks,
> > Andrew
> >
> >
>
>



Nov 20 '05 #9
> I don't expect that this will behaviour never be changed,

lol. Sorry. I'm suffering from BFS today (Brain Fried Syndrome). It should
read:

"I don't expect that this behaviour will ever be changed"

Trev.
Nov 20 '05 #10
Hi Jay,

I understand Trev's point. The very lack of anything being there can lead
to confusion.

I'll give you an example (as I understand it)... when Java first came out if
a class could throw an exception you "had to" create an error handler for
that error regardless of whether you wanted to trap the error or not. You
could pass it along but the concept was "at least you knew you had an error
and you consciously decided to pass it up the chain."

This proved to be too much of a burden (so people worked around it) and I
believe the compiler no longer enforces it as a rule. The concept was
"sound" but in practice it didn't work.

It can't be considered very much work to add a public constructor... at
least one could see it there and not wonder if there was one further down
the class somewhere. As with so many readability issues they mostly don't
affect the person who originally writes the code but for those that come
along afterward.

As Trev points out, it won't change so it isn't a big deal. But look at how
much nicer things are now that you can see the actual code that the form
generates for initializing the form. VB was horrible for hiding this for so
many years, Delphi was great for exposing it right from the start.

Tom
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:OZ**************@TK2MSFTNGP09.phx.gbl...
Trev,
I'm sorry that does not make sense! ;-)

If I'm reading you correctly, you are suggesting that you want to have to
add a public default constructor if you want a publicly created class, which I suspect is what the majority of developers will want. As opposed adding a private default constructor in the minority of the cases?

I'm excluding the cases where you have non-default constructors
(parameterized constructors) as you have to add them any way.

Hope this helps
Jay

"Trev Hunter" <hu*********@hotmail.com> wrote in message
news:e5**************@TK2MSFTNGP11.phx.gbl...
Cheers Jay. I stand corrected.

Doesn't mean I like having to define a private or friend constructor just
to
override the default behaviour of a class being publicly creatable. I'd much
rather the default behaviour was to not make the class createble unless

you
explicitly defined a constructor (which is something the IDE could add in as
part of code completion maybe).

Just my 2c in my little explicit world ;)

Trev.
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in

message news:%2****************@TK2MSFTNGP12.phx.gbl...
Trev,
> It's yet another thing that VB tries to hide from the developer.
I don't think the "Default constructor" being added is a trait of VB.NET
per
se, as Java, C#, and C++ do the same thing.

Hope this helps
Jay

"Trev Hunter" <hu*********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
> AFAIK The default blank constructor is only provided as long as you

don't
> provide any other constructor. If you provide any other constructor
(public,
> private, with parameters etc.) then it won't automatically put the

default
> one in - you can still specify it yourself if you want.
>
> It's yet another thing that VB tries to hide from the developer. As

a > convention I always manually put a blank constructor in the class if

it > supports it - even if it is only a call to MyBase.New() - just for
clarity.
>
> HTH,
>
> Trev.
>
>
> "Andrew J. Marshall" <An*************@ObjectVision.net> wrote in message > news:e0**************@TK2MSFTNGP11.phx.gbl...
> > I want to create a class that must receive a parameter when
instantiated.
> > In other words, I do not want it to have a "Public Sub New()".
> >
> > 1) Does VB.NET create a default public constructor if I do not declare > one?
> >
> > 2) I've read that I can have a "Private Sub New()" which should take care
> of
> > my needs. Comments?
> >
> > Thanks,
> > Andrew
> >
> >
>
>



Nov 20 '05 #11
Hey Tom,
"Tom Leylan" <ge*@iamtiredofspam.com> wrote in message
news:ei**************@TK2MSFTNGP10.phx.gbl...
Hi Jay,

I understand Trev's point. The very lack of anything being there can lead
to confusion.

I'll give you an example (as I understand it)... when Java first came out if a class could throw an exception you "had to" create an error handler for
that error regardless of whether you wanted to trap the error or not. You
could pass it along but the concept was "at least you knew you had an error and you consciously decided to pass it up the chain."

Unless this was corrected in the last year it still does it (the last
version of Java I worked with was 1.4.1). And there were two ways to deal
with it.

a) When you declared a method you could add a throws clause.
i.e.

void myFunction() extends mybaseclass throws IOException {}

b) you could use try catch

void myFunction() extends mybaseclass
{
try {
...
} catch (IOException e) {}
}

the primary difference is that the latter catches it and does what it wants.
The first though is great for inherited classes because the inherited class
knows from the start all the exceptions that can be thrown during the life
of the method execution.

Some see it as a pain, I liked it because I never had runtime errors, and if
I did, it was because I wanted to, not because I didn't know.

Relevance... 0... just wanted to talk. =)
This proved to be too much of a burden (so people worked around it) and I
believe the compiler no longer enforces it as a rule. The concept was
"sound" but in practice it didn't work.

It can't be considered very much work to add a public constructor... at
least one could see it there and not wonder if there was one further down
the class somewhere. As with so many readability issues they mostly don't
affect the person who originally writes the code but for those that come
along afterward.

As Trev points out, it won't change so it isn't a big deal. But look at how much nicer things are now that you can see the actual code that the form
generates for initializing the form. VB was horrible for hiding this for so many years, Delphi was great for exposing it right from the start.

Tom
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:OZ**************@TK2MSFTNGP09.phx.gbl...
Trev,
I'm sorry that does not make sense! ;-)

If I'm reading you correctly, you are suggesting that you want to have to
add a public default constructor if you want a publicly created class, which
I suspect is what the majority of developers will want. As opposed adding a
private default constructor in the minority of the cases?

I'm excluding the cases where you have non-default constructors
(parameterized constructors) as you have to add them any way.

Hope this helps
Jay

"Trev Hunter" <hu*********@hotmail.com> wrote in message
news:e5**************@TK2MSFTNGP11.phx.gbl...
Cheers Jay. I stand corrected.

Doesn't mean I like having to define a private or friend constructor
just
to
override the default behaviour of a class being publicly creatable. I'd much
rather the default behaviour was to not make the class createble
unless you
explicitly defined a constructor (which is something the IDE could add in
as
part of code completion maybe).

Just my 2c in my little explicit world ;)

Trev.
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in

message news:%2****************@TK2MSFTNGP12.phx.gbl...
> Trev,
> > It's yet another thing that VB tries to hide from the developer.
> I don't think the "Default constructor" being added is a trait of VB.NET per
> se, as Java, C#, and C++ do the same thing.
>
> Hope this helps
> Jay
>
> "Trev Hunter" <hu*********@hotmail.com> wrote in message
> news:%2****************@TK2MSFTNGP10.phx.gbl...
> > AFAIK The default blank constructor is only provided as long as
you don't
> > provide any other constructor. If you provide any other constructor > (public,
> > private, with parameters etc.) then it won't automatically put the
default
> > one in - you can still specify it yourself if you want.
> >
> > It's yet another thing that VB tries to hide from the developer.

As a > > convention I always manually put a blank constructor in the class
if it
> > supports it - even if it is only a call to MyBase.New() - just for
> clarity.
> >
> > HTH,
> >
> > Trev.
> >
> >
> > "Andrew J. Marshall" <An*************@ObjectVision.net> wrote in

message
> > news:e0**************@TK2MSFTNGP11.phx.gbl...
> > > I want to create a class that must receive a parameter when
> instantiated.
> > > In other words, I do not want it to have a "Public Sub New()".
> > >
> > > 1) Does VB.NET create a default public constructor if I do not

declare
> > one?
> > >
> > > 2) I've read that I can have a "Private Sub New()" which should

take > care
> > of
> > > my needs. Comments?
> > >
> > > Thanks,
> > > Andrew
> > >
> > >
> >
> >
>
>



Nov 20 '05 #12
> The very lack of anything being there can lead
to confusion.


I guess "Option REALLY Explicit" is what we need ;) In my little world, this
would mean (among other things)

1) Option Strict On
2) No default constructors
3) No class, method or field definitions without a scope modifier
4) No optional parameters (use overloading instead)

Just my 2c

Trev.
Nov 20 '05 #13
I'm gonna disagree on the optional parameters part. =) I always liked that
feature. =) Sure, its just as easy to copy and paste your method signiture
and add a paramter, and then call the one of the other overloaded methods...

but look how much time that took... just to type the idea... when I can
just do

public mySub (param1 as integer, optional param2 as integer =0)

thats just super. =)

-CJ
"Trev Hunter" <hu*********@hotmail.com> wrote in message
news:Ot**************@TK2MSFTNGP11.phx.gbl...
The very lack of anything being there can lead
to confusion.
I guess "Option REALLY Explicit" is what we need ;) In my little world,

this would mean (among other things)

1) Option Strict On
2) No default constructors
3) No class, method or field definitions without a scope modifier
4) No optional parameters (use overloading instead)

Just my 2c

Trev.

Nov 20 '05 #14
Thanks for the clarification. I recently read Bruce Eckel's book on Java
and there was a discussion. He is proposing that checked exceptions are
probably not necessary, in principal because people end up trying to work
around it instead.

I agree that in principal having to deal with it (regardless of whether
you've done it well) means you cannot be clueless about the possibility of
the exception happening.

Tom
"CJ Taylor" <no****@blowgoats.com> wrote in message
news:10*************@corp.supernews.com...
Hey Tom,
"Tom Leylan" <ge*@iamtiredofspam.com> wrote in message
news:ei**************@TK2MSFTNGP10.phx.gbl...
Hi Jay,

I understand Trev's point. The very lack of anything being there can lead
to confusion.

I'll give you an example (as I understand it)... when Java first came out
if
a class could throw an exception you "had to" create an error handler
for that error regardless of whether you wanted to trap the error or not. You could pass it along but the concept was "at least you knew you had an

error
and you consciously decided to pass it up the chain."


Unless this was corrected in the last year it still does it (the last
version of Java I worked with was 1.4.1). And there were two ways to deal
with it.

a) When you declared a method you could add a throws clause.
i.e.

void myFunction() extends mybaseclass throws IOException {}

b) you could use try catch

void myFunction() extends mybaseclass
{
try {
...
} catch (IOException e) {}
}

the primary difference is that the latter catches it and does what it

wants. The first though is great for inherited classes because the inherited class knows from the start all the exceptions that can be thrown during the life
of the method execution.

Some see it as a pain, I liked it because I never had runtime errors, and if I did, it was because I wanted to, not because I didn't know.

Relevance... 0... just wanted to talk. =)
This proved to be too much of a burden (so people worked around it) and I believe the compiler no longer enforces it as a rule. The concept was
"sound" but in practice it didn't work.

It can't be considered very much work to add a public constructor... at
least one could see it there and not wonder if there was one further down the class somewhere. As with so many readability issues they mostly don't affect the person who originally writes the code but for those that come
along afterward.

As Trev points out, it won't change so it isn't a big deal. But look at how
much nicer things are now that you can see the actual code that the form
generates for initializing the form. VB was horrible for hiding this for so
many years, Delphi was great for exposing it right from the start.

Tom
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in
message news:OZ**************@TK2MSFTNGP09.phx.gbl...
Trev,
I'm sorry that does not make sense! ;-)

If I'm reading you correctly, you are suggesting that you want to have to add a public default constructor if you want a publicly created class,

which
I suspect is what the majority of developers will want. As opposed adding
a
private default constructor in the minority of the cases?

I'm excluding the cases where you have non-default constructors
(parameterized constructors) as you have to add them any way.

Hope this helps
Jay

"Trev Hunter" <hu*********@hotmail.com> wrote in message
news:e5**************@TK2MSFTNGP11.phx.gbl...
> Cheers Jay. I stand corrected.
>
> Doesn't mean I like having to define a private or friend constructor

just
to
> override the default behaviour of a class being publicly creatable.

I'd much
> rather the default behaviour was to not make the class createble unless you
> explicitly defined a constructor (which is something the IDE could add in
as
> part of code completion maybe).
>
> Just my 2c in my little explicit world ;)
>
> Trev.
>
>
> "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in

message
> news:%2****************@TK2MSFTNGP12.phx.gbl...
> > Trev,
> > > It's yet another thing that VB tries to hide from the developer.
> > I don't think the "Default constructor" being added is a trait of

VB.NET
> per
> > se, as Java, C#, and C++ do the same thing.
> >
> > Hope this helps
> > Jay
> >
> > "Trev Hunter" <hu*********@hotmail.com> wrote in message
> > news:%2****************@TK2MSFTNGP10.phx.gbl...
> > > AFAIK The default blank constructor is only provided as long as you > don't
> > > provide any other constructor. If you provide any other constructor > > (public,
> > > private, with parameters etc.) then it won't automatically put
the > default
> > > one in - you can still specify it yourself if you want.
> > >
> > > It's yet another thing that VB tries to hide from the developer. As
a
> > > convention I always manually put a blank constructor in the class if it
> > > supports it - even if it is only a call to MyBase.New() - just

for > > clarity.
> > >
> > > HTH,
> > >
> > > Trev.
> > >
> > >
> > > "Andrew J. Marshall" <An*************@ObjectVision.net> wrote in
message
> > > news:e0**************@TK2MSFTNGP11.phx.gbl...
> > > > I want to create a class that must receive a parameter when
> > instantiated.
> > > > In other words, I do not want it to have a "Public Sub New()".
> > > >
> > > > 1) Does VB.NET create a default public constructor if I do not
declare
> > > one?
> > > >
> > > > 2) I've read that I can have a "Private Sub New()" which

should take
> > care
> > > of
> > > > my needs. Comments?
> > > >
> > > > Thanks,
> > > > Andrew
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 20 '05 #15
> public mySub (param1 as integer, optional param2 as integer =0)

thats just super. =)
Yes, but it's not CLS compliant ;( I gave up on optional parametes because
more often than not I couldn't find a reliable way of detecting whether a
parameter was passed or not. I wasn't over the moon with MSDN's
recommendation to "set an unlikley value as the default" either.

Trev.
"CJ Taylor" <no****@blowgoats.com> wrote in message
news:10*************@corp.supernews.com... I'm gonna disagree on the optional parameters part. =) I always liked that feature. =) Sure, its just as easy to copy and paste your method signiture and add a paramter, and then call the one of the other overloaded methods...
but look how much time that took... just to type the idea... when I can
just do

public mySub (param1 as integer, optional param2 as integer =0)

thats just super. =)

-CJ
"Trev Hunter" <hu*********@hotmail.com> wrote in message
news:Ot**************@TK2MSFTNGP11.phx.gbl...
The very lack of anything being there can lead
to confusion.


I guess "Option REALLY Explicit" is what we need ;) In my little world,

this
would mean (among other things)

1) Option Strict On
2) No default constructors
3) No class, method or field definitions without a scope modifier
4) No optional parameters (use overloading instead)

Just my 2c

Trev.


Nov 20 '05 #16
Trev,
Oh! I follow you.

I can see some merit in for the IDE to do it to help maintain a "shop
standard", as long as it was configurable.

You could possible create a macro that automatically added the default
constructor for you, that either looked for when you added a new class or
that you just press a button to add the default constructor?

Hope this helps
Jay

"Trev Hunter" <hu*********@hotmail.com> wrote in message
news:et**************@TK2MSFTNGP12.phx.gbl...
I suspect is what the majority of developers will want.
As opposed adding a private default constructor in the
minority of the cases?
I'm not arguing that a blank public constructor shouldn't be added by
default, I'm arguing that it shouldn't be hidden. It could be added
automatically by the code editor - the same way the IDE sticks in "End If"
constructs. I realize not everyone uses an IDE, and I know this goes

against the grain of most developers creating a class and expecting it to be
creatable by default, but I tend to write code so as to make it as explicit as possible and this is one of those things that I like to make explicit.
I'd much rather delete the default Public Constructor than implement a
private constructor (that overrides some hidden constructor) in order to
make a class non-creatable.

I don't expect that this will behaviour never be changed, but one of the
reasons I tend to make things explicit is that I've been caught out too many times in the past by double standards in VB - for example if you use "Dim"
to declare a field at class level leaving out a scope marker - its scope is private. However if you declare a class without a scope marker, its scope is friend.

Sorry for rambling on a crappy technicality. I'm just in a talkitive mood
today ;)

Trev.
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:OZ**************@TK2MSFTNGP09.phx.gbl...
Trev,
I'm sorry that does not make sense! ;-)

If I'm reading you correctly, you are suggesting that you want to have to
add a public default constructor if you want a publicly created class, which
I suspect is what the majority of developers will want. As opposed adding a
private default constructor in the minority of the cases?

I'm excluding the cases where you have non-default constructors
(parameterized constructors) as you have to add them any way.

Hope this helps
Jay

"Trev Hunter" <hu*********@hotmail.com> wrote in message
news:e5**************@TK2MSFTNGP11.phx.gbl...
Cheers Jay. I stand corrected.

Doesn't mean I like having to define a private or friend constructor
just
to
override the default behaviour of a class being publicly creatable. I'd much
rather the default behaviour was to not make the class createble
unless you
explicitly defined a constructor (which is something the IDE could add in
as
part of code completion maybe).

Just my 2c in my little explicit world ;)

Trev.
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in

message news:%2****************@TK2MSFTNGP12.phx.gbl...
> Trev,
> > It's yet another thing that VB tries to hide from the developer.
> I don't think the "Default constructor" being added is a trait of VB.NET per
> se, as Java, C#, and C++ do the same thing.
>
> Hope this helps
> Jay
>
> "Trev Hunter" <hu*********@hotmail.com> wrote in message
> news:%2****************@TK2MSFTNGP10.phx.gbl...
> > AFAIK The default blank constructor is only provided as long as
you don't
> > provide any other constructor. If you provide any other constructor > (public,
> > private, with parameters etc.) then it won't automatically put the
default
> > one in - you can still specify it yourself if you want.
> >
> > It's yet another thing that VB tries to hide from the developer.

As a > > convention I always manually put a blank constructor in the class
if it
> > supports it - even if it is only a call to MyBase.New() - just for
> clarity.
> >
> > HTH,
> >
> > Trev.
> >
> >
> > "Andrew J. Marshall" <An*************@ObjectVision.net> wrote in

message
> > news:e0**************@TK2MSFTNGP11.phx.gbl...
> > > I want to create a class that must receive a parameter when
> instantiated.
> > > In other words, I do not want it to have a "Public Sub New()".
> > >
> > > 1) Does VB.NET create a default public constructor if I do not

declare
> > one?
> > >
> > > 2) I've read that I can have a "Private Sub New()" which should

take > care
> > of
> > > my needs. Comments?
> > >
> > > Thanks,
> > > Andrew
> > >
> > >
> >
> >
>
>



Nov 20 '05 #17
Trev,
"Option REALLY Explicit" is called a Code Critic or Code Analyzer.

In C++ it was called lint.

Its a utility that looks at your code and gives you feedback on what you
should change to improve it.

Such as
http://www.fmsinc.com/reviews/tnanalyzer/sd1202.htm

Hope this helps
Jay

"Trev Hunter" <hu*********@hotmail.com> wrote in message
news:Ot**************@TK2MSFTNGP11.phx.gbl...
The very lack of anything being there can lead
to confusion.
I guess "Option REALLY Explicit" is what we need ;) In my little world,

this would mean (among other things)

1) Option Strict On
2) No default constructors
3) No class, method or field definitions without a scope modifier
4) No optional parameters (use overloading instead)

Just my 2c

Trev.

Nov 20 '05 #18
Cheers Jay, I'll have a look.

Is there one for "Option Bugs Off" ;) - that one really bothers me!

Trev.

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:e9*************@tk2msftngp13.phx.gbl...
Trev,
"Option REALLY Explicit" is called a Code Critic or Code Analyzer.

In C++ it was called lint.

Its a utility that looks at your code and gives you feedback on what you
should change to improve it.

Such as
http://www.fmsinc.com/reviews/tnanalyzer/sd1202.htm

Hope this helps
Jay

Nov 20 '05 #19
> You could possible create a macro that
automatically added the default
constructor for you, that either looked
for when you added a new class or
that you just press a button to add the default constructor?


Yeah, I suppose that would do the job. ATM, I'm just using the handy feature
of the toolbar that lets you drag & drop pieces of text onto it. I've added
code snippets for most common things that I do (class templates, method and
property templates etc). I know there's plugins that allow you to do this,
but haven't gotten around to investigating them.

Cheers,

Trev.
Nov 20 '05 #20

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

Similar topics

15
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including...
6
by: Gunnar G | last post by:
If I don't define a default constructor for my class "Foo", I still get one from the compiler. But if I define a constructor that takes an argument, I don't get the default constructor, why? I...
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
23
by: Jess | last post by:
Hello, I understand the default-initialization happens if we don't initialize an object explicitly. I think for an object of a class type, the value is determined by the constructor, and for...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
43
by: JohnQ | last post by:
Are a default constructor, destructor, copy constructor and assignment operator generated by the compiler for a struct if they are not explicitely defined? I think the answer is yes, because...
4
by: Macneed | last post by:
i am a newbie, i remember i read a book talking about when u declare a array variable using float ABC = new float; the whole array element in ABC ( ABC to ABC ) will automatic initialize to 0...
10
by: Jason Doucette | last post by:
Situation: I have a simple struct that, say, holds a color (R, G, and B). I created my own constructors to ease its creation. As a result, I lose the default constructor. I dislike this, but...
5
by: * Tong * | last post by:
Hi, I couldn't figure out the answer myself about the default constructor -- does C++ create it by default? If yes, under what circumstances? Actually, I've found out the partial answer myself...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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
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,...

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.