简体   繁体   中英

User Control VB.Net script Conversion to C#

<script language="VB" runat="server">
    Public Data As String = "" 
    Public Height As Byte = 25
    Public WidthMultiplier As Byte = 1

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim dictEncoding As StringDictionary
        Dim sbBarcodeImgs As StringBuilder

        Dim strEncodedData As String
        Dim I As Integer

        dictEncoding = New StringDictionary()
        dictEncoding.Add("0", "101001101101")
        dictEncoding.Add("1", "110100101011")
        dictEncoding.Add("2", "101100101011")
        dictEncoding.Add("3", "110110010101")
        dictEncoding.Add("4", "101001101011")
        dictEncoding.Add("5", "110100110101")
        dictEncoding.Add("6", "101100110101")
        dictEncoding.Add("7", "101001011011")
        dictEncoding.Add("8", "110100101101")
        dictEncoding.Add("9", "101100101101")
        dictEncoding.Add("A", "110101001011")
        dictEncoding.Add("B", "101101001011")
        dictEncoding.Add("C", "110110100101")
        dictEncoding.Add("D", "101011001011")
        dictEncoding.Add("E", "110101100101")
        dictEncoding.Add("F", "101101100101")
        dictEncoding.Add("G", "101010011011")
        dictEncoding.Add("H", "110101001101")
        dictEncoding.Add("I", "101101001101")
        dictEncoding.Add("J", "101011001101")
        dictEncoding.Add("K", "110101010011")
        dictEncoding.Add("L", "101101010011")
        dictEncoding.Add("M", "110110101001")
        dictEncoding.Add("N", "101011010011")
        dictEncoding.Add("O", "110101101001")
        dictEncoding.Add("P", "101101101001")
        dictEncoding.Add("Q", "101010110011")
        dictEncoding.Add("R", "110101011001")
        dictEncoding.Add("S", "101101011001")
        dictEncoding.Add("T", "101011011001")
        dictEncoding.Add("U", "110010101011")
        dictEncoding.Add("V", "100110101011")
        dictEncoding.Add("W", "110011010101")
        dictEncoding.Add("X", "100101101011")
        dictEncoding.Add("Y", "110010110101")
        dictEncoding.Add("Z", "100110110101")
        dictEncoding.Add("-", "100101011011")
        dictEncoding.Add(":", "110010101101")
        dictEncoding.Add(" ", "100110101101")
        dictEncoding.Add("$", "100100100101")
        dictEncoding.Add("/", "100100101001")
        dictEncoding.Add("+", "100101001001")
        dictEncoding.Add("%", "101001001001")
        dictEncoding.Add("*", "100101101101")


        strEncodedData = dictEncoding("*") & "0"
        For I = 1 To Len(Data)
            strEncodedData = strEncodedData & dictEncoding(Mid(Data, I, 1)) & "0"
        Next I
        strEncodedData = strEncodedData & dictEncoding("*")




        sbBarcodeImgs = New StringBuilder()
        For I = 1 To Len(strEncodedData)
            If Mid(strEncodedData, I, 1) = "1" Then
                sbBarcodeImgs.Append("<img src=""images/bar_blk.gif"" width=""" & WidthMultiplier & """ height=""" & Height & """ />")
            Else
                sbBarcodeImgs.Append("<img src=""images/bar_wht.gif"" width=""" & WidthMultiplier & """ height=""" & Height & """ />")
            End If
        Next I

        litBarcode.Text = sbBarcodeImgs.ToString
    End Sub
</script>
<asp:Literal ID="litBarcode" runat="server" />

Primarily the MID and dictionary usage are unfamiliar to me. Can this be completely converted to C#?

StringDictionary is just another collection class so no problem. Mid could still be used as Microsoft.VisualBasic.Mid() if you're willing to import the Visual Basic library to your C# app (nothing bad about that) or it could be rewritten fairly easily.

Edit: Actually, the VB.Net code just seems to use the Mid in the same way as you can use String.Substring so no need to use the Visual Basic library even. (I was thinking of Mid in VB6 that could be either a function or a statement, the function is similar to String.Substring but there's no real easy way to replicate it if it's the statement one but either way, doesn't matter for this code).

Have you tried using the Telerik Converter ? I ran your stuff through there and didn't get any errors.

Yes. Use one of the converter tools mentioned here; only run through the code though, and not the markup parts of it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM