• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 412
  • Last Modified:

convert vb.net to C#

I need this correctly converted.
I appreciate it.
Dim chinese As String() = New String() {"Rat", _
    "Ox", _
    "Tiger", _
    "Hare", _
    "Dragon", _
    "Snake", _
    "Horse", _
    "Sheep", _
    "Monkey", _
    "Fowl", _
    "Dog", _
    "Pig"}
 
    Dim branches As String() = New String() {"Zi", _
    "Chou", _
    "Yin", _
    "Mao", _
    "Chen", _
    "Si", _
    "Wu", _
    "Wei", _
    "Shen", _
    "You", _
    "Xu", _
    "Hai"}
 
    Dim stems As String() = New String() {"Jia", _
    "Yi", _
    "Bing", _
    "Ding", _
    "Wu", _
    "Ji", _
    "Geng", _
    "Xin", _
    "Ren", _
    "Gui"}
 
    Dim chinesenewyear As Single() = New Single() {1.31, 2.19, 2.08, 1.29, 2.16, 2.04, 1.25, 2.13, 2.02, 1.22, _
                                       2.1, 1.3, 2.18, 2.06, 1.26, 2.14, 2.03, 1.23, 2.11, 2.01, _
                                       2.2, 2.08, 1.28, 2.16, 2.05, 1.25, 2.13, 2.02, 1.23, 2.1, _
                                       1.3, 2.17, 2.06, 1.26, 2.14, 2.04, 1.24, 2.11, 1.31, 2.19, _
                                       2.08, 1.27, 2.15, 2.05, 1.25, 2.13, 2.02, 1.22, 2.1, 1.29, _
                                       2.17, 2.06, 1.27, 2.14, 2.03, 1.24, 2.12, 1.31, 2.18, 2.08, _
                                       1.28, 2.15, 2.05, 1.25, 2.13, 2.02, 1.21, 2.09, 1.3, 2.17, _
                                       2.06, 1.27, 2.15, 2.03, 1.23, 2.11, 1.31, 2.18, 2.07, 1.28, _
                                       2.16, 2.05, 1.25, 2.13, 2.02, 2.2, 2.09, 1.29, 2.17, 2.06, _
                                       1.27, 2.15, 2.04, 1.23, 2.1, 1.31, 2.19, 2.07, 1.28, 2.16}
    Function getChinese(ByVal day As Integer, ByVal month As Integer, ByVal year As Integer)
        Dim newYear As Single
        Dim newMonth As Integer
        Dim newDay As Integer
        Dim chineseBranch = (year - 3) Mod 12
        Dim chineseStem = (year - 3) Mod 10
 
        If ((year > 1900) And (year < 1999)) Then
            newYear = chinesenewyear(year - 1899)
            newMonth = Math.Floor(newYear)
            newDay = Math.Round((newYear * 100) - (newMonth * 100))
 
            If ((month > newMonth) Or ((month = newMonth) And (day >= newDay))) Then
                ' this year
                chineseBranch = (year - 3) Mod 12
            Else
                ' last year
                chineseBranch = (year - 4) Mod 12
            End If
 
        Else
            chineseBranch = (year - 3) Mod 12
        End If
 
        If (chineseBranch = 0) Then
            chineseBranch = 12
        End If
        Return chinese(chineseBranch - 1)
    End Function
 
    Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
        TextBox1.Text = getChinese(DateTimePicker1.Value.Day, DateTimePicker1.Value.Month, DateTimePicker1.Value.Year)
    End Sub
0
mathieu_cupryk
Asked:
mathieu_cupryk
  • 2
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
That looks like pretty standard, uncomplicated VB code...did none of the free online converters work for you?
http://www.developerfusion.com/tools/convert/vb-to-csharp/

You would need to change the case of the controls as the default in C# is lowercase ("textBox1" vs. "TextBox1").
You also need to manually wire up the events since C# doesn't have the "Handles" keyword.

It produced:
string[] chinese = new string[] { "Rat", "Ox", "Tiger", "Hare", "Dragon", "Snake", "Horse", "Sheep", "Monkey", "Fowl", 
"Dog", "Pig" };

string[] branches = new string[] { "Zi", "Chou", "Yin", "Mao", "Chen", "Si", "Wu", "Wei", "Shen", "You", 
"Xu", "Hai" };

string[] stems = new string[] { "Jia", "Yi", "Bing", "Ding", "Wu", "Ji", "Geng", "Xin", "Ren", "Gui"
 };

float[] chinesenewyear = new float[] { 1.31, 2.19, 2.08, 1.29, 2.16, 2.04, 1.25, 2.13, 2.02, 1.22, 
2.1, 1.3, 2.18, 2.06, 1.26, 2.14, 2.03, 1.23, 2.11, 2.01, 
2.2, 2.08, 1.28, 2.16, 2.05, 1.25, 2.13, 2.02, 1.23, 2.1, 
1.3, 2.17, 2.06, 1.26, 2.14, 2.04, 1.24, 2.11, 1.31, 2.19, 
2.08, 1.27, 2.15, 2.05, 1.25, 2.13, 2.02, 1.22, 2.1, 1.29, 
2.17, 2.06, 1.27, 2.14, 2.03, 1.24, 2.12, 1.31, 2.18, 2.08, 
1.28, 2.15, 2.05, 1.25, 2.13, 2.02, 1.21, 2.09, 1.3, 2.17, 
2.06, 1.27, 2.15, 2.03, 1.23, 2.11, 1.31, 2.18, 2.07, 1.28, 
2.16, 2.05, 1.25, 2.13, 2.02, 2.2, 2.09, 1.29, 2.17, 2.06, 
1.27, 2.15, 2.04, 1.23, 2.1, 1.31, 2.19, 2.07, 1.28, 2.16
 };
public object getChinese(int day, int month, int year)
{
    float newYear = 0;
    int newMonth = 0;
    int newDay = 0;
    var chineseBranch = (year - 3) % 12;
    var chineseStem = (year - 3) % 10;
    
    if (((year > 1900) & (year < 1999))) {
        newYear = chinesenewyear(year - 1899);
        newMonth = Math.Floor(newYear);
        newDay = Math.Round((newYear * 100) - (newMonth * 100));
        
        if (((month > newMonth) | ((month == newMonth) & (day >= newDay)))) {
            // this year 
            chineseBranch = (year - 3) % 12;
        }
        else {
            // last year 
            chineseBranch = (year - 4) % 12;
        }
    }
    else {
        
        chineseBranch = (year - 3) % 12;
    }
    
    if ((chineseBranch == 0)) {
        chineseBranch = 12;
    }
    return chinese(chineseBranch - 1);
}

private void DateTimePicker1_ValueChanged(System.Object sender, System.EventArgs e)
{
    TextBox1.Text = getChinese(DateTimePicker1.Value.Day, DateTimePicker1.Value.Month, DateTimePicker1.Value.Year);
}

Open in new window

0
 
Kumaraswamy RCommented:
HI

Try to use code converter Url or Application :

http://www.carlosag.net/Tools/CodeTranslator/
0
 
mathieu_cuprykAuthor Commented:
   public class ChineseZodiac
    {
        string[] chinese = new string[] { "Rat", "Ox", "Tiger", "Hare", "Dragon", "Snake", "Horse", "Sheep", "Monkey", "Fowl", "Dog", "Pig" };
        string[] branches = new string[] { "Zi", "Chou", "Yin", "Mao", "Chen", "Si", "Wu", "Wei", "Shen", "You", "Xu", "Hai" };
        string[] stems = new string[] { "Jia", "Yi", "Bing", "Ding", "Wu", "Ji", "Geng", "Xin", "Ren", "Gui" };

        float[] chinesenewyear = new float[] { 1.31F, 2.19F, 2.08F, 1.29F, 2.16F, 2.04F, 1.25F, 2.13F, 2.02F, 1.22F,  
        2.1F, 1.3F, 2.18F, 2.06F, 1.26F, 2.14F, 2.03F, 1.23F, 2.11F, 2.01F, 2.2F, 2.08F, 1.28F, 2.16F, 2.05F, 1.25F,
        2.13F, 2.02F, 1.23F, 2.1F, 1.3F, 2.17F, 2.06F, 1.26F, 2.14F, 2.04F, 1.24F, 2.11F, 1.31F, 2.19F,  
        2.08F, 1.27F, 2.15F, 2.05F, 1.25F, 2.13F, 2.02F, 1.22F, 2.1F, 1.29F,  
        2.17F, 2.06F, 1.27F, 2.14F, 2.03F, 1.24F, 2.12F, 1.31F, 2.18F, 2.08F,  
        1.28F, 2.15F, 2.05F, 1.25F, 2.13F, 2.02F, 1.21F, 2.09F, 1.3F, 2.17F,  
        2.06F, 1.27F, 2.15F, 2.03F, 1.23F, 2.11F, 1.31F, 2.18F, 2.07F, 1.28F,  
        2.16F, 2.05F, 1.25F, 2.13F, 2.02F, 2.2F, 2.09F, 1.29F, 2.17F, 2.06F,  
        1.27F, 2.15F, 2.04F, 1.23F, 2.1F, 1.31F, 2.19F, 2.07F, 1.28F, 2.16F
         };



        public static object GetChinese(int day, int month, int year)
        {
            float newYear = 0;
            int newMonth = 0;
            int newDay = 0;
            var chineseBranch = (year - 3) % 12;
            var chineseStem = (year - 3) % 10;

            if (((year > 1900) & (year < 1999)))
            {
                newYear = chinesenewyear(year - 1899);
                newMonth = Convert.ToInt32(Math.Floor(newYear));
                newDay = Convert.ToInt32(Math.Round((newYear * 100) - (newMonth * 100)));

                if (((month > newMonth) | ((month == newMonth) & (day >= newDay))))
                {
                    // this year  
                    chineseBranch = (year - 3) % 12;
                }
                else
                {
                    // last year  
                    chineseBranch = (year - 4) % 12;
                }
            }
            else
            {

                chineseBranch = (year - 3) % 12;
            }

            if ((chineseBranch == 0))
            {
                chineseBranch = 12;
            }
            return chinese(chineseBranch - 1);
        }

    }
}
0
 
mathieu_cuprykAuthor Commented:
k
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now