Create an object

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am new to VB.NET. Anyone could give a hint? Thanks.
What is the problem about the following code:

Dim myPrincipal As CustomPrincipal = New CustomPrincipal(id, roles)

where CustomPrincipal is constructor defined as
Public Sub CustomPrincipal(ByVal identity As IIdentity, ByVal roles As
String())

the error message:
Too many arguments to 'Public Sub New()'.
 
A Sub is not a class. The New Operator is for creating instances of classes.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.
 
Hi
I am a new ASP.NET programmer

In VB.NET the constructor name should be New and not the class name (C#
constructor name is the class name)

If there is no constructor declared with New keyword then the runtime will
create a default parameterless constructor for you.

So the solution may be:

One: change your constructor name to New instead of CustomPrincipal

Dim myPrincipal As CustomPrincipal = New CustomPrincipal(id, roles)
where CustomPrincipal is constructor defined as
Public Sub New(ByVal identity As IIdentity, ByVal roles As String())


or

Two create the object without any arguments passed to it


Dim myPrincipal As CustomPrincipal = New CustomPrincipal()
where CustomPrincipal is constructor defined as
Public Sub CustomPrincipal(ByVal identity As IIdentity, ByVal roles As
String())
 
Hello David,

I think you need to define the constructor as

Public Sub New(ByVal identity As IIdentity, ByVal roles As String())
 
As kevin says, you need to create a constructor...

Publc Sub New(ByVal identity As IIdentity, ByVal roles As String())

perhaps you are used to C# where the constructor is the name of the
class...in VB.Net, you use the New keyword as the constructor name...

karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
 
Using the name of the class for the constructor is a (C#) language
convention. In VB.NET contrcutors are using always the name "New"...

Patrice
 
Try

Dim myPrincipal as New CustomPrincipal(id, roles)

I assume your parameters are of the correct type

Hope that helps
 
Back
Top