Click here to Skip to main content
15,905,607 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have a problem in converting the vb.net code to c# .
In vb.net they have used for each loop where they are changing the iterable variable
and in IF condition they are comparing chat array to string where i am getting an error .

VB.NET CODE:




VB
Dim rndAlphabet As New Random
Dim rndAlphaDisplay As New Random

For Each CharValue As Char In captchaDigit.ToCharArray

              
             Dim intAlphaDisplay As Integer = rndAlphaDisplay.Next(0, 9)
             
           
             If strAlphaDisplay.ToCharArray(intAlphaDisplay, 1) = "1" Then
                 CharValue = alphabets(rndAlphabet.Next(0, 51))
             End If
next



C# code:

C#
 Random rndAlphabet = new Random();
                Random rndAlphaDisplay = new Random();


foreach (char CharValue in captchaDigit.ToCharArray) {

   

    int intAlphaDisplay = rndAlphaDisplay.Next(0, 9);
   
    )
    if (strAlphaDisplay.ToCharArray(intAlphaDisplay, 1) == "1") {
        CharValue = alphabets(rndAlphabet.Next(0, 51));
    }
}



hope you can fix this

thanks in advance
Posted
Updated 17-Feb-12 3:44am
v2

C#
Random rndAlphabet = new Random();
Random rndAlphaDisplay = new Random();

foreach (char CharValue in captchaDigit.ToCharArray) {


	int intAlphaDisplay = rndAlphaDisplay.Next(0, 9);


	if (strAlphaDisplay.ToCharArray(intAlphaDisplay, 1) == "1") {
		CharValue = alphabets(rndAlphabet.Next(0, 51));
	}
}
 
Share this answer
 
Hello, try this code it will fix your problem and have nice ^_^ :

Random rndAlphabet = new Random();

Random rndAlphaDisplay = new Random();

Char xchar = ' ';

foreach (Char CharValue in captchaDigit.ToCharArray())
{

int intAlphaDisplay = rndAlphaDisplay.Next(0, 9);

if (strAlphaDisplay.ToCharArray(intAlphaDisplay, 1).First() == '1')
{
xchar = Convert.ToChar(alphabets[rndAlphabet.Next(0, 51)].ToString());
}
else
xchar = CharValue;
}
 
Share this answer
 
Hello,

'VB' takes it easy and it leds to be forgotten some elaborate works by developers.

I don't like 'VisualBasic.Net' because it's like an automatic car, but a nice car!

By the way I think you could try this:

C#
Random rndAlphabet = new Random();
Random rndAlphaDisplay = new Random();
 

foreach (char CharValue in captchaDigit.ToCharArray()) 
{  
    int intAlphaDisplay = rndAlphaDisplay.Next(0, 9);  
    
    if (strAlphaDisplay.ToCharArray(intAlphaDisplay, 1).First() == '1') 
    {
        CharValue = alphabets(rndAlphabet.Next(0, 51));
    }
}
 
Share this answer
 
v3
Comments
mohanrajkiller 21-Feb-12 7:52am    
is that possible to assign a value to an iterator variable in foreach loop
Shahin Khorshidnia 21-Feb-12 9:41am    
No, but you can try this:

Random rndAlphabet = new Random();
Random rndAlphaDisplay = new Random();

StringBuilder str = new StringBuilder();
foreach (char CharValue in captchaDigit.ToCharArray())
{
int intAlphaDisplay = rndAlphaDisplay.Next(0, 9);

str.Append((strAlphaDisplay.ToCharArray(intAlphaDisplay, 1).First() == '1')?alphabets(rndAlphabet.Next(0, 51)):CharValue);

}
captchaDigit = str.ToString();

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