I am trying to covert a line of code I have in ASP .Net VB:
VB Code:
strRandomText &= Mid(strCharacters, Int(Len(strCharacters) * Rnd()) + 1, 1)
How would I do this in C#?
Printable View
I am trying to covert a line of code I have in ASP .Net VB:
VB Code:
strRandomText &= Mid(strCharacters, Int(Len(strCharacters) * Rnd()) + 1, 1)
How would I do this in C#?
Give this a whirl
strRandomText += Mid(strCharacters, Int(Len(strCharacters) * Rnd()) + 1, 1);
That's also how it should be done in VB.NET. Mid, Int, Len, Rnd() are all compatibility functions.Code:Random r = new Random();
StringBuilder randomText = new StringBuilder();
// ...
randomText.Append(randcharacters.Substring(r.Next(0, characters.Length), 1));
Hi thanks for that.
I have another slight problem if you can help me....
VB Code:
Font objCAPTCHAFont = Font("Tahoma", 20, FontStyle.Italic or FontStyle.Bold);
I get this error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS1026: ) expected
I think its because i didnt do the OR statement correctly
The bitwise OR operator used to combine flags is a single pipe |.
Logical or, for conditional testing, is ||.Code:Font objCAPTCHAFont = Font("Tahoma", 20, FontStyle.Italic | FontStyle.Bold);
Thanks that great.... and one last thing (hopefully) can someone check to see if this is correct
VB Code:
arrColours[10, 2] = new Color(); //Holds an array of different colours (for the text) to display the captcha in. arrPen[10] = new Pen(); //Holds an array of different colours (for the lines) to display the captcha in. intArrIndex = intArrRnd * 4; //****************** //** Colour Array ** //****************** //Gray arrColours(0, 0) = Color.Gray; arrColours(0, 1) = Color.DarkGray; arrPen(0) = Pens.LightGray; //Green arrColours(1, 0) = Color.Green; arrColours(1, 1) = Color.DarkGreen; arrPen(1) = Pens.LightGreen; //Blue arrColours(2, 0) = Color.Blue; arrColours(2, 1) = Color.DarkBlue; arrPen(2) = Pens.LightBlue; //Red arrColours(3, 0) = Color.Red; arrColours(3, 1) = Color.DarkRed; arrPen(3) = Pens.LightPink; //Brown arrColours(4, 0) = Color.Brown; arrColours(4, 1) = Color.Black; arrPen(4) = Pens.Maroon;
You're mixing index operators between VB and C#. C# uses square bracket index notation exclusively.
I suggest you read a tutorial on C# if you are not familiar with the basic syntax.
There are also code converters available (see my sig for some examples) that could have sorted most of these issues for you, but as penagate says, you should have an understanding of C# keywords and operators.
I took your advice, looked at some tutorials but for some reason this
returns the errorVB Code:
arrColours[10, 2] = new Color(); //Holds an array of different colours (for the text) to display the captcha in.
The name 'arrColours' does not exist in the current context
Means exactly what it says. You have to declare all variables before you can use them.
If you're trying to declare a 2D array of Colors on that line it should be:The left hand side declares the arrColors variable as type Color[,], which is 2D array of Color. The right hand side creates a new 10 x 2 array of Color objects, and the equals sign assigns the array object to the variable.Code:Color[,] arrColors = new Color[10, 2];