We help IT Professionals succeed at work.

Convert C# to VB

tonelm54
tonelm54 asked
on
Ive been trying to convert some c# code from 'http://www.c-sharpcorner.com/UploadFile/nipuntomar/FileUploadsilverlight03182009030537AM/FileUploadsilverlight.aspx' however having issues with the section:-
            c.OpenWriteCompleted += (sender, e) =>
            {
                PushData(data, e.Result);
                e.Result.Close();
                data.Close();
            };

My latest attempt was to run it through a code conversion tool at http://www.developerfusion.com/tools/convert/csharp-to-vb/, but still produces errors most of which I fixed, but still have the bit above.

I have tried using the raiseevent code, but it just tells me end of statment expected :-(

Any ideas?

Imports System.IO
Imports System
Imports System.Net

Partial Public Class MainPage
    Inherits UserControl

    Public Sub New()
        InitializeComponent()
    End Sub
    Private Sub UploadFile(ByVal fileName As String, ByVal data As Stream)
        Dim ub As New UriBuilder("http://localhost:3840/receiver.ashx")
        ub.Query = String.Format("filename={0}", fileName)
        Dim c As New WebClient
 	c.OpenWriteCompleted += Function(sender, e) Do
        PushData(data, e.Result)
        e.Result.Close()
        data.Close()
    End Sub
	c.OpenWriteAsync(ub.Uri)
End Sub

    Private Sub PushData(ByVal input As Stream, ByVal output As Stream)
        Dim buffer As Byte() = New Byte(4095) {}
        Dim bytesRead As Integer

        While (InlineAssignHelper(bytesRead, input.Read(buffer, 0, buffer.Length))) <> 0
            output.Write(buffer, 0, bytesRead)
        End While
    End Sub
End Class

Open in new window

Comment
Watch Question

Hi

Please see the solution below. Hope this helps

Imports System.IO
Imports System
Imports System.Net
 
Partial Public Class MainPage
    Inherits UserControl
 
    Public Function New() As Sub
    End Function

        Dim bytesRead As Integer
 
        While (InlineAssignHelper(bytesRead, input.Read(buffer, 0, buffer.Length))) <> 0
            output.Write(buffer, 0, bytesRead)
        End While
    End Sub
End Class

Thanks

Andy
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
Use AddHandler() and a separate method:
    Private Sub UploadFile(ByVal fileName As String, ByVal data As Stream)
        Dim ub As New UriBuilder("http://localhost:3840/receiver.ashx")
        ub.Query = String.Format("filename={0}", fileName)
        Dim c As New WebClient
 	AddHandler c.OpenWriteCompleted, AddressOf MyOpenWriteCompletedEventHandler
	c.OpenWriteAsync(ub.Uri)
    End Sub

    Public Sub MyOpenWriteCompletedEventHandler(ByVal sender As Object, ByVal e As OpenWriteCompletedEventArgs)
        PushData(data, e.Result)
        e.Result.Close()
        data.Close()
    End Sub

Open in new window

Kusala WijayasenaSoftware Engineer

Commented:
Hi tonelm54,

If you look at line 19 : "c.OpenWriteCompleted += Function(sender, e) Do", it means that you're assigning a callback function (remember, FUNCTION not SUB) to "OpenWriteCompleted"

So do following correction

c.OpenWriteCompleted += Function(sender, e) Do
   PushData(data, e.Result)
   e.Result.Close()
   data.Close()
End Sub <------ Change this to "End Function"


-Kusala
Kusala WijayasenaSoftware Engineer

Commented:
Not line 19, it should be corrected as line 15 :) [but somehow error is in line 19]

-Kusala
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
If you want to do "inline" then I think it should be:
        Dim c As New WebClient
        AddHandler c.OpenWriteCompleted, Sub(sender, e)
                                             PushData(data, e.Result)
                                             e.Result.Close()
                                             data.Close()
                                         End Sub

Open in new window

Author

Commented:
Hi Kusala, I tried changing 'End Sub' to 'End Function', and it just changing it to End sub as soon as I change line
        PushData(data, e.Result)
        e.Result.Close()
        data.Close()
    End Sub
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
I don't see why it should be a function...it's not returning anything.

Try my last comment:
http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_26255917.html#32979990
Kusala WijayasenaSoftware Engineer

Commented:
Hmmm, did little googling and found out that you are not the first one who come across this matter. Have a look at this

http://forums.silverlight.net/forums/p/85549/199424.aspx

-Kusala
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
You can only use the inline/anonymous approach if you have VB.Net 2010.  For earlier versions you'll have to separate it out into a separate method as in my first post here:
http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_26255917.html#32979490

For VB 2010, here are some more examples to look at:
http://msdn.microsoft.com/en-us/magazine/ee336123.aspx
Shahan AyyubSenior Software Engineer

Commented:
Hi! I have converted the piece of code provided in the question via Converter and found this:
This is the link for conversion of your whole project:
http://www.tangiblesoftwaresolutions.com/Product_Details/Instant_CSharp.html


C#:
        static void Main(string[] args)
        {
             c.OpenWriteCompleted += (sender, e) =>
            {
                 PushData(data, e.Result);
                e.Result.Close();
                 data.Close();
            };
        }

VB:

        Shared Sub Main(ByVal args() As String)
            AddHandler c.OpenWriteCompleted, Function(sender, e) AnonymousMethod1(sender, e)
        End Sub
       
        Private Shared Function AnonymousMethod1(ByVal sender As Object, ByVal e As Object) As Object
            PushData(data, e.Result)
            e.Result.Close()
            data.Close()
            Return Nothing
        End Function

Hope this can provide help.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
That's really no different than what I already posted back here:
http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_26255917.html#32979490

It still requires an external method and all it does is replace "AddressOf" with "Function".

*And YOU have made it a function by returning Nothing?  Make it a Sub!....
Shahan AyyubSenior Software Engineer

Commented:
The reason for the last post is not to copy your post and made some changes and post again. I provide a link that can help to convert the code which tonelm54 was not able to convert from:
http://www.developerfusion.com/tools/convert/csharp-to-vb/

So, I want tonelm54 to try that link to convert code from C# to VB.