JohnDotOwl
JohnDotOwl

Reputation: 3755

Storing String in a variable in a class file

This is my class file which contains variables which I need to store.

public class general
{
    String imagename2;
    String name;
    public string getimage()
    {
        return imagename2;
    }

    public void viewimage(String imagename){
        imagename2 = imagename;
    }
}

I firstly store it to the class file

selected = lbFiles.SelectedItem.ToString();
general item = new general();
item.viewimage(selected);
MessageBox.Show(selected);
NavigationService.Navigate(new Uri("/View.xaml", UriKind.Relative));

And by the time it redirect to another page, when I retrieve, its null instead of the value

public View()
{
    InitializeComponent();
    general general = new general(); 
    viewimagename = general.getimage(); // NULL HERE!!!!!!!!!!!!!!!!!!!!!

    this.ReadFromIsolatedStorage(viewimagename+".jpg");
   // LoadFromLocalStorage();
}

I've been thinking and not sure why it became null.

Upvotes: 4

Views: 4889

Answers (3)

JohnDotOwl
JohnDotOwl

Reputation: 3755

Form1
In the form you want to extract your data from

private static string _first;   
public string First
{
    get
    {
        return _first;
    }
}

Form 2
In the form you want to display your data from Form 1

View2 f1 = new View2();
viewimagename = f1.First;

Upvotes: 2

Ondrej Tucny
Ondrej Tucny

Reputation: 27962

You are creating a new instance of the general class each time, hence you get a new, shiny, blank set of field values.

Upvotes: 1

BrokenGlass
BrokenGlass

Reputation: 160852

I think you misunderstood how classes and instances of classes work, OOP in general:

You are setting the value of a field in one particular instance of the general class - this field will only be set for that instance. When you create a new instance of the class, this is a completely separate, different instance - so the field will have its default value, which is null for a string.

Upvotes: 2

Related Questions