Storing a string in a variable in a class file

this is my class file that contains the variables that i need to save.

public class general { String imagename2; String name; public string getimage() { return imagename2; } public void viewimage(String imagename){ imagename2 = imagename; } } 

First I store it in a 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 gets redirected to another page, when I get its value is null instead of value

 public View() { InitializeComponent(); general general = new general(); viewimagename = general.getimage(); // NULL HERE!!!!!!!!!!!!!!!!!!!!! this.ReadFromIsolatedStorage(viewimagename+".jpg"); // LoadFromLocalStorage(); } 

I thought and didn’t know why it became zero oO Can someone help me with this? Thanks, I'm in advance !: D

+4
source share
3 answers

Each time a new instance of the general class is created, so you get a new, brilliant, empty set of field values.

+1
source

I think you misunderstood how classes and class instances, OOP as a whole, work:

You set the value of the field in one specific instance of the general class - this field will be set only for this instance. When you create a new instance of the class, it is a completely separate, different instance, so the field will have a default value that is null for the row.

+2
source

Form1
In the form you want to extract from

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

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

 View2 f1 = new View2(); viewimagename = f1.First; 
+2
source

Source: https://habr.com/ru/post/1393800/


All Articles