【问题标题】:How can I set a short local variable in a function to public如何将函数中的短局部变量设置为公共
【发布时间】:2017-05-04 08:51:58
【问题描述】:

我正在 Visual Studios 2015 中构建一个 wpf 应用程序,我是 Visual Studios 和 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;
            }
        }
    }

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

【问题讨论】:

  • 在方法外创建公共属性
  • 为什么这个问题用 wpf 标记?

标签: c# wpf variables local short


【解决方案1】:
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;
        }
    }
}

客户端变量在整个类中都可用。

【讨论】:

  • 嗨拉维。我在这里注意到您建议的编辑之一:stackoverflow.com/review/suggested-edits/17899412 - 一切看起来都不错 - 但请不要在帖子中添加“感谢”。事实上,如果您看到它包含在帖子中,您应该将其删除。否则,编辑看起来不错;坚持下去
猜你喜欢
  • 2012-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-16
  • 2010-12-31
  • 2020-12-04
相关资源
最近更新 更多