Search code examples
c#vb.netcode-conversion

Converting Code Snippet from C# to VB.NET


All the automated, online converters weren't able to convert this code. Unfortunately my brief knowledge of C# has also let me down. The code originates from a blog, linked from another of my questions.

Here is the code snippet in C#;

        var virtualFileDataObject = new VirtualFileDataObject();
        virtualFileDataObject.SetData(new VirtualFileDataObject.FileDescriptor[]
        {
            new VirtualFileDataObject.FileDescriptor
            {
                Name = "abc.txt",
                StreamContents = stream =>
                    {
                        using(var webClient = new WebClient())
                        {
                            var data = webClient.DownloadData("http://www.google.com");
                            stream.Write(data, 0, data.Length);
                        }
                    }
            },
        });

I currently have in VB.NET (removed some of the in-line stuff);

    Dim virtualFileDataObject = New VirtualFileDataObject()
    Dim vf As New VirtualFileDataObject.FileDescriptor()

    vf.Name = "abc.txt"
    vf.StreamContents = ??

    Using webc As New WebClient()
        Dim data = webc.DownloadData("http://www.google.com")
        stream??.Write(data, 0, data.Length)
    End Using

    virtualFileDataObject.SetData(vf)

Your help would be greatly appreciated!


Solution

  • StreamContents is being set with an anonymous method, which VB.NET does not support (but it does in VB.NET 10 which is coming out in .NET 4.0). Next best thing I can suggest is this:

    vf.StreamContents = AddressOf(MyStreamContents)
    
    Public Sub MyStreamContents(ByVal stream As <Whatever the type is>)
    
      Using webc As New WebClient()
            Dim data = webc.DownloadData("http://www.google.com")
            stream.Write(data, 0, data.Length)
        End Using
    
    End Sub