如何将函数中的短局部变量设置为 public

How can I set a short local variable in a function to public

我正在 visual studio 2015 中构建一个 wpf 应用程序,我是 visual studio 和 C# 的新手,运行 遇到了一个小问题。

我有一个函数(如下所示)。在 SelectionChanged 事件中,我获取 ClientList 的值并将其设置为局部变量 client.

 public void ClientList_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        short client = (short)ClientList.SelectedValue;

        foreach (var orgComm in orgComms)
        {
            if(orgComm.OrgId == client)
            {
               CommList.ItemsSource = orgComm.CommunicationTemplateConfigs;
               CommList.SelectedIndex = 0;
            }
        }
    }

我需要这个简短的局部变量对应用程序中的其他函数公开可用,尽管这听起来很简单,但答案似乎总是在回避我。

public short client;

public void ClientList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    client = (short)ClientList.SelectedValue;

    foreach (var orgComm in orgComms)
    {
        if(orgComm.OrgId == client)
        {
           CommList.ItemsSource = orgComm.CommunicationTemplateConfigs;
           CommList.SelectedIndex = 0;
        }
    }
}

客户端变量在 class 期间可用。