views:

59

answers:

1

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!

A: 

Hi there.

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

Cheers. Jas.

Jason Evans
Cool, now Action(of ) is making sense -- a delegate.How would I pass a URL parameter to MyStreamContents in-place of the hard coded Google URL?
Shadow
You can use variables declared outside of the Sub, such as a field or global variable.
Jason Evans