-
[RESOLVED] C# to VB.Net, help in conversion
Could someone help me out converting this code to VB.Net? Thanks in advance...
Code:
protected bool IsComplete(string reply) {
string[] parts = reply.Replace("\r\n", "\n").Split('\n');
if (parts.Length > 1 && ((parts[parts.Length - 2].Length > 3 && parts[parts.Length - 2].Substring(3, 1).Equals(" ")) || (parts[parts.Length - 2].Length == 3)))
return true;
Else
return false;
}
-
Re: C# to VB.Net, help in conversion
There's a link to a code converter in my sig.
-
Re: C# to VB.Net, help in conversion
parts is a string array and "\r\n" is the equilivalent of Environment.NewLine. The rest is just checking the last 2 characters if it has a carriage return and if the array contains any elements. I'm may not be 100% correct but that should be the jist of it. If I was sure I would convert it for you but maybe this will help someone else to be able to do it. PM John hes usually online as much as me. :D
Edit::lol: See what did I tell you.
-
Re: C# to VB.Net, help in conversion
What are you trying to say Rob? :D Looking at the code more closely, the Replace is redundant because you can use Regex.Split instead:
Code:
string[] parts = reply.Replace("\r\n", "\n").Split('\n');
becomes
VB Code:
Dim parts As String() = System.Text.RegularExpressions.Regex.Split(reply, Environment.NewLine)
Edit:
Note that the Split Runtime function would also do the job, and it may actually be more efficient than either of the other two options. I'd have to test that to see though.
-
Re: C# to VB.Net, help in conversion
That part was confusing me as why would you replace and then split? Is it just to make sure only the "/n" exists after the split? But I was close, huh? :D C# is next for me to learn.
-
Re: C# to VB.Net, help in conversion
String.Split only splits on a single character, while Regex.Split, at its simplest, will split on a substring. Even in C# you could use:
Code:
string[] parts = System.Text.RegularExpressions.Regex.Split(reply, "\r\n");
Obviously the Runtime function option is only available in VB. I say that it may be the most efficient option because Regex.Split has a great deal of power, but in this simple situation that power may actually create additional overhead and be a hindrance.
-
Re: C# to VB.Net, help in conversion
Quote:
Originally Posted by jmcilhinney
There's a link to a code converter in my sig.
I tried it but it here's the result, you think it has been converted correctly?
Code:
Protected Function IsComplete(ByVal reply As String) As Boolean
Dim parts As String() = reply.Replace("" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "", "" & Microsoft.VisualBasic.Chr(10) & "").Split(Microsoft.VisualBasic.Chr(10))
If parts.Length > 1 AndAlso ((parts(parts.Length - 2).Length > 3 AndAlso parts(parts.Length - 2).Substring(3, 1).Equals(" ")) OrElse (parts(parts.Length - 2).Length = 3)) Then
Return True
Else
Return False
End If
End Function
-
Re: C# to VB.Net, help in conversion
Quote:
Originally Posted by RobDog888
parts is a string array and "\r\n" is the equilivalent of Environment.NewLine. The rest is just checking the last 2 characters if it has a carriage return and if the array contains any elements. I'm may not be 100% correct but that should be the jist of it. If I was sure I would convert it for you but maybe this will help someone else to be able to do it. PM John hes usually online as much as me. :D
Edit::lol: See what did I tell you.
If my memory serves me right I think \n stands for NewLine but I am not sure with \r\n... :(
-
Re: C# to VB.Net, help in conversion
I think the /r is a return as in the vb6 CrLf = /r/n.
-
Re: C# to VB.Net, help in conversion
That's faithful to the original. Obviously VB.NET lacks the escape sequences that C-based languages, so that conversion is a little less elegant. Also, the concatenating of the empty strings is a quirk of the converter, but you can easily remove them.
"\r" is a cariage return character and "\n" is a line feed character. The use of both is a bit of a historical anomaly. You can use either on its own to create a line break these days, but officially you should use both together. That's why you have vbCrLf, which is equivalent to "\r\n". Environment.NewLine is the preferred method in VB.NET these days, although you do have several Runtime alternatives, including ControlChars.NewLine, ControlChars.CrLf and the aforementioned vbCrLf.
-
Re: C# to VB.Net, help in conversion
http://blogs.msdn.com/csharpfaq/arch.../12/88415.aspx
the R is CR (asc 13)
So it's basically VBCrLf, which is the same as Environment.Newline
Bill
-
Re: C# to VB.Net, help in conversion
Looks like I spoke 20 seconds too late :)
Bill
-
Re: C# to VB.Net, help in conversion
Looks like I finally beat John to the post. :D
-
Re: C# to VB.Net, help in conversion
Quote:
Originally Posted by RobDog888
Looks like I finally beat John to the post. :D
Damn. You step away to check your appearance in the mirror for one moment and look what happens. I guess I'd better get a monitor with a glossy coating so I can do both at the same time. :D
-
Re: C# to VB.Net, help in conversion
\r\n is used on Windows
\n is used on Unix/Mac
Environment.NewLine returns the correct newline sequence for the platform, so for maximum portability you should use that.
Although, how portable .NET is anyway is a topic for another discussion :D
-
Re: C# to VB.Net, help in conversion
Quote:
Originally Posted by jmcilhinney
What are you trying to say Rob? :D Looking at the code more closely, the Replace is redundant because you can use Regex.Split instead:
Code:
string[] parts = reply.Replace("\r\n", "\n").Split('\n');
becomes
VB Code:
Dim parts As String() = System.Text.RegularExpressions.Regex.Split(reply, Environment.NewLine)
Edit:
Note that the Split Runtime function would also do the job, and it may actually be more efficient than either of the other two options. I'd have to test that to see though.
Forgive me for wanting to ask this, what's the difference between
and
-
Re: C# to VB.Net, help in conversion
There is no difference. You have to put the parentheses after the variable name if you specify an upper bound but if it's just a declaration then you can do either.
-
Re: C# to VB.Net, help in conversion
Thanks, 'String()' seem weird to me that I thought it had other purpose... :)
In C# is it be possible to do...
Code:
string parts[] = reply.Replace("\r\n", "\n").Split('\n');
then? I can't it test right now, I don't have VS.Net here...
-
Re: C# to VB.Net, help in conversion
In C# you have to use string[] and for that reason I'd suggest the As String() notation over Parts() As String in VB.NET.
-
Re: C# to VB.Net, help in conversion
Quote:
Originally Posted by penagate
In C# you have to use string[] and for that reason I'd suggest the As String() notation over Parts() As String in VB.NET.
That's what confused me... :( What's the benefit of using String() then? Coming from VB6.0 it really seems awkward...
-
Re: C# to VB.Net, help in conversion
The C# array notation is due to the type syntax - thing X declares variable X of type "thing" thing[] X declares variable X of type "thing array". If you want you can declare an array of 3 things - thing[] X = new thing[3]. Note that that does not create 3 thing objects - it merely creates an array of 3 elements. You can then instantiate each object separately.
-
Re: C# to VB.Net, help in conversion
I prefer the "Dim parts As String()" form more because to me it seems more logical. I'm declaring a single variable that is of type String array, and that's what that declaration says. I only use the other form when I instantiate the array in the declaration. Some people might criticise the inconsistency but there are situations where both get used by necessity so inconsistency is guaranteed anyway.
-
Re: C# to VB.Net, help in conversion
How about the ff.?
Code:
Dim parts As String() = reply.Replace(ControlChars.Cr + ControlChars.Lf,ControlChars.Lf).Split(ControlChars.Lf)
Compared to the previous conversion which do you think it better?
-
Re: C# to VB.Net, help in conversion
It's definitely better than what you had before, but it's still doing unnecessary work by replacing and then splitting when it can just do the split in the first place. Also, if you are going to go that way, there's no point using ControlChars.Cr + ControlChars.Lf when there is ControlChars.CrLf and ControlChars.NewLine. Note that both those are exactly the same thing but NewLine is a bit more intuitive because it treats them as a unit rather than making any distinction between the two characters.
-
Re: C# to VB.Net, help in conversion
Hmmmnnn... I guess I have to use your solution then... :)
Resolved for now.
-
Re: [RESOLVED] C# to VB.Net, help in conversion
You can use any solution you like, but I'd say that the most efficient and easiest to read would either be the use of Regex.Split or the Split Runtime function.
-
Re: [RESOLVED] C# to VB.Net, help in conversion
string[] arrFirst = objFile.ReadToEnd().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
-
Re: [RESOLVED] C# to VB.Net, help in conversion
I'm sure you're very proud of that but please look at the post dates before replying in future.
-
Re: [RESOLVED] C# to VB.Net, help in conversion
Plus the fact that that overload of Split didn't exist in .NET 1.1, and that code is wrong anyway. It's going to split on every carriage return AND every line feed, then remove the empty entry that it creates between the two each time. It should be:
string[] arrFirst = objFile.ReadToEnd().Split(new string[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries);
-
Re: [RESOLVED] C# to VB.Net, help in conversion
Quote:
Originally Posted by jmcilhinney
Plus the fact that that overload of Split didn't exist in .NET 1.1, and that code is wrong anyway. It's going to split on every carriage return AND every line feed, then remove the empty entry that it creates between the two each time. It should be:
string[] arrFirst = objFile.ReadToEnd().Split(new string[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries);
interesting, the code i posted works, but i cant seem to get yours to work. am i overlooking something?
-
Re: [RESOLVED] C# to VB.Net, help in conversion
If your code works and mine doesn't then that can only mean that your file doesn't contain standard Windows line breaks in the first place. It must only contain '\n' characters for line breaks rather than "\r\n".
-
Re: [RESOLVED] C# to VB.Net, help in conversion
Quote:
Originally Posted by jmcilhinney
If your code works and mine doesn't then that can only mean that your file doesn't contain standard Windows line breaks in the first place. It must only contain '\n' characters for line breaks rather than "\r\n".
no, my file has standard breaks.. i can see the \r\n 's in the string before the split in the watch window ... :confused: