2

Hello I have a code that use with VB.net:

Public Shared Function GetNextID(ByVal Prefix As String) As String
    Dim dt As DataTable = Database.GetDataTable("select * from _NumberGeneration Where Entity = '" & Prefix & "'")
    If dt Is Nothing OrElse dt.Rows.Count = 0 Then Return ""
    Dim format As String = dt.Rows(0)("Format") & ""
    Dim sqlCnt As String = dt.Rows(0)("SQL") & ""
    Dim cnt As Int32 = Val(Database.ExecuteScalar(sqlCnt)) + 1  ' 11
    Dim RN As String = format.Substring(format.IndexOf("["c), 4) ' [R5]
    Dim newRN As String = StrDup(CInt(RN.Substring(2,1), "0"c) ' 00000
    newRN = cnt.ToString(newRN) ' String.Format(newRN, cnt) ' 00011
    Dim newformat As String = format.Replace(RN, newRN)  '"PR"yyMM00011
    Return Today.ToString(newformat) ' String.Format(newformat, cnt)
End Function

For the StrDup When I write the code with C# same as code below:

string newRN = new string (int.Parse(RN.Substring(2, 1)),'0');

It show the error.

Ley
  • 41
  • 2
  • 7

1 Answers1

5

Use the string constructor with character and count parameters:

string newRN = new string('0', 4);

You could also reference the Microsoft.VisualBasic assembly and call the method via the Strings module, but the alternative above is pretty simple.

Dave Doknjas
  • 6,394
  • 1
  • 15
  • 28