Too many arguments to Public Sub New()

methc

New member
Joined
Nov 8, 2017
Messages
1
Programming Experience
Beginner
[FONT=&quot]Hi guys! How are you all doing?[/FONT]

[FONT=&quot]I'm new to this forum, just started working in VB .Net and I'm now starting my first project. [/FONT]

[FONT=&quot]I ran into a little snag today, spent quiet a bit of time trying to fix it but you can't fix something if you don't understand what's going on, right? [/FONT]

[FONT=&quot]So two things - can someone explain why I'm getting the error In the attached screen shot and how do I fix it?[/FONT]

[FONT=&quot]Additional info: [/FONT]
[FONT=&quot]Intellisense is suggesting that I generate a constructor sub in frm_reportviewer1 but when I do I get some more errors saying "reference to a non-shared member requires an object reference" (I get this when I say frm_reportviewer.show() [/FONT] toomanyarguments.jpg

[FONT=&quot]Any assistance is appreciated. [/FONT]

[FONT=&quot]Thanks![/FONT]
 
Firstly, please don't post photos of your screen. A screenshot would be better but don't do that either when what you want to display is code. Copy the code from your IDE and paste it into your post inside appropriate formatting tags. This:

[xcode=vb]your code here[/xcode]

or you can do something like this:

[code]your code here [B][U][COLOR="#FF0000"]with extra emphasis[/COLOR][/U][/B][/code]

to get something like this:

VB.NET:
your code here [B][U][COLOR="#FF0000"]with extra emphasis[/COLOR][/U][/B]

Properly formatted code is easy to read and, importantly, easy to copy and paste in order to test if required.

AS for your issue, the problem is exactly as the error message says. You're invoking a constructor, i.e. a New method, and passing one argument but there is no constructor that has one parameter. When a method is written, part of the signature of that method is the number and type of parameters. In the method body, those parameters will be used. The code of the method can't magically change when you call it so it requires that you pass an argument to each parameter and it also requires that you don't pass arguments for any parameters that don't exist. You're passing a report as an argument to that constructor but there's no code in it that knows what to do with it. What exactly are you expecting to happen to that report inside the form and how are you expecting that to happen when you haven't written any code to do it?

When you add a form to your project, it will have a parameterless constructor by default, i.e. a method like this:
Public Sub New()
    '...
End Sub

That means that the only way that you can create an instance of that type is by invoking the constructor and passing no arguments. If you want to be able to pass an argument to a constructor then you have to write a constructor with a parameter to pass an argument to. You have to add code to that constructor so that it knows what to do with the value of that parameter.

Because of the way the designer code works, it's possible to mess up your own constructor if you're not careful. There's an auto-generated method named InitializeComponent that contains all the code to create and configure your controls that must be called. The first thing to decide is whether you want just your own constructor with parameters or you want that in addition to the parameterless constructor. I'm guessing that you want the former in this case, i.e. you want to require that a report is always passed in when creating a form instance of this type. In that case, you should do as follows:

1. Add a parameterless constructor to your form code. Just type 'Public Sub New' and hit Return and the IDE should complete the code for you, including adding a call to InitializeComponent.
2. Add the appropriate parameter(s) to your constructor, e.g. 'Public Sub New(report As SomeAppropriateType)'.
3. Add code to your constructor, where indicated by the auto-generated comments, to make use of the parameter(s) as you require.

By the way, did you notice that there's a 'Similar Threads' section at the bottom of your thread? It's quite possible that your question has already been answered in one of those.
 
Back
Top