|
-
Aug 31st, 2006, 04:54 AM
#1
Thread Starter
Addicted Member
Simple conversion help!
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#?
-
Aug 31st, 2006, 05:01 AM
#2
Hyperactive Member
Re: Simple conversion help!
Give this a whirl
strRandomText += Mid(strCharacters, Int(Len(strCharacters) * Rnd()) + 1, 1);
-
Aug 31st, 2006, 05:06 AM
#3
Re: Simple conversion help!
Code:
Random r = new Random();
StringBuilder randomText = new StringBuilder();
// ...
randomText.Append(randcharacters.Substring(r.Next(0, characters.Length), 1));
That's also how it should be done in VB.NET. Mid, Int, Len, Rnd() are all compatibility functions.
-
Aug 31st, 2006, 06:17 AM
#4
Thread Starter
Addicted Member
Re: Simple conversion help!
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
-
Aug 31st, 2006, 06:23 AM
#5
Re: Simple conversion help!
The bitwise OR operator used to combine flags is a single pipe |.
Code:
Font objCAPTCHAFont = Font("Tahoma", 20, FontStyle.Italic | FontStyle.Bold);
Logical or, for conditional testing, is ||.
-
Aug 31st, 2006, 08:36 AM
#6
Thread Starter
Addicted Member
Re: Simple conversion help!
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;
-
Aug 31st, 2006, 08:39 AM
#7
Re: Simple conversion help!
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.
-
Aug 31st, 2006, 09:11 AM
#8
Re: Simple conversion help!
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.
-
Aug 31st, 2006, 09:16 AM
#9
Thread Starter
Addicted Member
Re: Simple conversion help!
I took your advice, looked at some tutorials but for some reason this
VB Code:
arrColours[10, 2] = new Color(); //Holds an array of different colours (for the text) to display the captcha in.
returns the error
The name 'arrColours' does not exist in the current context
-
Aug 31st, 2006, 09:18 AM
#10
Re: Simple conversion help!
Means exactly what it says. You have to declare all variables before you can use them.
-
Aug 31st, 2006, 05:07 PM
#11
Re: Simple conversion help!
If you're trying to declare a 2D array of Colors on that line it should be:
Code:
Color[,] arrColors = new Color[10, 2];
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|