Results 1 to 32 of 32

Thread: [RESOLVED] C# to VB.Net, help in conversion

  1. #1

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Resolved [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;
        }
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

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

    Re: C# to VB.Net, help in conversion

    There's a link to a code converter in my sig.
    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
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

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

    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:
    1. 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.
    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
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

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

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

  7. #7

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    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
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  8. #8

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    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.


    Edit: 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...
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

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

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

  11. #11
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    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
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  12. #12
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: C# to VB.Net, help in conversion

    Looks like I spoke 20 seconds too late

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  13. #13
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

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

    Re: C# to VB.Net, help in conversion

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

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

    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

  16. #16

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: C# to VB.Net, help in conversion

    Quote 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:
    1. 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

    VB Code:
    1. Dim parts As String()

    and

    VB Code:
    1. Dim parts() As String
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

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

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

  18. #18

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

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


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

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

    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.

  20. #20

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

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


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

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

    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.

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

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

  23. #23

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    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?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

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

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

  25. #25

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: C# to VB.Net, help in conversion

    Hmmmnnn... I guess I have to use your solution then...

    Resolved for now.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

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

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

  27. #27
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046

    Re: [RESOLVED] C# to VB.Net, help in conversion

    string[] arrFirst = objFile.ReadToEnd().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

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

    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.

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

    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);
    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

  30. #30
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046

    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?

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

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

  32. #32
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046

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

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