Need help converting VB to C#

Hi

Can someone help me by converting this VB to C#.

Thanks a bunch.

James Keele

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox(Modulo10(TextBox1.Text))
End Sub

Private Function Modulo10(ByVal strNummer As String) As Integer

'strNummer may only contain numbers between 0 und 9!

Dim intTable(10) As Integer
Dim intTransfer As Integer
Dim intIndex As Integer

intTable(0) = 0 : intTable(1) = 9
intTable(2) = 4 : intTable(3) = 6
intTable(4) = 8 : intTable(5) = 2
intTable(6) = 7 : intTable(7) = 1
intTable(8) = 3 : intTable(9) = 5

For intIndex = 1 To Len(strNummer)
intTransfer = intTable((intTransfer + Mid(strNummer, intIndex, 1)) Mod 10)
Next

Modulo10 = (10 - intTransfer) Mod 10

End Function

End Class




Answer this question

Need help converting VB to C#

  • yosonu

    Yes - you're right about that.

    However, you might want to check that array declaration - it has a one-off error in addition to not being compilable.

    David Anton
    www.tangiblesoftwaresolutions.com
    Instant C#: VB to C# converter
    Instant VB: C# to VB converter
    Instant C++: C# to C++ converter, VB to C++ converter
    Instant Python: VB to Python converter



  • softwarejaeger

    you are correct, thanks for that :-)

  • Saitham8

    It may help you in some cases. In the current case (the original post sample) it bombs.

    David Anton
    www.tangiblesoftwaresolutions.com
    Instant C#: VB to C# converter
    Instant VB: C# to VB converter
    Instant C++: C# to C++ converter, VB to C++ converter
    Instant Python: VB to Python converter



  • Simon Bowles UK

    it was done in my last post James :-)

    There are a few ways to initialize the arrays, which both have been posted



  • Sunath

    I'll give it a shot. Remember there is a button click event there so drag and drop a button and double click to make the event. Then in this button click event:

    MessageBox.Show(this.Modulo10(this.TextBox1.Text).ToString())

     

    the function:

     



    private int Modulo10(string strNumber)
    {
       int[] intTable = new int[10];
       int intTransfer = 0;
       int intIndex = 0;
     
       intTable[ 0] = 0;
       intTable[ 1] = 9;
       intTable[ 2] = 4;
       intTable[ 3] = 6;
       intTable [ 4] = 8;
       intTable [ 5] = 2;
       intTable[ 6] = 7;
       intTable[ 7] = 1;
       intTable[ 8] = 3;
       intTable[9] = 5;
     
       for(int counter = 0 to strNumber.Length - 1)
       {
          intTransfer = intTable[intTransfer + Convert.ToInt32(strNumber.SubString(counter, 1))] % 10;
       }
     
       return 10 - intTransfer % 10;
    }

     

     

    the bold line I'm unsure of but gave it my best shot.



  • enric vives

    >> However, you might want to check that array declaration - it has a one-off error in addition to not being compilable.

    Actually, it's your code that's making the array one bigger than necessary -- but then, the error was in the original (probably a C programmer writing in VB), so we'll let that pass.

    However, you both sem to be missing the obvious when it comes to the array:

    int[] intTable = {0,9,4,6,8,2,7,1,3,5};

    Further, since we are only plucking one character at a time at of the string, we can use the indexer (strNumber[counter])--- Or, better yet, the enumerator:

    foreach (char c in strNumber)
    {
    intTransfer = intTable[(intTransfer + Convert.ToInt32(c.ToString())) %
    10
    ] ;
    }

    (Unfortunately, I still had to convert the char back into a string because ToInt32("0") == 0, while ToInt32('0') == 48 (it's ascii value))

    (also, ahmedilyas, note that the "% 10" has to go inside the brackets)



  • LouisVanAlphen

    I also have this code in C/C++

    int modulo10(const char* lpszNumber)

    // 'lpszNumber' can only be digits between 0 und 9!

    static const int nTable[] = { 0, 9, 4, 6, 8, 2, 7, 1, 3, 5 };
    int nTransfer = 0;

    while (*lpszNumber)
    {
    nTransfer = nTable[(nTransfer + *lpszNumber - '0') % 10];
    ++lpszNumber;
    }

    return (10 - nUebertrag) % 10;
    }

    So maybe this will help getting this into C#.

    Thanks again

    James



  • mbeninca

    Yes - our converters don't have built-in refactoring and optimization

    David Anton
    www.tangiblesoftwaresolutions.com
    Instant C#: VB to C# converter
    Instant VB: C# to VB converter
    Instant C++: C# to C++ converter, VB to C++ converter
    Instant Python: VB to Python converter



  • dag.loraas

    No, my question is still not answered because I still cannot get rid of the error messages.

  • OmegaMan

    Thanks for helping ahmedilyas.

    Using your suggestion, I get the following errors:

    ahmedilyas wrote:


    for(int counter = 0 to strNumber.Length - 1)

    VS2005 says the "to" and the "strNumber" ;expected

    I am also using a message box coded as below:

    private void button1_Click(object sender, EventArgs e)

    {

    MessageBox.Show(Modulo10(TextBox1.Text));

    }

    But here it says:

    Error 4 Argument '1': cannot convert from 'int' to 'string' and

    Error 3 The best overloaded method match for 'System.Windows.Forms.MessageBox.Show(string)' has some invalid arguments

    How can I fix this

    Thanks to everyone for helping........




  • VuaCorona

    Is your question answered or not

  • GPinNY

    David:

    intTransfer = intTable[(intTransfer + strNummer.Substring(intIndex - 1, 1)) % 10];

    would produce an error when compiling - since SubString returns a string but the intTransfer is an integer variable :-) So place Convert.ToInt32() around the strNummer and before the % as in my attempted solution.

    I guess overall it maybe:

     

    private int Modulo10(string strNumber)
    {
       int[] intTable = new int[10];
       int intTransfer = 0;
       int intIndex = 0;
     
       intTable[ 0] = 0;
       intTable[ 1] = 9;
       intTable[ 2] = 4;
       intTable[ 3] = 6;
       intTable [ 4] = 8;
       intTable [ 5] = 2;
       intTable[ 6] = 7;
       intTable[ 7] = 1;
       intTable[ 8] = 3;
       intTable[9] = 5;
     
       for(int counter = 0 to strNumber.Length - 1)
       {
          intTransfer = intTable[intTransfer + Convert.ToInt32(strNumber.SubString(counter, 1)) % 10];
       }
     
       return (10 - intTransfer) % 10;
    }



  • Mike Uttormark

    Hai friend

    this site will really help u

    http://www.developerfusion.co.uk/utilities/convertvbtocsharp.aspx

    Regards

    Ratheesh



  • ooh_flirty

    Instant C# produces the following (note that array sizes are used in C# declarations, not upper bound):

    //TODO: INSTANT C# TODO TASK: Insert the following converted event handlers at the end of the 'InitializeComponent' method for forms or into a constructor for other classes:
    Button1.Click += new System.EventHandler(Button1_Click);

     private void Button1_Click(object sender, System.EventArgs e)
     {
      MessageBox.Show(Modulo10(TextBox1.Text));
     }

     private int Modulo10(string strNummer)
     {

      //strNummer may only contain numbers between 0 und 9!

      int[] intTable = new int[11];
      int intTransfer = 0;
      int intIndex = 0;

      intTable[0] = 0;
      intTable[1] = 9;
      intTable[2] = 4;
      intTable[3] = 6;
      intTable[4] = 8;
      intTable[5] = 2;
      intTableDevil = 7;
      intTable[7] = 1;
      intTableMusic = 3;
      intTable[9] = 5;

      for (intIndex = 1; intIndex <= strNummer.Length; intIndex++)
      {
       intTransfer = intTable[(intTransfer + strNummer.Substring(intIndex - 1, 1)) % 10];
      }

      return (10 - intTransfer) % 10;

     }

    }

    David Anton
    www.tangiblesoftwaresolutions.com
    Instant C#: VB to C# converter
    Instant VB: C# to VB converter
    Instant C++: C# to C++ converter, VB to C++ converter
    Instant Python: VB to Python converter



  • Need help converting VB to C#