Click here to Skip to main content
15,904,653 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Please help me!
How can I convert this VB code to C# ?

VB
Dim i, j, Rand As Integer
        ClicksMouse = 0
        For i = 1 To 6
            Radombutton(i).Image = Nothing
        Next

        For i = 1 To 6
A1:
            Randomize()
            Rand = Int(6 * Rnd() + 1)

            'Kiểm tra trùng này !
            For j = 1 To i - 1
                If Rand = List1(j) Then GoTo A1
            Next
            List1(i) = Rand
        Next
Posted
Updated 22-May-11 23:04pm
v2
Comments
CPallini 23-May-11 6:32am    
Please note that such a usage of the Randomize() function makes (statistically) no sense.

Try:
int i = 0;
int j = 0;
int Rand = 0;
ClicksMouse = 0;
for (i = 1; i <= 6; i++) {
	Radombutton(i).Image = null;
}
for (i = 1; i <= 6; i++) {
	A1:
	VBMath.Randomize();
	Rand = Conversion.Int(6 * VBMath.Rnd() + 1);
	//Kiểm tra trùng này !
	for (j = 1; j <= i - 1; j++) {
		if (Rand == List1(j))
			goto A1;
	}
	List1(i) = Rand;
}
This is not tested: I use a website for C# <=> VB conversion: http://www.developerfusion.com/tools/convert/vb-to-csharp/[^]
 
Share this answer
 
Comments
[no name] 23-May-11 5:10am    
You bet me in this by just few seconds...
Ashishmau 23-May-11 5:22am    
Nice link...
OriginalGriff 23-May-11 5:33am    
Useful, isn't it? :laugh:
It's not perfect, but it does the job for most stuff.
Hi,

This is manually translated but still untested (and I don't know what you're trying to do with this..

Anyway, here is the partially-refactored code:

C#
public void Method()
{
    Random rnd = new Random();
    for (int i = 1; i <= 6; i++)
    {
        // RandomButton is a collection, right?
        RandomButton[i].Image = null;
    }
    for (int i = 1; i <= 6; i++)
    {
        bool checkSuccessful = true;
        int randomNumber = 6 * rnd.Next() + 1;
        // Kiểm tra trùng này !
        for (int j = 1; j < i - 1; j++)
        {
            // Also List1 is a collection?
            if (randomNumber == List1[j])
            {
                // Never use "GOTO"! use a variable to check.
                checkSuccessful = false;
                // Not nice but this is the only short solution I found for.
                break;
            }
        }
        // Check if the inner-loop-check was not failing.
        if (checkSuccessful)
        {
            List1[i] = randomNumber;
        }
    }
}



Hope this helps.

Best regards and happy coding,
Stops
 
Share this answer
 
Here is your C# code:

int i = 0;
int j = 0;
int Rand = 0;
ClicksMouse = 0;
for (i = 1; i <= 6; i++) {
    Radombutton(i).Image = null;
}

for (i = 1; i <= 6; i++) {
    A1:
    VBMath.Randomize();
    Rand = Conversion.Int(6 * VBMath.Rnd() + 1);

    //Kiểm tra trùng này !
    for (j = 1; j <= i - 1; j++) {
        if (Rand == List1(j))
            goto A1;
    }
    List1(i) = Rand;
}
 
Share this answer
 
Speaking about the tricky part (random extraction of numbers without repetitions) you don't need to 'convert' exactly that ugly VB, you may write a better code doing the same thing, try, for instance:
C#
static void Main()
    {
        const int SIZE = 6;
        int[] list = new int[SIZE];
        Random rnd = new Random();
        for (int i = 0; i < SIZE; i++)
        {
            list[i] = i+1;
        }
        for (int i = 0; i < SIZE - 1; i++)
        {
            int r = rnd.Next(SIZE-i);

            int tmp = list[SIZE - 1];
            list[SIZE - 1] = list[r];
            list[r] = tmp;
        }
        for (int i = 0; i < SIZE; i++ )
            Console.WriteLine("list[{0}]={1}", i, list[i]);
    }


The algorithm is explained here[^].
 
Share this answer
 
v2
I've don't exactly what the way to convert the code from vb to c#,
bt i hav done through this http://www.developerfusion.com/tools/convert/vb-to-csharp/
it gives u correct code.......
 
Share this answer
 
Comments
Arun_Reddy 24-May-11 9:29am    
If ur problem gets solved plz click the "accept solution" button
Thank u..........
I think that you can use this code converter
Telerik Code Converter
 
Share this answer
 
As many have pointed out, there are many VB.Net < = > C# converters out there, some better than others, some free, some commercial.
The standard rule is this...

They will convert most code from one to the other, but none of them are perfect, and you will still need to go through and debug the converted code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900