title: In a basic WPF project, how can I avoid making a wide scope class initialization?
body: <p>This is a very basic question, and I'm certain the reason I haven't found an answer is because I'm not 100% positive how to google it.</p>
  
  <p>I have a very simple project, where I want to use a class that contains my info.</p>
  
  <pre><code>public class MyInfo
  {
      public string name = String.Empty;
      public string ssn = String.Empty;
      public string dob = String.Empty;
  }
  </code></pre>
  
  <p>very basic.</p>
  
  <p>I have a xaml file with buttons, and I just want to set the value of the variables in my class, to the contents in my buttons.  Here's the xaml.</p>
  
  <p><strong>MainWindow.xaml</strong></p>
  
  <pre><code>&lt;Window x:Class="WpfApplication1.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          Title="MainWindow" Height="350" Width="525"&gt;
      &lt;Grid&gt;
          &lt;Button Name="name" Content="John Doe" HorizontalAlignment="Left" Margin="117,60,0,0" VerticalAlignment="Top" Width="75" Click="name_click"/&gt;
          &lt;Button Name="ssn" Content="222-22-2222" HorizontalAlignment="Left" Margin="117,120,0,0" VerticalAlignment="Top" Width="75" Click="ssn_click"/&gt;
          &lt;Button Name="dob" Content="June 1st 1992" HorizontalAlignment="Left" Margin="117,178,0,0" VerticalAlignment="Top" Width="75" Click="dob_click"/&gt;
  
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>The problem I'm having, is that the only way I know how to do this (being a total C# noob) is by having my class initialization in a super wide scope.  </p>
  
  <p>For example, here's my cs file with comments on what I initially did, but not what I think should be done.</p>
  
  <p><strong>MainWindow.xaml.cs</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Data;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Imaging;
  using System.Windows.Navigation;
  using System.Windows.Shapes;
  
  namespace WpfApplication1
  {
  
      public class MyInfo
      {
          public string name = String.Empty;
          public string ssn = String.Empty;
          public string dob = String.Empty;
      }
  
      //  If i create a class instance here like this
      //MyInfo  infoTest = new MyInfo();
      //  then I can just set the values in the click functions like 
      //infoTest.dob = dob.Content.ToString();
      public partial class MainWindow : Window
      {
          public MainWindow()
          {
              InitializeComponent();
  
          }
  
          private void name_click(object sender, RoutedEventArgs e)
          {
              //save name.content into MyInfo.name
          }
  
          private void ssn_click(object sender, RoutedEventArgs e)
          {
              //save ssn.content into MyInfo.ssn
          }
  
          private void dob_click(object sender, RoutedEventArgs e)
          {
              //save dob.Content into MyInfo.dob
          }
      }
  }
  </code></pre>
  
  <p>I would think it would be better to create an instance of my class in the MainWindow class, after <code>InitializeComponent();</code>, but then I'm not certain how to get my values into the class object with a closed scope.  </p>