Как исправить ошибку в работе с переменной? С#

using System.Windows;

namespace WpfApp2
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            double width = SystemParameters.PrimaryScreenWidth / SystemParameters.CaretWidth;
            double height = SystemParameters.PrimaryScreenHeight / SystemParameters.CaretWidth;
            int WidthConvert = (int)width;
            int HeightConvert = (int)height;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show($"{WidthConvert}");
        }
    }
}

Ошибка в том что WidthConvert не существует в текущем контексте


Ответы (1 шт):

Автор решения: Александр Муксимов

Может Вам нужно так

using System.Windows;

namespace WpfApp2
{
    public partial class MainWindow : Window
    {
        int WidthConvert;  //Переносим сюда

        public MainWindow()
        {
            InitializeComponent();
            double width = SystemParameters.PrimaryScreenWidth / SystemParameters.CaretWidth;
            double height = SystemParameters.PrimaryScreenHeight / SystemParameters.CaretWidth;

            WidthConvert = (int)width;  //Здесь убираем декларацию
            int HeightConvert = (int)height;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show($"{WidthConvert}");
        }
    }
}

:)

→ Ссылка