字段初始化程序无法引用非静态字段:正在尝试创建构造函数以修复问题

本文关键字:字段 创建 构造函数 问题 程序 初始化 引用 静态 | 更新日期: 2023-09-27 18:21:20

我创建了一个生成随机状态的类。这是有效的。接下来,使用我在网上找到的代码样本,我尝试使用Dictionary方法,该方法(1)匹配与随机选择的州对应的有效城市,(2)匹配与相同随机选择的状态对应的有效邮政编码。

以下是迄今为止的代码示例

  public class GenerateCityStateZip
    {
        public static string state;
        public static string city;
        public static string zip;
        public static Random rnd = new Random();
        public static string GenRandomState()
        {
            List<string> lst = new List<string>();
            state = string.Empty;
            lst.Add("Alabama");
            lst.Add("Alaska");
            lst.Add("Arizona");
            ...
            state = lst.OrderBy(xx => rnd.Next()).First();
            return state;
        } // End GenRandomState
       public static Dictionary<string, string> cities = new Dictionary<string, string>
        {
            {"Alabama", "Huntsville"},
            {"Alaska", "Anchorage"},
            {"Arizona", "Phoenix"},
...
};
       public static string GetCity(string city)
        {
            // Try to get the result in the static Dictionary
            string result;
            if (cities.TryGetValue(city, out result))
            {
                return result;
            }
            else
            {
                return null;
            }
        } // End GetCity
        public static Dictionary<string, string> zipcodes = new Dictionary<string, string>
        {
            {"Alabama", "35801"},
            {"Alaska", "99501"},
            {"Arizona", "85001"},
...
};
        public static string GetZip(string zip)
        {
            // Try to get the result in the static Dictionary
            string result;
            if (zipcodes.TryGetValue(zip, out result))
            {
                return result;
            }
            else
            {
                return null;
            }
        } // End GetZip
}

接下来,我有一个类,它为state city和zip变量调用GenerateCityStateZip类。状态变量已成功生成,但当我尝试创建city和zip时,我收到一条消息,说"字段初始化程序无法引用非静态字段"。

我知道我需要在这个类中创建一个构造函数,但我不确定如何正确实现。下面的代码。

   // My attempt to create a constructor
    GenerateCityStateZip g = new GenerateCityStateZip();
    string state = GenerateCityStateZip.GenRandomState();

    string city = GenerateCityStateZip.GetCity(state);
    string zip = GenerateCityStateZip.GetZip(state);

提前感谢

字段初始化程序无法引用非静态字段:正在尝试创建构造函数以修复问题

根据Darren的建议,我创建了调用GenerateCityStateZip类的公共静态方法。

public static string getState()
        {
            string state = GenerateCityStateZip.GenRandomState();
            return state;
        }
        public string _state = getState();
  public static string getCity(string state)
    {
        string city = GenerateCityStateZip.GetCity(state);
        return city;
    }
    public static string getZip(string state)
    {
        string zip = GenerateCityStateZip.GetZip(state);
        return zip;
    }

接下来,我从辅助类(而不是GenerateCityStateZip类)内部的另一个方法调用

public void InputNameZipClickNext()
{
    txtClientName.Click();
    txtClientName.SendKeys(bogusCompanyName);
    txtZipCode.Click();
    txtZipCode.SendKeys(getZip(_state));
    txtInsuredName.Clear();
    txtInsuredName.SendKeys(clientCompanyName);
    txtAddressPhysical.Clear();
    txtAddressPhysical.SendKeys(clientAddress);
    txtCityPhysical.Clear();
    txtCityPhysical.SendKeys(getCity(_state));
    txtStatePhysical.SendKeys(_state);
    btnCopyPhysical.Click();
    btnCreateNext.Click();
}

再次感谢Darren