• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
  • 實現.net框架帶有無限參數的WriteLine()函數

    發表于:2007-05-25來源:作者:點擊數: 標簽:框架帶有無限.NET參數
    .net Framework庫中的Console.WriteLine最多只支持3個參數,使用起來非常不方便,雖然C++支持一個無限參數的WriteLine,但是 VB 和C#和其他大量語言都不支持?,F在,我編寫了一個VB的函數,他利用VB、C#和JScript中參數數組的功能,實現了無限參數的WrtieLin

     

    .net Framework庫中的Console.WriteLine最多只支持3個參數,使用起來非常不方便,雖然C++支持一個無限參數的WriteLine,但是VB和C#和其他大量語言都不支持?,F在,我編寫了一個VB的函數,他利用VB、C#和JScript中參數數組的功能,實現了無限參數的WrtieLine。用VB編寫的原因是VB含有許多簡單的字符串函數,可以大大簡化程序。而編譯為組件后,C#是可以使用它的。

        Public Shared Sub WriteLine(ByVal format As String, ByVal ParamArray args() As Object)
            '********************************************************************
            '*作者:Ninputer
            '*日期:2001年8月27日
            '*
            '*參數:format是輸出字符串,包含輸出內容的位置
            '*      args()是輸出內容,可以有無限個參數,與format中相應的位置吻合
            '*返回值:無
            '*說明:提供無限參數的WriteLine方法,比System.Console更加方便
            '********************************************************************

            Dim sSubString, sSubStringRp As String
            Dim i, j As Integer

            If args.Length = 0 Then
                Console.WriteLine(format)
            Else
                '解決無限參數的輸出

                If format = "" Then '如果沒有字符串,則跳過循環搜索部分
                    Console.WriteLine("")
                    Exit Sub
                End If

                Try
                    i = 1 '當前字符指針的位置
                    Do
                        If format.Chars(i - 1) = "{" Then
                            j = InStr(i, format, "}") 'j是從i開始,第一個“}”的位置

                            '對兩個大括號中的內容進行截取
                            sSubString = format.Substring(i, j - i - 1) 'Like "3"
                            sSubStringRp = format.Substring(i - 1, j - i + 1) 'Like "{3}"

                            '如果格式正確,就把{n}替換成所需要的內容
                            If IsNumeric(sSubString) Then
                                If Int(Val(sSubString)) <> Val(sSubString) Then Throw New Exception() '跳轉到Catch區域
                                format = format.Substring(0, i - 1) & _
                                         Replace(format, sSubStringRp, args(CInt(Val(sSubString))).ToString, i, 1)
                                '重新規定搜索的起點,避免重新替換
                                '讓指針指向剛剛替換的子字符串的末尾
                                i += args(CInt(Val(sSubString))).ToString.Length - 1
                            Else
                                Throw New Exception() '跳轉到Catch區域
                            End If
                        End If
                        i += 1
                    Loop While i <= format.Length
                Catch e As Exception
                    '拋出異常:表示輸出格式字符串不正確
                    Throw New FormatException("輸出格式不正確。")
                End Try
                Console.WriteLine(format)
            End If
        End Sub

    原文轉自:http://www.kjueaiud.com

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>