[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:
B = Strings.InStr(1, A[0], ControlChars.NullChar);
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:
public bool GetStrings(ref string ClientID, ref string ClientPass, ref string Seed, ref string Str1, ref string Str2, ref int mode)
{
bool getStringsReturn = false;
string[] A = new string[0];
int B;
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;
}
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 :wave:
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.
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:
public bool GetStrings(ref string ClientID, ref string ClientPass, ref string Seed, ref string Str1, ref string Str2, ref int mode)
{
// Cant get String.IndexOf to show up in this Area...
}
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:
Dim index As Integer = InStr("Hello World", "o W")
would become this:
VB Code:
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. ;)
Re: [2.0] String & Char Conversion
lol yeah i know..I thought it was funny when you said that :D
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:
Dim index As Integer = InStr("Hello World", "o W")
would become this:
VB Code:
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.. :ehh:
VB Code:
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:
A[0] = new string('\0', 100);
A[1] = new string('\0', 100);
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.
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:
B = A[0].IndexOf('\0');
Str1 = A[0].Substring(B - 1);
B = A[1].IndexOf('\0');
Str2 = A[1].Substring(B - 1);
Old Code Below::
VB Code:
Str2 = Strings.Left(A[0], B - 1);
Str2 = Strings.Left(A[1], B - 1);
Re: [2.0] String & Char Conversion
Code:
str1 = A[0].Substring(0, A[0].IndexOf('\0'))
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.. :wave: