Getting the string value from a void on a class does not output correctly

Viewed 74

I got a class that I'm hooking later as an ItemsSource on a Listbox. This Listbox got an Item which got an Image Binding "{Binding LocalImage}". The issue is that when the Item is getting the value from LocalImage, is returning a null value when it shouldn't. The void doesImageExistLocally works perfectly and has been tested on another project.

Calling it alone from the Main void does correctly output the path of the image, so its not an issue on the void, is an issue with the class / listbox item getting the value. Also need to note that the string Image is assigned correctly too. Console.WriteLine(Logic.doesImageExistLocally("imageurl"));

public class Test {

    private string _LocalImage;

    public string Image {get; set:}
    public string LocalImage
    {
        get { return _LocalImage; }
        set { _LocalImage = doesImageExistLocally(Image); }
     }
}

Edit:

Test test = new Test();
test.Image = "URL";
doesImageExistLocally(Image); // output = Images/name-of-picture.jpg
Console.WriteLine(test.LocalImage); // output = null;

Void code:

public static string doesImageExistLocally(string imageURL)
    {
        Uri uri = new Uri(imageURL);

        string path = "Images/" + System.IO.Path.GetFileName(uri.LocalPath);
        Console.WriteLine(path);
        if (!File.Exists(path))
        {
            using (WebClient client = new WebClient())
            {
                client.DownloadFile(new Uri(imageURL), path);
            }
        }
        return path;

    }
2 Answers

In the Test class, LocalImage is never set . use this code :

public class Test {

    private string _LocalImage;
    private string _Image;
    public string Image
    {
        get
        {
            return _Image;
        }
        set
        {
            _Image = value;
            _LocalImage = doesImageExistLocally(value);
        }
    }
    public string LocalImage
    {
         get { return _LocalImage; }
    }
 }

I think if you modify your code like this, it will be correct.

Test test = new Test();
test.Image = "URL";
test.LocalImage = "URL"; // output = Images/name-of-picture.jpg
Console.WriteLine(test.LocalImage); // output = null;

The issue is that you are never actively set LocalImage even thus you calling a method inside the property. To prove this set a debugger marker on set line and you will never hit it.

Honestly, I am not the fan of setting property like the way you do it. IMO you can do it two ways, one by setting it in through Constructor OR by setting it after class instantiation. This way you gain better control on your property. If you absolutely want to set it after setting Image property, you can follow the other answer.

By Constructor example:

public Test()
{
    LocalImage = DoesImageExistLocally(Image);
}

public string Image { get; set; }
public string LocalImage { get; set; }

Or after class instantiation example:

test.LocalImage = DoesImageExistLocally(test.Image);

Now when you call test.LocalImage you get the expected result "Images/name-of-picture.jpg"

Enjoy.

Related