0

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!

Community
  • 1
  • 1
Jake Edwards
  • 1,190
  • 11
  • 24

1 Answers1

0

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
Jason Evans
  • 28,906
  • 14
  • 90
  • 154
  • 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? – Jake Edwards Mar 07 '10 at 11:59
  • You can use variables declared outside of the Sub, such as a field or global variable. – Jason Evans Mar 07 '10 at 13:01