Results 1 to 10 of 10

Thread: [2.0] String & Char Conversion [RESOLVED]

  1. #1

    Thread Starter
    Hyperactive Member Rattlerr's Avatar
    Join Date
    Jul 2005
    Location
    FloralCity,Florida
    Posts
    269

    Question [2.0] String & Char Conversion [RESOLVED]

    ok their are 2 lines of code i'm working with that i'm trying to figure out here,i'm using Microsoft.VisualBasic; &Microsoft.VisualBasic.Compatability in the C#....

    VB Code:
    1. B = Strings.InStr(1, A[0], ControlChars.NullChar);
    2.  
    3. B = Strings.InStr(1, A[1], ControlChars.NullChar);

    Those are the 2 Strings i'm getting errors with i will post the Errors after this Section of Code,So you can see the Entire Block that those lines Go into::


    VB Code:
    1. public bool GetStrings(ref string ClientID, ref string ClientPass, ref string Seed, ref string Str1, ref string Str2, ref int mode)
    2.         {
    3.             bool getStringsReturn = false;
    4.             string[] A = new string[0];
    5.             int B;
    6.             try
    7.             {
    8.                 A[0] = new string(ControlChars.NullChar, 100);
    9.                 A[1] = new string(ControlChars.NullChar, 100);
    10.                 getStringsReturn = SM90A_Encoded_Encrypt(ClientID, ClientPass, Seed, A[0], A[1], ref mode);
    11.                 B = Strings.InStr(1, A[0], ControlChars.NullChar);
    12.                 Str1 = Strings.Left(A[0], B - 1);
    13.                 B = Strings.InStr(1, A[1], ControlChars.NullChar);
    14.                 Str2 = Strings.Left(A[1], B - 1);
    15.                 return getStringsReturn;
    16.             }
    17.             catch
    18.             {
    19.                 goto err_Renamed;
    20.             }
    21.             err_Renamed:
    22.             getStringsReturn = false;
    23.             return getStringsReturn;
    24.         }

    Here are the Errors for the 2 Lines of Code at the Top:

    Error 1 The best overloaded method match for 'Microsoft.VisualBasic.Strings.InStr(string, string, Microsoft.VisualBasic.CompareMethod)' has some invalid arguments


    Thxs in Advance Rattlerr
    Last edited by Rattlerr; Aug 8th, 2006 at 09:04 PM.

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

    Re: [2.0] String & Char Conversion

    You should be tied to a tree and shot. Using the Microsoft.VisualBasic namespace in C# is bad enough, but using Microsoft.VisualBasic.Compatibility is inexcusable. That namespace should only be used when it has been added automatically by the upgrade wizard when upgrading a VB6 project to VB.NET. To use Microsoft.VisualBasic.Compatibility even in new VB.NET code should be punishable by death. Get rid of all that abominable code as soon as possible. Use String.IndexOf instead of InStr and String.Substring instead of Left.

    Your issue is because of the fact that C# doesn't understand optional parameters, which are used heavily in VB Runtime functions. In C# you have to provide a value for EVERY argument. Apart from that, you're relying on implicit conversions. InStr takes String arguments, not Char, and you're passing Chars. That's not as bad a crime but you could still get some hard time for it.
    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

  3. #3

    Thread Starter
    Hyperactive Member Rattlerr's Avatar
    Join Date
    Jul 2005
    Location
    FloralCity,Florida
    Posts
    269

    Re: [2.0] String & Char Conversion

    Ok but does ControlChars.NullChar Exist in C#??? cause i need the NullChar property... Also i'm not getting String.IndexOf all Microsoft.VisualBasic References have been Completely Removed...

    VB Code:
    1. public bool GetStrings(ref string ClientID, ref string ClientPass, ref string Seed, ref string Str1, ref string Str2, ref int mode)
    2.         {
    3.            // Cant get String.IndexOf to show up in this Area...
    4.  
    5.  
    6.         }
    Last edited by Rattlerr; Aug 8th, 2006 at 07:33 PM.

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

    Re: [2.0] String & Char Conversion

    ControlChars is Microsoft.VisualBasic.ControlChars. The null character in C# is the same as in C: '\0'.

    String.IndexOf is an instance member, so you call it on an instance of the String class, unlike InStr, to which you pass the instance as a parameter. So, this:
    VB Code:
    1. Dim index As Integer = InStr("Hello World", "o W")
    would become this:
    VB Code:
    1. Dim index As Integer = ("Hello World").IndexOf("o W")
    and it's used the same way in C#.

    By the way, I wouldn't really have shot you.
    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

  5. #5

    Thread Starter
    Hyperactive Member Rattlerr's Avatar
    Join Date
    Jul 2005
    Location
    FloralCity,Florida
    Posts
    269

    Re: [2.0] String & Char Conversion

    lol yeah i know..I thought it was funny when you said that

  6. #6

    Thread Starter
    Hyperactive Member Rattlerr's Avatar
    Join Date
    Jul 2005
    Location
    FloralCity,Florida
    Posts
    269

    Question Re: [2.0] String & Char Conversion

    Quote Originally Posted by jmcilhinney
    ControlChars is Microsoft.VisualBasic.ControlChars. The null character in C# is the same as in C: '\0'.

    String.IndexOf is an instance member, so you call it on an instance of the String class, unlike InStr, to which you pass the instance as a parameter. So, this:
    VB Code:
    1. Dim index As Integer = InStr("Hello World", "o W")
    would become this:
    VB Code:
    1. Dim index As Integer = ("Hello World").IndexOf("o W")
    and it's used the same way in C#.

    By the way, I wouldn't really have shot you.

    Ok,i'm not sure how to Right that into the Example i have Below..
    VB Code:
    1. B = Strings.InStr(1, A[0], ControlChars.NullChar);

    [U]Now for the NullChar part this is what i put from your example of C# Null Character::[/U]
    VB Code:
    1. A[0] = new string('\0', 100);
    2.             A[1] = new string('\0', 100);

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

    Re: [2.0] String & Char Conversion

    So you have a string array and you want to find the index of the first null character in one of the elements, correct? In that case the element is the instance, so you call IndexOf on that:
    Code:
    B = A[0].IndexOf('\0');
    Also, what's with the goto? There is never a need for a goto in properly structured code. Without looking at other changes, this:
    Code:
                try
                {
                    A[0] = new string(ControlChars.NullChar, 100);
                    A[1] = new string(ControlChars.NullChar, 100);
                    getStringsReturn = SM90A_Encoded_Encrypt(ClientID, ClientPass, Seed, A[0], A[1], ref mode);
                    B = Strings.InStr(1, A[0], ControlChars.NullChar);
                    Str1 = Strings.Left(A[0], B - 1);
                    B = Strings.InStr(1, A[1], ControlChars.NullChar);
                    Str2 = Strings.Left(A[1], B - 1);
                    return getStringsReturn;
                }
                catch
                {
                    goto err_Renamed;
                }
                err_Renamed:
                getStringsReturn = false;
                return getStringsReturn;
    should be this:
    Code:
                try
                {
                    A[0] = new string(ControlChars.NullChar, 100);
                    A[1] = new string(ControlChars.NullChar, 100);
                    getStringsReturn = SM90A_Encoded_Encrypt(ClientID, ClientPass, Seed, A[0], A[1], ref mode);
                    B = Strings.InStr(1, A[0], ControlChars.NullChar);
                    Str1 = Strings.Left(A[0], B - 1);
                    B = Strings.InStr(1, A[1], ControlChars.NullChar);
                    Str2 = Strings.Left(A[1], B - 1);
                }
                catch
                {
                    getStringsReturn = false;
                }
    
                return getStringsReturn;
    Now you have a single return statement and no goto.
    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

  8. #8

    Thread Starter
    Hyperactive Member Rattlerr's Avatar
    Join Date
    Jul 2005
    Location
    FloralCity,Florida
    Posts
    269

    Re: [2.0] String & Char Conversion

    I just need you to look at this just for confirmation that i have the .Substring part Correct:

    VB Code:
    1. B = A[0].IndexOf('\0');
    2.                 Str1 = A[0].Substring(B - 1);
    3.                 B = A[1].IndexOf('\0');
    4.                 Str2 = A[1].Substring(B - 1);

    Old Code Below::
    VB Code:
    1. Str2 = Strings.Left(A[0], B - 1);
    2.  
    3. Str2 = Strings.Left(A[1], B - 1);

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

    Re: [2.0] String & Char Conversion

    Code:
    str1 = A[0].Substring(0, A[0].IndexOf('\0'))
    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

  10. #10

    Thread Starter
    Hyperactive Member Rattlerr's Avatar
    Join Date
    Jul 2005
    Location
    FloralCity,Florida
    Posts
    269

    Re: [2.0] String & Char Conversion

    Ok Thxs appreciate your help on this...Really the first time i'v delt with the String and Char like this..

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