Dynamic Prameters not supported?


Alexander Wurzinger wrote (via the Visual Studio Gallery page)
Dynamic Prameters not supported?

it seems Dynamic Parameters are not supported/threaded like a single parameter.
ex, something like this:
Dim l as list(of integer)
String.Format("{0}{1}", l.ToString()) 
Complaines about invalid Format string.

file attachments

Closed Sep 18, 2014 at 10:01 PM by AdamSpeight2008
Correct behaviour according to MSDN Documentation


AdamSpeight2008 wrote Sep 18, 2014 at 9:51 PM

Alexander Wurzinger, thank you for your report.

Sadly to say that the diagnostic is correct in its analysis.

Module Module1

  Sub Main()
    Dim l As new List(Of Integer)
  Dim r=  String.Format("{0}{1}", l.ToString())
  End Sub

End Module
If you were to run that that code it would throw the following runtime exception. MSDN FormatException
An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

Additional information: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

I think you maybe slightly confuse by what you call "Dynamic Parameters" (no offence)

You assign a list of integers to the variable l
Dim l As New List(Of Integer)
Then add (or insert) the new items into the list ( values 0 , 1 )
l.Add( 0 )
l.Add( 1 )
Then call the String.Format function MSDN String.Format (String, Object[])
With the following arguments for the parameters.
(0) = "{0}{1}"
(1) = l.ToString()
String.Format treats the first parameter as the format string to use, and rest as arguments for that format string. Note: That the indexes used are not 1, 2, 3 .. but 0, 1, 2 ...
(0) = l.ToString()
It parses the format string and recognises an arg hole {0} and checks to see if that arg index is valid.
It determines that it is. Rationale 0 <= 0 < 1
It continues to parse the format string and recognises another arg hole {`} and checks to see if that arg index is valid. It determines that it is not valid. Rationale 0 <= 1 < 1

String.Format Diagnostic recognised that issue at compile time.

Invalid FormatString (Reason: Index of (1) is invalid. (0 <= Index < 1))

If we remove the problematic arg hole from the format string. "{0}"
The contains of the string r would be System.Collections.Generic.List`1[System.Int32]
Note: String.Format Diagnostic guesses that the contains of r is "l"
This is because it can only get value of variable it knows at compile time. Which means Constants and Literals.