Call another overload function of the same name misses a serious problem that appears in parentheses

xiaoxiao2021-03-06  15

My ASP.NET can't debug, because it is just to change a page, I don't debug it. Check the code carefully.

However, the problem that I encountered today did not check it out, and then copied the code to the notebook, and then I found a problem!

Public Function GetInsertsql () AS STRING

DIM STRSQL AS STRING

STRSQL = String.Format ("INSERT INTO {0}) VALUES ({1}, {2}, {3});", _TablesData, _ReportID, _ItemID, _Value)

Return strsql

END FUNCTION

Public Function GetInsertsql (Byval DDL As DropdownList) AS String

If Ddl.Items.count = 0 THEN

Return getInsertsql 'Here is a bracket, it should be return getInsertsql ()

'Because the function name in VB.NET represented the object to be returned, the compilation does not have an error.

'If you use getInsertsql directly, it is a Nothing.

'If getInsertsql () indicates that the overload function getInssertsql () is called, so it is correct

'Can understand this: System implicitly declared a variable, the variable name is a function name, the type is the return type of the function

END IF

DIM strsql as string = "" "

DIM V As Double

DIM SEL As String = DDL.SelectedValue

For Each Item As ListItem in Ddl.Items

V = IIF (item.value = SEL, _VALUE, 0)

STRSQL & = String.Format ("INSERT INTO {0} (INSERT INTO {0}) VALUES ({1}, {2}, {3}, '{4}');", _TablesData, _ReportID, _ItemID, V, Item.Text.trim ())

NEXT

Return strsql

END FUNCTION

It's a very hidden mistake. I have to pay attention to it, parenthey is good (I used to automatically add parentheses).

For such a problem, the compiler should make a warning or when the cancel function name represents the return value.

Although MSDN praises this:

The advantage of assigning the return value to the function name is that the function returns to the function until the program encounters the exit function or End Function statement. This allows you to allocate a preliminary value, and then adjust it if necessary.

转载请注明原文地址:https://www.9cbs.com/read-47411.html

New Post(0)