Indexing function return value without parameters

Recently, I came across what, in my opinion, is a rather obscure feature of the language when writing vb.net code.

The function is that if the function you are calling does not receive any parameters, and you try to call it with the parameter (And wait for an error to appear, or at least get a compile-time error!) , It will be implicitly converted into an attempt to index the return value of the function .

Below is an example of code from a new project of the vb.net form created in visual studio.

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load

        Call GetCaption()


        Me.Text = GetCaption() ' The caption of the form is 'this is my new form'

        Me.Text = GetCaption(1) ' The caption of the form is 'h'

        Me.Text = GetString(2) 'The caption of the form is '2'
    End Sub

    Private Function GetCaption() As String
        Return "This is my new form"
    End Function

    Private Function GetString() As String()

        Dim x As String() = {"", "0", "2"}

        Return x

    End Function
End Class

, , " , . ". : https://msdn.microsoft.com/en-gb/library/1wey3ak2.aspx

, , , : https://msdn.microsoft.com/en-us/library/y1wwy0we(v=vs.140).aspx

, , , , .

?

+4
1

. , , . GetCaption() - , - , , N- . :

Me.Text = GetCaption(1)   ' second char, arrays are zero based
Me.Text = GetCaption.Chars(1)   
Me.Text = GetCaption.ElementsAt(1)

VB-ism parens; , :

Me.Text = GetCaption()(1) 
Me.Text = GetCaption().Chars(1)   
Me.Text = GetCaption().ElementsAt(1)

, : . , .

+3

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


All Articles