CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 2009
    Location
    .NET 3.5/VS 2008
    Posts
    7

    [RESOLVED] Need help porting VB6 code to C#

    Alright i'm trying to get this:

    Code:
    Dim newcom As String, i As Integer, Data As String
        wskData.GetData Data
        WSBuffer = WSBuffer + Data
        While InStr(WSBuffer, vbLf) > 0
        i = InStr(WSBuffer, vbLf)
        newcom = Left(WSBuffer, i - 1)
        WSBuffer = Mid(WSBuffer, i + 1)
        ProcessData newcom
        Wend
    To C# so far this is what i came up with, but its not working lol:

    Code:
    string newcom;
    			int i;
    			WSBuffer = WSBuffer + data;
    			string lf = "\n";
    			while (WSBuffer.Contains(lf)) {
    				i = WSBuffer.IndexOf(lf);
    				newcom = VBLeft(WSBuffer, i - 1);
    				WSBuffer = WSBuffer.Substring(i + 1);
    				HandleProtocol(newcom);
    				curData = newcom;
    				try {
    					this.Invoke(new LogThreadCallback(AppendToLog));
    				} catch {
    					return;
    				}
    			}
    What this is supposed to do is collect all the data given from the server, and look through it on ever instance of a line feed, and process that. The reason i have to do this is because the server doesnt use packet ends as the stop for commands it uses a linefeed so one command can come in over 2 packets or more.

    If anyone knows how to help me that'd be great.

  2. #2
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Need help porting VB6 code to C#

    Quote Originally Posted by briansykes View Post
    Code:
    string newcom;
                int i;
                WSBuffer = WSBuffer + data;
                string lf = "\n";
                while (WSBuffer.Contains(lf)) {
                    i = WSBuffer.IndexOf(lf);
                    newcom = VBLeft(WSBuffer, i - 1);
                    WSBuffer = WSBuffer.Substring(i + 1);
                    HandleProtocol(newcom);
                    curData = newcom;
                    try {
                        this.Invoke(new LogThreadCallback(AppendToLog));
                    } catch {
                        return;
                    }
                }
    I think you at fist need to learn basic language features of C# befoe tying to convert full progams from VB to C#
    Most things are done in another way here.
    Also I think you havn't given the full code of that method,because I cannot see how this code should get the needed data into WSBuffer
    Why are you using a While command ? This would only work as long as you will get LongFeeds if your packages doesn't contain such packages it will neve start to do anything. Any why mixing code?
    VBLeft is unusual in C# simple use Substring beginning with zero and your needed len this is just as you are using Left$ in VB
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  3. #3
    Join Date
    May 2009
    Location
    .NET 3.5/VS 2008
    Posts
    7

    Re: Need help porting VB6 code to C#

    I made a function like Left in VB6 as C# didn't have the equivalent i wanted, as substring passes an error if the len is bigger then the string, so this way it doesn't, and it adds the data to WSBuffer on the OnDataReceive sub when it comes in, like so:

    Code:
    public void OnDataReceived(IAsyncResult asyn) {
    			try {
    				SocketPacket theSockId = (SocketPacket)asyn.AsyncState;
    				int iRx = theSockId.thisSocket.EndReceive(asyn);
    				char[] chars = new char[iRx + 1];
    				System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
    				int charLen = d.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0);
    				System.String szData = new System.String(chars);
    				WSBuffer = WSBuffer + szData;
    				WorkUpIncoming();
    				WaitForData();
    			} catch (ObjectDisposedException ) {
    				curData = "ObjectDisposedException thrown";
    				try {
    					this.Invoke(new LogThreadCallback(AppendToLog));
    				} catch {
    					
    				}
    			} catch (SocketException se) {
    				curData = se.Message;
    				try {
    					this.Invoke(new LogThreadCallback(AppendToLog));
    				} catch {
    					
    				}
    			}
    		}
    Also, if you know of a better way to do this, i'd be more then happy to hear it, because this worked in VB6. Now i'm brand new to .NET in general, but i'm a fast learner, and unless while loops are different in C# it should end the loop until more data comes in ending with a line feed. Also, thank you for helping me realize my problem, it doesn't work when there are no line feeds, so, if you have any suggestions on how to get this to work, i'd be more then grateful, ty in advance.

  4. #4
    Join Date
    May 2007
    Posts
    1,546

    Re: Need help porting VB6 code to C#

    just keep calling Socket.Receive (http://msdn.microsoft.com/en-us/library/8s4y8aff.aspx) in a loop until it returns '0' or throws an exception.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  5. #5
    Join Date
    May 2009
    Location
    .NET 3.5/VS 2008
    Posts
    7

    Re: Need help porting VB6 code to C#

    Wouldn't that be a synchronous method, if i use that in a GUI application, it'll hang completely, unless i can make a thread using that method.

  6. #6
    Join Date
    May 2009
    Location
    .NET 3.5/VS 2008
    Posts
    7

    Re: Need help porting VB6 code to C#

    Alright, i fixed the problem, thanks for the insight, will mark as resolved, once again thanks alot guys

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

Featured