Compiler doesn't appear to notice that I passed in a Parameter that isn't part of the Function definition

  • Thread starter Thread starter rozeboosje2
  • Start date Start date
R

rozeboosje2

Guest
I would just like to know what is happening here and why the compiler doesn't pick up on it, and the application even runs until it hits that final statement.

I tried the same with a Sub GetWordApplication2() and calling it ... GetWordApplication2(sProgress) and as expected I got a compiler error Too many arguments to 'Private Shared Sub GetWordApplication2' and the application won't compile. However, if I declare the Function as shown below, the compiler is perfectly happy, it does not pick up on the fact that I am calling it with a parameter sProgress that hasn't been declared in the Function declaration. It even RUNS - you can step through the code in GetWordApplication and get right up to, and including the "Return" statement. Only AFTER stepping through and returning to the statement oWord = GetWordApplication(sProgress) in the calling Main() method - only THEN is an error raised. In the case where I'm returning Nothing it raises a NullreferenceException, in the case of the GetObject call I'm getting the message: "An unhandled exception of type 'System.MissingMemberException' occurred in Microsoft.VisualBasic.dll
Additional information: No default member found for type 'ApplicationClass'."

As you can imagine I was getting the "missingmemberexception" error in a big project which led me down a rabbit hole and on a wild goose chase thinking something was amiss with Word, until I took a step back and realised I was calling a function passing in a parameter it wasn't expecting at which point I slapped myself for being so stupid.

But why didn't the compiler spot this?



Friend Shared Sub Main()

Dim oWord As Object = Nothing

Dim sProgress As String = String.Empty

oWord = GetWordApplication(sProgress)

End Sub

Private Shared Function GetWordApplication() As Object

Return Nothing

'OR COMMENT OUT THE ABOVE AND DO THE BELOW INSTEAD
'Return Microsoft.VisualBasic.Interaction.GetObject("", "Word.Application")

End Function


Pino Carafa

Continue reading...
 
Back
Top