Take the 2-minute tour ×

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();
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()
    general general = new general(); 
    viewimagename = general.getimage(); // NULL HERE!!!!!!!!!!!!!!!!!!!!!

   // LoadFromLocalStorage();

I've been thinking and not sure why it became null o.O Can someone help me with this? Thanks i advance! :D

share|improve this question
Each time you call new you're ending up with a new instance of general so the one you are storing to is not the same one you are reading from. It's like putting your cash in a cereal packet, throwing it away and then opening a brand new box of cereal and then wondering where your cash has gone. –  Paul Ruane Jan 30 '12 at 16:38
Consider adopting a consistent naming convention to make your code easier to read. –  Yuck Jan 30 '12 at 16:39
add comment

3 Answers

up vote 1 down vote accepted

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

share|improve this answer
Oh, then how do i not create new and use the existing one :x –  FlyingAtom Jan 30 '12 at 16:50
Create one instance at the class level, not a local variable inside a method. Then refer to that same instance in all cases instead of making a new one. –  recursive Jan 30 '12 at 16:58
Under public partial class View : PhoneApplicationPage? –  FlyingAtom Jan 30 '12 at 17:06
If that's the class your method excerpts are from, then yes. –  recursive Jan 30 '12 at 17:15
add comment

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.

share|improve this answer
HMm okay i think i got it, any way round it? –  FlyingAtom Jan 30 '12 at 16:51
You need to either store your data to disk or some other medium where you can restore it from, or keep the original class instance around –  BrokenGlass Jan 30 '12 at 16:56
Some examples would be nice xD –  FlyingAtom Jan 30 '12 at 17:08
add comment

In the form you want to extract your data from

private static string _first;   
public string First
        return _first;

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

View2 f1 = new View2();
viewimagename = f1.First;
share|improve this answer
add comment

Your Answer


