-1

I'm using WebClient to download images over my code is very large, so I'm trying to separate my code into classes. Can someone give me an example of how to put the code below in a class or function?

WebClient client = new WebClient ();
client.DownloadDataCompleted += 
    (object sender, DownloadDataCompletedEventArgs e) => 
    {
        byte[] result = e.Result;
        if (result != null) 
        {
            NSData data1 = NSData.FromArray (e.Result);
            UIImage img = UIImage.LoadFromData (data1);
            InvokeOnMainThread (delegate {
                avatar.Image = img;     
            });             
        }
   };

client.DownloadDataAsync(new Uri(
    "http://xx.xx.xx.xx/fbcache/" + 
    list[indexPath.Row].comentario_id_usuario + 
    ".jpg"));
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
pattrick
  • 41
  • 5

1 Answers1

-1

I think you might be confused about what a class is and what purpose it serves. As far as putting that code into a function, try below to get started (though I suspect you will need to do something about the async call you are making to get the results you want):

public WebClient MyFunctionName()
{
        #region Baixando as imagens e as exibindo
        WebClient client = new WebClient ();
        client.DownloadDataCompleted += (object sender, DownloadDataCompletedEventArgs e) => {
            byte[] result = e.Result;
            if (result != null) {
                NSData data1 = NSData.FromArray (e.Result);
                UIImage img = UIImage.LoadFromData (data1);
                InvokeOnMainThread (delegate {
                    avatar.Image = img;


                });

            }
        };
        client.DownloadDataAsync (new Uri ("http://xx.xx.xx.xx/fbcache/"+list [indexPath.Row].comentario_id_usuario+".jpg"));
        #endregion
        return client;
}
Abe Miessler
  • 82,532
  • 99
  • 305
  • 486
  • Yes, it's true. My intention is to put this function or class in a separate file – pattrick Mar 08 '13 at 23:40
  • This is a function. A class can contain a function, but they are very different concepts. I'm trying to find a good explanation to direct you to. – Abe Miessler Mar 08 '13 at 23:41
  • This is a decent concise explanation: http://stackoverflow.com/questions/1093968/what-is-a-class-and-object-in-c – Abe Miessler Mar 08 '13 at 23:42
  • @JohnSaunders, true. In my defense I was answering his question of `how to put the code below in a...function` and not attempting to refactor his code. – Abe Miessler Mar 09 '13 at 00:04
  • Abe, I get it, but sometimes it's better not to answer the smaller questions when the answers leave larger questions behind, if you get my meaning. – John Saunders Mar 09 '13 at 00:08