0

複数の値を追加すると、前に入力されたアイテムが追加されたアイテムの値を取得する辞書があります。.Net3.5を使用しています。コードは次のとおりです。

public static Dictionary<string, Neighborhoods> Families()
    {
        if (File.Exists(calculatePath() + "Family.txt")){}
        else {File.Create(calculatePath() + "Family.txt").Close();}
        string[] inp = File.ReadAllLines(calculatePath() + "Family.txt");
        Neighborhoods temp = new Neighborhoods();
        Dictionary<string, Neighborhoods> All_Families = new Dictionary<string, Neighborhoods>();
        string currentphase = null;
        foreach (string s in inp)
        {
            switch (s)
            {
                case "!<Start Family>!": temp = new Neighborhoods();
                    break;
                case "<Family Name>": currentphase = "<Family Name>";
                    break;
                case "<End Family Name>": currentphase = null;
                    break;
                case "<Neighbour Enabled>True": temp.Neighbourhood_Enabled1 = true;
                    currentphase = "<Neighbour Enabled>True";
                    break;
                case "<Neighbour Enabled>False": temp.Neighbourhood_Enabled1 = false;
                    temp.Neighbourhood_Input1 = null;
                    break;
                case "<University Enabled>True": temp.University_Enabled1 = true;
                    currentphase = "<University Enabled>True";
                    break;
                case "<University Enabled>False": temp.University_Enabled1 = false;
                    temp.University_Input1 = null;
                    currentphase = null;
                    break;
                case "<Downtown Enabled>True": temp.Downtown_Enabled1 = true;
                    currentphase = "<Downtown Enabled>True";
                    break;
                case "<Downtown Enabled>False": temp.Downtown_Enabled1 = false;
                    temp.Downtown_Input1 = null;
                    currentphase = null;
                    break;
                case "!<End Family>!": All_Families.Add(temp.Name, temp);
                    break;
                default: if (currentphase == "<Family Name>") temp.Name = s;
                    if (currentphase == "<Neighbour Enabled>True") temp.Neighbourhood_Input1 = s;
                    if (currentphase == "<University Enabled>True") temp.University_Input1 = s;
                    if (currentphase == "<Downtown Enabled>True") temp.Downtown_Input1 = s;
                    break;
            }
        }
        return All_Families;
    }

新しいキーと値を追加するときに、古いキーが元の値を保持するようにするにはどうすればよいですか?


サンプルデータ:

!<Start Family>!
Family Name>
qwe
<End Family Name>
<Neighbour Enabled>True
qwe
<University Enabled>True
we
<Downtown Enabled>True
qwe
!<End Family>!
!<Start Family>!
<Family Name>
123
<End Family Name>
<Neighbour Enabled>True
123
<University Enabled>True
123
<Downtown Enabled>True
123
!<End Family>!

参考までに、これがnieghbourhoodsクラスです。xmlメソッドを試してみますが、すぐには終了しません。まだこのことを学んでいます。

class Neighborhoods
{
    public Neighborhoods()
    {
        name = "";
        Neighbourhood_Enabled = false;
        Neighbourhood_Input = "";
        University_Enabled = false;
        University_Input = "";
        Downtown_Enabled = false;
        Downtown_Input = "";
    }

    static string name;

    public string Name
    {
        get { return Neighborhoods.name; }
        set { Neighborhoods.name = value; }
    }
    static bool Neighbourhood_Enabled;

    public bool Neighbourhood_Enabled1
    {
        get { return Neighborhoods.Neighbourhood_Enabled; }
        set { Neighborhoods.Neighbourhood_Enabled = value; }
    }
    static string Neighbourhood_Input;

    public string Neighbourhood_Input1
    {
        get { return Neighborhoods.Neighbourhood_Input; }
        set { Neighborhoods.Neighbourhood_Input = value; }
    }
    static bool University_Enabled;

    public bool University_Enabled1
    {
        get { return Neighborhoods.University_Enabled; }
        set { Neighborhoods.University_Enabled = value; }
    }
    static string University_Input;

