Constructors?

  • Thread starter Thread starter Arpan
  • Start date Start date
A

Arpan

The ASP.NET book that I am referring to learn ASP.NET says the
following:

----------------
If you don't provide a constructor that doesn't take any parameters,
you cannot do the following:

Dim mObject As New MyObject
----------------

Now isn't the above line (not the code line) wrong since ASP.NET
implicitly creates a no-argument constructor with the name "New" if a
class doesn't have a constructor? Hence the above code line is very
much valid, isn't it?

Please correct me if I am wrong.

Thanks,

Arpan
 
Hi,
The ASP.NET book that I am referring to learn ASP.NET says the
following:

----------------
If you don't provide a constructor that doesn't take any parameters,
you cannot do the following:

Dim mObject As New MyObject
----------------

Now isn't the above line (not the code line) wrong since ASP.NET
implicitly creates a no-argument constructor with the name "New" if a
class doesn't have a constructor? Hence the above code line is very
much valid, isn't it?

Please correct me if I am wrong.

Thanks,

Arpan

(Please allow me to write the example in C#, as I am not fluent in
VB.NET and wouldn't want to write wrong code)

That's only true if you don't provide *any* constructor. If you provide
a constructor with (for example)

public MyObject( string param1 )
{
// ...
}

then no empty constructor is created. Sometimes, as a developer, you
just don't want to allow empty constructors for your class.

HTH,
Laurent
 
Back
Top