-1

I am coding a wpf application that may have a few pages for the user enter information in the text box or ticking of the check boxes provided. I will need to store all of these data and print it out after the user is done with answering all the questions required.

Whats the best approach to go about doing this? Do i create a class file to store the data, create a global array to store these items or is there a better way? Thanks.

Bongchi
  • 37
  • 1
  • 8
  • Possible duplicate of [Session or Global Variables c# wpf](https://stackoverflow.com/questions/46064380/session-or-global-variables-c-sharp-wpf) – FLICKER Oct 04 '19 at 17:54
  • What happens when you close the app down? If you need to store across sessions or this is a lot of data then serialise to disk. If not then use dependency injection and a singleton class. Perhaps with properties which are complex objects. – Andy Oct 05 '19 at 00:06
  • @Andy The stored users data will probably only be temporary as i may just add an option to print the displayed output or be saved as a file – Bongchi Oct 05 '19 at 02:47
  • If you're sharing across layers and views then i'd prefer dependency injection myself. You can easily mock for any automated tests and the classes clearly indicate their dependencies because they're obvious on the ctor. If they are huge then use a mediator injected so you can have more control over disposing them. A mediator itself being a sort of container. – Andy Oct 05 '19 at 09:16

2 Answers2

0

The easiest way I find in storing Global variables is using application's settings, follow these steps:

  1. access the application setting's menu. open your WPF project and go to "Project\[your application's name] Properties..." and then click on "Settings" tab.

  2. Create a setting and name it "GlobalText", make sure it is set to a string type and on user scope and keep the value empty, you may close the application setting's window now, make sure to save the work.

  3. Now to assign a value to the setting you've just created just use this code:

    Properties.Settings.Default.GlobalText = TxtBox1.Text;
    
  4. Now to read the value you've stored use this code:

    TxtBox1.Text = Properties.Settings.Default.GlobalText;
    
  5. If you wish to save the values use this code:

    Properties.Settings.Default.Save();
    
Dark Templar
  • 1,130
  • 8
  • 10
0

Create a class to hold the user information and pass it to each view (or preferably viewmodel if you're using MVVM) via constructor injection. This is probably the simplest approach.

Depending how simple your pages are you could probably use the same ViewModel for all the pages which would make it even simpler.

Jack
  • 886
  • 7
  • 27