Results 1 to 6 of 6

Thread: Help converting C# class to VB.NET

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2005
    Location
    USA Washington
    Posts
    191

    Help converting C# class to VB.NET

    I'm wasn't sure if I should have put this in C# or the VB.NET--but anyway, I decided either way would work.

    I have this class--not my own code--but I want to make use of it. And since I don't have C# and also don't know it (would learn it if I had it) I need to convert it. Would anyone please help me convert it? I know some of it will convert without really even needing too much knowledge of C# but some of it I'm not sure what the equivalents are.


    Help would be greatly appreciated.


    Thanks
    (I'm sure this is all I need, I actually have the whole project but can open it- But if more information is needed I can provide it--just ask.)

    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Drawing;
    
    namespace UPCA
    {
      class cUPCA
      {
        private Bitmap newBitmap;
        private Graphics g;
        private int barCodeHeight = 80;
        private int placeMarker = 0;
        private int imageWidth = 0;
        private float imageScale = 1;
        private string UPCABegin = "0000000000000101";
        private string[] UPCALeft = { "0001101", "0011001", "0010011", "0111101", "0100011", "0110001", "0101111", "0111011", "0110111", "0001011" };
        private string UPCAMiddle = "01010";
        private string[] UPCARight = { "1110010", "1100110", "1101100", "1000010", "1011100", "1001110", "1010000", "1000100", "1001000", "1110100" };
        private string UPCAEnd = "1010000000000000";
    
        public Image CreateBarCode(string txt, int scale)
        {
          Image img = null;
          imageWidth = 120;
          imageScale = scale;
          imageWidth = System.Convert.ToInt32(imageWidth * imageScale);
          int imageHeightHolder = System.Convert.ToInt32(barCodeHeight * imageScale);
          string incomingString = txt.ToUpper();
          if(incomingString.Length == 0)
          {
            return img;
          }
          int numberOfChars = incomingString.Length;
          newBitmap = new Bitmap((imageWidth), imageHeightHolder, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
          g = Graphics.FromImage(newBitmap);
          g.ScaleTransform(imageScale, imageScale);
          Rectangle newRec = new Rectangle(0, 0, (imageWidth), imageHeightHolder);
          g.FillRectangle(new SolidBrush(Color.White), newRec);
          placeMarker = 0;
          txt = txt.Substring(0, 11) + GetCheckSum(txt).ToString();
          int wholeSet = 0;
          for(wholeSet = 1; wholeSet <= System.Convert.ToInt32(incomingString.Length); wholeSet++)
          {
            int currentASCII = (int)Convert.ToChar((incomingString.Substring(wholeSet - 1, 1))) - 48;
            string currentLetter = "";
            if(wholeSet == 1)
            {
              DrawSet(UPCABegin, placeMarker, 0);
              DrawSet(UPCALeft[currentASCII], placeMarker, 0);
            }
            else if(wholeSet <= 5)
            {
              DrawSet(UPCALeft[currentASCII], placeMarker, 6);
            }
            else if(wholeSet == 6)
            {
              DrawSet(UPCALeft[currentASCII], placeMarker, 6);
              DrawSet(UPCAMiddle, placeMarker, 0);
            }
            else if(wholeSet <= 11)
            {
              DrawSet(UPCARight[currentASCII], placeMarker, 6);
            }
            else if(wholeSet == 12)
            {
              DrawSet(UPCARight[currentASCII], placeMarker, 0);
              DrawSet(UPCAEnd, placeMarker, 0);
            }
          }
          
          System.Drawing.Font font = new System.Drawing.Font("Courier New, Bold", 9);
          try
          {
            g.DrawString(txt.Substring(0, 1), font, Brushes.Black, new System.Drawing.PointF(0, 67));
            g.DrawString(txt.Substring(1, 5), font, Brushes.Black, new System.Drawing.PointF(22, 67));
            g.DrawString(txt.Substring(6, 5), font, Brushes.Black, new System.Drawing.PointF(60, 67));
            g.DrawString(txt.Substring(11, 1), font, Brushes.Black, new System.Drawing.PointF(108, 67));
          }
          finally
          {
            font.Dispose();
          }
          img = Image.FromHbitmap(newBitmap.GetHbitmap());
          return img;
        }
    
        public int GetCheckSum(string barCode)
        {
          string leftSideOfBarCode = barCode.Substring(0, 11);
          int total = 0;
          int currentDigit = 0;
          int i = 0;
          for( i = 0; i <= leftSideOfBarCode.Length - 1; i++)
          {
            currentDigit = Convert.ToInt32(leftSideOfBarCode.Substring(i, 1));
            if(((i - 1) % 2 == 0))
            {
              total += currentDigit * 1;
            }
            else
            {
              total += currentDigit * 3;
            }
          }
          int iCheckSum = (10 - (total % 10)) % 10;
          return iCheckSum;
        }
    
        private void DrawSet(string upcCode, int drawLocation, int barHeight)
        {
          int[] currentLetterArray = new int[upcCode.Length];
          placeMarker += upcCode.Length;
          barHeight = barCodeHeight - barHeight;
          int i = 0;
          for(i = 0; i <= upcCode.Length - 1; i++)
          {
            currentLetterArray[i] = Convert.ToInt16(upcCode.Substring(i, 1));
          }
          for(i = 0; i <= upcCode.Length - 1; i++)
          {
            if(currentLetterArray[i] == 0)
            {
              g.DrawLine(Pens.White, i + (drawLocation), 0, i + (drawLocation), barHeight - 6);
            }
            else if(currentLetterArray[i] == 1)
            {
              g.DrawLine(Pens.Black, i + (drawLocation), 0, i + (drawLocation), barHeight - 6);
            }
          }
        }
    
      }
    }

  2. #2
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: Help converting C# class to VB.NET

    there are online converters that will do this for you like
    http://labs.developerfusion.co.uk/co...arp-to-vb.aspx
    and here is the output
    Code:
    Imports System
    Imports System.Collections.Generic
    Imports System.Text
    Imports System.Drawing
    
    Namespace UPCA
        Class cUPCA
            Private newBitmap As Bitmap
            Private g As Graphics
            Private barCodeHeight As Integer = 80
            Private placeMarker As Integer = 0
            Private imageWidth As Integer = 0
            Private imageScale As Single = 1
            Private UPCABegin As String = "0000000000000101"
            Private UPCALeft As String() = {"0001101", "0011001", "0010011", "0111101", "0100011", "0110001", _
            	"0101111", "0111011", "0110111", "0001011"}
            Private UPCAMiddle As String = "01010"
            Private UPCARight As String() = {"1110010", "1100110", "1101100", "1000010", "1011100", "1001110", _
            	"1010000", "1000100", "1001000", "1110100"}
            Private UPCAEnd As String = "1010000000000000"
            
            Public Function CreateBarCode(ByVal txt As String, ByVal scale As Integer) As Image
                Dim img As Image = Nothing
                imageWidth = 120
                imageScale = scale
                imageWidth = System.Convert.ToInt32(imageWidth * imageScale)
                Dim imageHeightHolder As Integer = System.Convert.ToInt32(barCodeHeight * imageScale)
                Dim incomingString As String = txt.ToUpper()
                If incomingString.Length = 0 Then
                    Return img
                End If
                Dim numberOfChars As Integer = incomingString.Length
                newBitmap = New Bitmap((imageWidth), imageHeightHolder, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
                g = Graphics.FromImage(newBitmap)
                g.ScaleTransform(imageScale, imageScale)
                Dim newRec As New Rectangle(0, 0, (imageWidth), imageHeightHolder)
                g.FillRectangle(New SolidBrush(Color.White), newRec)
                placeMarker = 0
                txt = txt.Substring(0, 11) + GetCheckSum(txt).ToString()
                Dim wholeSet As Integer = 0
                For wholeSet = 1 To System.Convert.ToInt32(incomingString.Length)
                    Dim currentASCII As Integer = CInt(Convert.ToChar((incomingString.Substring(wholeSet - 1, 1)))) - 48
                    Dim currentLetter As String = ""
                    If wholeSet = 1 Then
                        DrawSet(UPCABegin, placeMarker, 0)
                        DrawSet(UPCALeft(currentASCII), placeMarker, 0)
    ElseIf wholeSet <= 5 Then
                        DrawSet(UPCALeft(currentASCII), placeMarker, 6)
    ElseIf wholeSet = 6 Then
                        DrawSet(UPCALeft(currentASCII), placeMarker, 6)
                        DrawSet(UPCAMiddle, placeMarker, 0)
    ElseIf wholeSet <= 11 Then
                        DrawSet(UPCARight(currentASCII), placeMarker, 6)
    ElseIf wholeSet = 12 Then
                        DrawSet(UPCARight(currentASCII), placeMarker, 0)
                        DrawSet(UPCAEnd, placeMarker, 0)
                    End If
                Next
                
                Dim font As New System.Drawing.Font("Courier New, Bold", 9)
                Try
                    g.DrawString(txt.Substring(0, 1), font, Brushes.Black, New System.Drawing.PointF(0, 67))
                    g.DrawString(txt.Substring(1, 5), font, Brushes.Black, New System.Drawing.PointF(22, 67))
                    g.DrawString(txt.Substring(6, 5), font, Brushes.Black, New System.Drawing.PointF(60, 67))
                    g.DrawString(txt.Substring(11, 1), font, Brushes.Black, New System.Drawing.PointF(108, 67))
                Finally
                    font.Dispose()
                End Try
                img = Image.FromHbitmap(newBitmap.GetHbitmap())
                Return img
            End Function
            
            Public Function GetCheckSum(ByVal barCode As String) As Integer
                Dim leftSideOfBarCode As String = barCode.Substring(0, 11)
                Dim total As Integer = 0
                Dim currentDigit As Integer = 0
                Dim i As Integer = 0
                For i = 0 To leftSideOfBarCode.Length - 1
                    currentDigit = Convert.ToInt32(leftSideOfBarCode.Substring(i, 1))
                    If ((i - 1) Mod 2 = 0) Then
                        total += currentDigit * 1
                    Else
                        total += currentDigit * 3
                    End If
                Next
                Dim iCheckSum As Integer = (10 - (total Mod 10)) Mod 10
                Return iCheckSum
            End Function
            
            Private Sub DrawSet(ByVal upcCode As String, ByVal drawLocation As Integer, ByVal barHeight As Integer)
                Dim currentLetterArray As Integer() = New Integer(upcCode.Length - 1) {}
                placeMarker += upcCode.Length
                barHeight = barCodeHeight - barHeight
                Dim i As Integer = 0
                For i = 0 To upcCode.Length - 1
                    currentLetterArray(i) = Convert.ToInt16(upcCode.Substring(i, 1))
                Next
                For i = 0 To upcCode.Length - 1
                    If currentLetterArray(i) = 0 Then
                        g.DrawLine(Pens.White, i + (drawLocation), 0, i + (drawLocation), barHeight - 6)
    ElseIf currentLetterArray(i) = 1 Then
                        g.DrawLine(Pens.Black, i + (drawLocation), 0, i + (drawLocation), barHeight - 6)
                    End If
                Next
            End Sub
            
        End Class
    End Namespace
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  3. #3
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    710

    Re: Help converting C# class to VB.NET

    There's one big 'gotcha' that is missed by the online converters:

    Dim currentASCII As Integer = CInt(Convert.ToChar((incomingString.Substring(wholeSet - 1, 1)))) - 48

    should be:

    Dim currentASCII As Integer = CInt(Fix(Convert.ToChar((incomingString.Substring(wholeSet - 1, 1))))) - 48

    Note the use of the VB 'Fix' function - this is necessary to produce the same result as the original C# code. C# integer casts always truncate. The CInt macro rounds to the nearest integer. If you don't take this into account, the code will compile, but fail to produce the same results (which is the worst kind of conversion bug).
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com

  4. #4
    Frenzied Member ntg's Avatar
    Join Date
    Sep 2004
    Posts
    1,449

    Re: Help converting C# class to VB.NET

    Using reflector:
    VB Code:
    1. Friend Class cUPCA
    2.     ' Methods
    3.     Public Function CreateBarCode(ByVal txt As String, ByVal scale As Integer) As Image
    4.         Dim img As Image = Nothing
    5.         Me.imageWidth = 120
    6.         Me.imageScale = scale
    7.         Me.imageWidth = Convert.ToInt32(CSng((Me.imageWidth * Me.imageScale)))
    8.         Dim imageHeightHolder As Integer = Convert.ToInt32(CSng((Me.barCodeHeight * Me.imageScale)))
    9.         Dim incomingString As String = txt.ToUpper
    10.         If (incomingString.Length = 0) Then
    11.             Return img
    12.         End If
    13.         Dim length As Integer = incomingString.Length
    14.         Me.newBitmap = New Bitmap(Me.imageWidth, imageHeightHolder, PixelFormat.Format32bppArgb)
    15.         Me.g = Graphics.FromImage(Me.newBitmap)
    16.         Me.g.ScaleTransform(Me.imageScale, Me.imageScale)
    17.         Dim newRec As New Rectangle(0, 0, Me.imageWidth, imageHeightHolder)
    18.         Me.g.FillRectangle(New SolidBrush(Color.White), newRec)
    19.         Me.placeMarker = 0
    20.         txt = (txt.Substring(0, 11) & Me.GetCheckSum(txt).ToString)
    21.         Dim wholeSet As Integer = 0
    22.         wholeSet = 1
    23.         Do While (wholeSet <= Convert.ToInt32(incomingString.Length))
    24.             Dim currentASCII As Integer = (Convert.ToChar(incomingString.Substring((wholeSet - 1), 1)) - "0"c)
    25.             If (wholeSet = 1) Then
    26.                 Me.DrawSet(Me.UPCABegin, Me.placeMarker, 0)
    27.                 Me.DrawSet(Me.UPCALeft(currentASCII), Me.placeMarker, 0)
    28.             ElseIf (wholeSet <= 5) Then
    29.                 Me.DrawSet(Me.UPCALeft(currentASCII), Me.placeMarker, 6)
    30.             ElseIf (wholeSet = 6) Then
    31.                 Me.DrawSet(Me.UPCALeft(currentASCII), Me.placeMarker, 6)
    32.                 Me.DrawSet(Me.UPCAMiddle, Me.placeMarker, 0)
    33.             ElseIf (wholeSet <= 11) Then
    34.                 Me.DrawSet(Me.UPCARight(currentASCII), Me.placeMarker, 6)
    35.             ElseIf (wholeSet = 12) Then
    36.                 Me.DrawSet(Me.UPCARight(currentASCII), Me.placeMarker, 0)
    37.                 Me.DrawSet(Me.UPCAEnd, Me.placeMarker, 0)
    38.             End If
    39.             wholeSet += 1
    40.         Loop
    41.         Using font As Font = New Font("Courier New, Bold", 9!)
    42.             Me.g.DrawString(txt.Substring(0, 1), font, Brushes.Black, New PointF(0!, 67!))
    43.             Me.g.DrawString(txt.Substring(1, 5), font, Brushes.Black, New PointF(22!, 67!))
    44.             Me.g.DrawString(txt.Substring(6, 5), font, Brushes.Black, New PointF(60!, 67!))
    45.             Me.g.DrawString(txt.Substring(11, 1), font, Brushes.Black, New PointF(108!, 67!))
    46.         End Using
    47.         Return Image.FromHbitmap(Me.newBitmap.GetHbitmap)
    48.     End Function
    49.  
    50.     Private Sub DrawSet(ByVal upcCode As String, ByVal drawLocation As Integer, ByVal barHeight As Integer)
    51.         Dim currentLetterArray As Integer() = New Integer(upcCode.Length  - 1) {}
    52.         Me.placeMarker = (Me.placeMarker + upcCode.Length)
    53.         barHeight = (Me.barCodeHeight - barHeight)
    54.         Dim i As Integer = 0
    55.         i = 0
    56.         Do While (i <= (upcCode.Length - 1))
    57.             currentLetterArray(i) = Convert.ToInt16(upcCode.Substring(i, 1))
    58.             i += 1
    59.         Loop
    60.         i = 0
    61.         Do While (i <= (upcCode.Length - 1))
    62.             If (currentLetterArray(i) = 0) Then
    63.                 Me.g.DrawLine(Pens.White, (i + drawLocation), 0, (i + drawLocation), (barHeight - 6))
    64.             ElseIf (currentLetterArray(i) = 1) Then
    65.                 Me.g.DrawLine(Pens.Black, (i + drawLocation), 0, (i + drawLocation), (barHeight - 6))
    66.             End If
    67.             i += 1
    68.         Loop
    69.     End Sub
    70.  
    71.     Public Function GetCheckSum(ByVal barCode As String) As Integer
    72.         Dim leftSideOfBarCode As String = barCode.Substring(0, 11)
    73.         Dim total As Integer = 0
    74.         Dim currentDigit As Integer = 0
    75.         Dim i As Integer = 0
    76.         i = 0
    77.         Do While (i <= (leftSideOfBarCode.Length - 1))
    78.             currentDigit = Convert.ToInt32(leftSideOfBarCode.Substring(i, 1))
    79.             If (((i - 1) Mod 2) = 0) Then
    80.                 total = (total + currentDigit)
    81.             Else
    82.                 total = (total + (currentDigit * 3))
    83.             End If
    84.             i += 1
    85.         Loop
    86.         Return ((10 - (total Mod 10)) Mod 10)
    87.     End Function
    88.  
    89.  
    90.     ' Fields
    91.     Private barCodeHeight As Integer = 80
    92.     Private g As Graphics
    93.     Private imageScale As Single = 1!
    94.     Private imageWidth As Integer
    95.     Private newBitmap As Bitmap
    96.     Private placeMarker As Integer
    97.     Private UPCABegin As String = "0000000000000101"
    98.     Private UPCAEnd As String = "1010000000000000"
    99.     Private UPCALeft As String() = New String() { "0001101", "0011001", "0010011", "0111101", "0100011", "0110001", "0101111", "0111011", "0110111", "0001011" }
    100.     Private UPCAMiddle As String = "01010"
    101.     Private UPCARight As String() = New String() { "1110010", "1100110", "1101100", "1000010", "1011100", "1001110", "1010000", "1000100", "1001000", "1110100" }
    102. End Class
    "Feel the force...read the source..."
    Utilities: POPFileDebugViewProcess ExplorerWiresharkKeePassUltraVNCPic2Ascii
    .Net tools & open source: DotNetNukelog4NetCLRProfiler
    My open source projects: Thales SimulatorEFT CalculatorSystem Info ReporterVSS2SVNIBAN Functions
    Customer quote: "If the server has a RAID array, why should we bother with backups?"
    Programmer quote: "I never comment my code. Something that is hard to write should be impossible to comprehend."
    Ignorant quote: "I have no respect for universities, as they teach not practicle stuff, and charge money for"

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2005
    Location
    USA Washington
    Posts
    191

    Re: Help converting C# class to VB.NET

    Hey, thanks all of you guys. I'll try it out.. Thank you so much..

    I'll post more to let you know how its going.

    Or if I have anymore problems.


    Thanks again.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    May 2005
    Location
    USA Washington
    Posts
    191

    Re: Help converting C# class to VB.NET

    Okay, So I used the Fix function and the syntax was all right but when I ran the program and got to that in code. It gave me this error:


    Code:
    An unhandled exception of type 'System.ArgumentException' occurred in microsoft.visualbasic.dll
    
    Additional information: Type of argument 'Number' is 'System.Char', which is not numeric.

    SO, I'm really confused on how that last part is worded... But I went ahead and used AscW() instead of the whole "Fix(convert(" section and it works perfectly.


    Thank you so much everyone.
    If anyone's interested its a barcode class (obviously) :] its pretty cool.
    I could upload it if any of you wanted..


    Thanks again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width