The ability to pass an argument to a function without parameters

I am currently using VB.NET and I ran into a problem. This is my class:

Public class foo

    Private _bar As Integer
    Private _name As String

    Public Sub New(bar As Integer)
        Me._bar = bar
        Me._name = getName(bar) '//Passing in an argument where it is not needed
    End Sub

    Private Function getName() As String

        '//Get name from database using _bar as a lookup(it essentially a primary key)
        '//Name is obtained successfully (checked when debugging)
        '//Return name

    End Function

End Class

I can run this code even though I passed the getName argument, where it has no parameters. However, when I run it, the field Me._namealways ends with an empty string (and not a null value, since it initially starts with), but I know that the method getNamereturns the correct string when I checked it during debugging. If I delete an unnecessary parameter, it will work as expected, and Me._name will get the return value.

Why can I pass an argument where it should not be and not detect errors in my error list? I tried this on my colleagues computer, and they got the error "Too many arguments."

+4
2

/sub VB.NET,

getName(bar)

getName()(bar)

.

, getName(bar) bar getName, (bar+1)th , getName().

, getName

Private Function getName() As String
    Return "test"
End Function

getName(1) , getName()(1), "test", "e".

+6

Chars - String.

Public NotInheritable Class [String]

    <__DynamicallyInvokable> _
    Public ReadOnly Default Property Chars(ByVal index As Integer) As Char
        <MethodImpl(MethodImplOptions.InternalCall), SecuritySafeCritical, __DynamicallyInvokable> _
        Get
    End Property

End Class

:

getName(bar) 

getName.Chars(bar)

, String , Expression is not an array or a method, and cannot have an argument list..

Public Class foo

    Private _bar As Integer
    Private _name As [String]

    Public Sub New(bar As Integer)
        Me._bar = bar
        Me._name = getName(bar) '//Passing in an argument where it is not needed
    End Sub

    Private Function getName() As [String]
        '//Get name from database using _bar as a lookup(it essentially a primary key)
        '//Name is obtained successfully (checked when debugging)
        '//Return name
    End Function

End Class

Public NotInheritable Class [String]

    Public ReadOnly Property Chars(index As Integer) As Char
        Get

        End Get
    End Property

End Class
+2

Source: https://habr.com/ru/post/1525780/


All Articles