|
-
Nov 13th, 2005, 07:29 PM
#1
[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;
}
-
Nov 13th, 2005, 08:54 PM
#2
Re: C# to VB.Net, help in conversion
There's a link to a code converter in my sig.
-
Nov 13th, 2005, 08:56 PM
#3
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. 
Edit: See what did I tell you.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Nov 13th, 2005, 09:05 PM
#4
Re: C# to VB.Net, help in conversion
What are you trying to say Rob? 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.
Last edited by jmcilhinney; Nov 13th, 2005 at 09:08 PM.
-
Nov 13th, 2005, 09:08 PM
#5
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? C# is next for me to learn.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Nov 13th, 2005, 09:21 PM
#6
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.
-
Nov 13th, 2005, 11:08 PM
#7
Re: C# to VB.Net, help in conversion
 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
-
Nov 13th, 2005, 11:09 PM
#8
Re: C# to VB.Net, help in conversion
-
Nov 13th, 2005, 11:16 PM
#9
Re: C# to VB.Net, help in conversion
I think the /r is a return as in the vb6 CrLf = /r/n.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Nov 13th, 2005, 11:16 PM
#10
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.
-
Nov 13th, 2005, 11:17 PM
#11
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
-
Nov 13th, 2005, 11:18 PM
#12
Re: C# to VB.Net, help in conversion
Looks like I spoke 20 seconds too late 
Bill
-
Nov 13th, 2005, 11:19 PM
#13
Re: C# to VB.Net, help in conversion
Looks like I finally beat John to the post.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Nov 13th, 2005, 11:26 PM
#14
Re: C# to VB.Net, help in conversion
 Originally Posted by RobDog888
Looks like I finally beat John to the post. 
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.
-
Nov 13th, 2005, 11:27 PM
#15
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
-
Nov 13th, 2005, 11:43 PM
#16
Re: C# to VB.Net, help in conversion
 Originally Posted by jmcilhinney
What are you trying to say Rob?  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
-
Nov 13th, 2005, 11:45 PM
#17
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.
-
Nov 13th, 2005, 11:47 PM
#18
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...
-
Nov 13th, 2005, 11:49 PM
#19
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.
-
Nov 13th, 2005, 11:51 PM
#20
Re: C# to VB.Net, help in conversion
 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...
-
Nov 13th, 2005, 11:56 PM
#21
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.
-
Nov 14th, 2005, 12:02 AM
#22
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.
-
Nov 14th, 2005, 03:13 AM
#23
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?
-
Nov 14th, 2005, 03:18 AM
#24
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.
-
Nov 14th, 2005, 03:21 AM
#25
Re: C# to VB.Net, help in conversion
Hmmmnnn... I guess I have to use your solution then...
Resolved for now.
-
Nov 14th, 2005, 03:31 AM
#26
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.
-
Jul 27th, 2006, 04:50 PM
#27
PowerPoster
Re: [RESOLVED] C# to VB.Net, help in conversion
string[] arrFirst = objFile.ReadToEnd().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
-
Jul 27th, 2006, 06:29 PM
#28
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.
-
Jul 27th, 2006, 06:49 PM
#29
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);
-
Jul 27th, 2006, 07:01 PM
#30
PowerPoster
Re: [RESOLVED] C# to VB.Net, help in conversion
 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?
-
Jul 27th, 2006, 08:04 PM
#31
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".
-
Jul 27th, 2006, 08:09 PM
#32
PowerPoster
Re: [RESOLVED] C# to VB.Net, help in conversion
 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 ...
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
|