locked
Equivalent of this C# code in VB RRS feed

  • Question

  • User148040890 posted

    The code in C# is as follows:

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)

            {

                //filling items in combobox1

                if (comboBox1.SelectedIndex==0)

                {

                comboBox2.Items.Clear();

                for (char i ='A' ; i < 'D'; i++)

                {

                    comboBox2.Items.Add(i);

                }

                }

                if (comboBox1.SelectedIndex == 1 || comboBox1.SelectedIndex == 2 || comboBox1.SelectedIndex == 3)

                {

                    comboBox2.Items.Clear();

                    for (char i = 'A'; i < 'E'; i++)

                    {

                        comboBox2.Items.Add(i);

                    }

                }

                if (comboBox1.SelectedIndex == 4)

                {

                    comboBox2.Items.Clear();

                    for (char i = 'A'; i < 'F'; i++)

                    {

                        comboBox2.Items.Add(i);

                    }

                }

                if (comboBox1.SelectedIndex == 5 || comboBox1.SelectedIndex == 6)

                {

                    comboBox2.Items.Clear();

                    for (char i = 'A'; i < 'G'; i++)

                    {

                        comboBox2.Items.Add(i);

                    }

                }

                if (comboBox1.SelectedIndex == 7 || comboBox1.SelectedIndex == 8 || comboBox1.SelectedIndex == 9)

                {

                    comboBox2.Items.Clear();

                    for (char i = 'A'; i < 'I'; i++)

                    {

                        comboBox2.Items.Add(i);

                    }

                }

            }




    I want to implement this code in VB.

    Using a converter, i got this:


    Private Sub comboBox1_SelectedIndexChanged(sender As Object, e As EventArgs)

    'filling items in combobox1

    If comboBox1.SelectedIndex = 0 Then

    comboBox2.Items.Clear()

    For i As Char = "A"C To "D"C - 1

    comboBox2.Items.Add(i)

    Next

    End If

    If comboBox1.SelectedIndex = 1 OrElse comboBox1.SelectedIndex = 2 OrElse comboBox1.SelectedIndex = 3 Then

    comboBox2.Items.Clear()

    For i As Char = "A"C To "E"C - 1

    comboBox2.Items.Add(i)

    Next

    End If

    If comboBox1.SelectedIndex = 4 Then

    comboBox2.Items.Clear()

    For i As Char = "A"C To "F"C - 1

    comboBox2.Items.Add(i)

    Next

    End If

    If comboBox1.SelectedIndex = 5 OrElse comboBox1.SelectedIndex = 6 Then

    comboBox2.Items.Clear()

    For i As Char = "A"C To "G"C - 1

    comboBox2.Items.Add(i)

    Next

    End If

    If comboBox1.SelectedIndex = 7 OrElse comboBox1.SelectedIndex = 8 OrElse comboBox1.SelectedIndex = 9 Then

    comboBox2.Items.Clear()

    For i As Char = "A"C To "I"C - 1

    comboBox2.Items.Add(i)

    Next

    End If

    End Sub



    When i compile the code, it returns an error saying Error


    " 'For' loop control variable cannot be of type 'Char' because the type does not support the required operators"

    Can you please help me out with this?

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                //filling items in combobox1
                if (comboBox1.SelectedIndex==0)
                {
                comboBox2.Items.Clear();
                for (char i ='A' ; i < 'D'; i++)
                {
                    comboBox2.Items.Add(i);
                }
                }
                if (comboBox1.SelectedIndex == 1 || comboBox1.SelectedIndex == 2 || comboBox1.SelectedIndex == 3)
                {
                    comboBox2.Items.Clear();
                    for (char i = 'A'; i < 'E'; i++)
                    {
                        comboBox2.Items.Add(i);
                    }
                }
                if (comboBox1.SelectedIndex == 4)
                {
                    comboBox2.Items.Clear();
                    for (char i = 'A'; i < 'F'; i++)
                    {
                        comboBox2.Items.Add(i);
                    }
                }
                if (comboBox1.SelectedIndex == 5 || comboBox1.SelectedIndex == 6)
                {
                    comboBox2.Items.Clear();
                    for (char i = 'A'; i < 'G'; i++)
                    {
                        comboBox2.Items.Add(i);
                    }
                }
                if (comboBox1.SelectedIndex == 7 || comboBox1.SelectedIndex == 8 || comboBox1.SelectedIndex == 9)
                {
                    comboBox2.Items.Clear();
                    for (char i = 'A'; i < 'I'; i++)
                    {
                        comboBox2.Items.Add(i);
                    }
                }
       
    Wednesday, November 24, 2010 7:16 PM

Answers

  • User397347636 posted

    Your 'for' loops will have to look like this in VB:

     

    For i As Integer = AscW("A"c) To AscW("D"c) - 1


    and then within the loop you'll have to change back to a Char:

    ChrW(i)


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 25, 2010 12:01 PM

All replies

  • User535082756 posted

    hi

    i tried this and it worked for me.....

    hope it helps for you tooo

    Dim i As Char = "A"
            Dim j As Char = "I"
            Dim c As Integer = 0
            While i <= j
                DropDownList1.Items.Add(i)
                c += 1
                i = ChrW(65 + c)
            End While

     

    thanks, 

    Thursday, November 25, 2010 1:46 AM
  • User397347636 posted

    Your 'for' loops will have to look like this in VB:

     

    For i As Integer = AscW("A"c) To AscW("D"c) - 1


    and then within the loop you'll have to change back to a Char:

    ChrW(i)


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 25, 2010 12:01 PM
  • User148040890 posted

    Thanks for the inputs friends. Lemme fit them in.


    Thanks again!

    Thursday, November 25, 2010 12:41 PM