    public string University_Input1
    {
        get { return Neighborhoods.University_Input; }
        set { Neighborhoods.University_Input = value; }
    }
    static bool Downtown_Enabled;

    public bool Downtown_Enabled1
    {
        get { return Neighborhoods.Downtown_Enabled; }
        set { Neighborhoods.Downtown_Enabled = value; }
    }
    static string Downtown_Input;

    public string Downtown_Input1
    {
        get { return Neighborhoods.Downtown_Input; }
        set { Neighborhoods.Downtown_Input = value; }
    }
}
4

3 に答える 3

2

その外観から、ファイルを解析し、それをディクショナリコレクションにロードします。

いくつかの批評...1つにXMLを使用し、非常識なパーサーを取り除きます。

<?xml version="1.0" encoding="utf-8"?>
<families>
  <family>
    <name>Smith</name>
    <neighborhood>true</neighborhood>
    <university>false</university>
    <downtown>false</downtown>
  </family>
  <family>
    <name>Jones</name>
    <neighborhood>false</neighborhood>
    <university>true</university>
    <downtown>false</downtown>
  </family>
</families>

これで、組み込みのSystem.XML名前空間を使用して、これをはるかに簡単に解析できます。

たとえば、コードを次のように書き直しました。

        Dictionary<String, Neighborhood> families = new Dictionary<string, Neighborhood>();

        XmlDocument doc = new XmlDocument();
        doc.Load("family.xml");

        foreach (XmlNode familyNode in doc.SelectNodes("//family"))
        {
            Neighborhood n = new Neighborhood();
            n.Name = familyNode.SelectSingleNode("name").InnerText;
            n.InNeighborhood = Boolean.Parse(familyNode.SelectSingleNode("neighborhood").InnerText);
            n.InDowntown = Boolean.Parse(familyNode.SelectSingleNode("downtown").InnerText);
            n.InUniversity = Boolean.Parse(familyNode.SelectSingleNode("university").InnerText);

            families.Add(n.Name,n);
        }

簡潔にするためにコードにエラー処理を追加しませんでしたが、問題なく動作します。

于 2008-11-09T20:14:14.547 に答える
2

あなたが与えたサンプルデータとあなたが与えたコードでは、次のNeighborhoodsようなクラスを使用して問題なく動作します:

public class Neighborhoods
{
    public string Name { get; set; }
    public string Neighbourhood_Input1 { get; set; }
    public string University_Input1 { get; set; }
    public string Downtown_Input1 { get; set; }
    public bool Neighbourhood_Enabled1 { get; set; }
    public bool University_Enabled1 { get; set; }
    public bool Downtown_Enabled1 { get; set; }
}

私のテストは、このコードを実行することです:

static void Main()
{
    var families = Families();

    foreach (var family in x.Values)
    {
        Console.WriteLine(y.Name);
    }
}

「qwe」と「123」が表示され、2 つの異なるオブジェクトが関係していることがわかります。

ただし、実際のNeighborhoodsクラスはまだ見ていません。静的フィールドを使用しているとは思いませんが (ただし、インスタンス プロパティを使用しています)、そうですか? それは確かにあなたが見ている行動を説明するでしょう。

編集: うん、これで意味のある Neighborhoods コードが表示されました。これらのフィールドは、タイプ自体だけでなく、各インスタンスに関連することを意図しているため、静的であってはなりません。

これがパーサーとは何の関係もないことを示すために、これを試してください:

Neighborhoods first = new Neighborhoods();
Neighborhoods second = new Neighborhoods();

first.Name = "First";
Console.WriteLine(second.Name);

「First」と表示されますが、これは明らかに望んでいるものではありません。

残念ながら、「静的」が何を意味するかについての適切なページはありませんが、お持ちの C# の書籍で調べることをお勧めします。

于 2008-11-09T20:38:25.777 に答える
0

Jon Skeet が指摘したように、そのような static は使用できません。

static を private に置き換えると、すべてがうまくいくはずです。

于 2008-11-09T20:44:32.143 に答える