Results 1 to 11 of 11

Thread: Simple conversion help!

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Posts
    135

    Simple conversion help!

    I am trying to covert a line of code I have in ASP .Net VB:
    VB Code:
    1. strRandomText &= Mid(strCharacters, Int(Len(strCharacters) * Rnd()) + 1, 1)

    How would I do this in C#?

  2. #2
    Hyperactive Member The_Duck's Avatar
    Join Date
    May 2005
    Location
    Leamington, UK
    Posts
    351

    Re: Simple conversion help!

    Give this a whirl

    strRandomText += Mid(strCharacters, Int(Len(strCharacters) * Rnd()) + 1, 1);

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Posts
    135

    Re: Simple conversion help!

    Hi thanks for that.

    I have another slight problem if you can help me....

    VB Code:
    1. 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

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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 ||.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Posts
    135

    Re: Simple conversion help!

    Thanks that great.... and one last thing (hopefully) can someone check to see if this is correct
    VB Code:
    1. arrColours[10, 2] = new Color();                  //Holds an array of different colours (for the text) to display the captcha in.
    2.         arrPen[10] = new Pen();                           //Holds an array of different colours (for the lines) to display the captcha in.
    3.  
    4.         intArrIndex = intArrRnd * 4;
    5.        
    6.         //******************
    7.         //** Colour Array **
    8.         //******************
    9.         //Gray
    10.         arrColours(0, 0) = Color.Gray;
    11.         arrColours(0, 1) = Color.DarkGray;
    12.         arrPen(0) = Pens.LightGray;
    13.        
    14.         //Green
    15.         arrColours(1, 0) = Color.Green;
    16.         arrColours(1, 1) = Color.DarkGreen;
    17.         arrPen(1) = Pens.LightGreen;
    18.        
    19.         //Blue
    20.         arrColours(2, 0) = Color.Blue;
    21.         arrColours(2, 1) = Color.DarkBlue;
    22.         arrPen(2) = Pens.LightBlue;
    23.  
    24.         //Red
    25.         arrColours(3, 0) = Color.Red;
    26.         arrColours(3, 1) = Color.DarkRed;
    27.         arrPen(3) = Pens.LightPink;
    28.  
    29.         //Brown
    30.         arrColours(4, 0) = Color.Brown;
    31.         arrColours(4, 1) = Color.Black;
    32.         arrPen(4) = Pens.Maroon;

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Posts
    135

    Re: Simple conversion help!

    I took your advice, looked at some tutorials but for some reason this
    VB Code:
    1. 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

  10. #10
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Simple conversion help!

    Means exactly what it says. You have to declare all variables before you can use them.

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width