gpt4 book ai didi

c# - 为什么我的数据在我没有改变的时候却在改变?

转载 作者:太空宇宙 更新时间:2023-11-03 23:43:46 25 4
gpt4 key购买 nike

<分区>

我正在开发一个 Microsoft Visual C# Form 应用程序,该应用程序将用于创建将进入 RPG 游戏的所有数据。我设计了一个结构并将其全部封装到一个类中,这样我就可以轻松地读取和写入 XML 文件。

在我的主窗体上,我有一个“公共(public)静态”/“全局”变量,我的所有子窗体都可以从中复制信息...操作它需要的...并将该信息发送回它。

例如。我想要多种货币。处理货币的表单仅从全局变量复制货币数据,并且可以自由支配仅操纵该副本。仅当用户单击“应用”或“接受”按钮时,全局变量才应更新以反射(reflect)这些更改。如果单击“取消”按钮,它应该只是关闭表单,并且在处理表单时复制的数据应该被扔到风中。

不幸的是,情况并非如此。每当我更改副本的数据时,它的更改似乎也会反射(reflect)在全局变量上。这里有一个概念我在这里失踪并且不明白。有人请解释。我检查了我的代码,只有那两点数据应该更新。

“主”窗体中的代码

public partial class frmMain : Form
{
public static RPGDataCollection DATA = new RPGDataCollection();
public static string FILE = "";

public frmMain ()
{
InitializeComponent();
FillDefaultData();
}

/// <summary>
/// Sets item info status text.
/// </summary>
/// <param name="text">Text to be displayed.</param>
private void SetItemInfo (string text)
{
lblItemInfo.Text = text;
}

货币形式代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using RPGData.ObjectTypes.DataGroups;

namespace RPGData.EntryForms
{
public partial class frmCurrencies : Form
{
#region Fields

private List<Currency> datCurrencies = new List<Currency>();
private string strEntry = "";
private bool blnGSC = false;
private bool blnUpperFlag = false;
private int intIndex = 0;

#endregion

#region Constructor

public frmCurrencies ()
{
InitializeComponent();
}

#endregion

#region Events

private void frmCurrencies_Load (object sender, EventArgs e)
{
datCurrencies = frmMain.DATA.Currencies;
DisplayData();
}

private void btnReplace_Click (object sender, EventArgs e)
{
intIndex = lstCurrencies.SelectedIndex;
Currency c = datCurrencies[intIndex];

if (txtEntry.Text.Trim().Length > 0)
{
SetValues();

c.Name = strEntry;
c.HandlesGSC = blnGSC;
c.Amount = 0;

datCurrencies[intIndex] = c;
}

ResetFields();
DisplayData();
}

private void btnCancel_Click (object sender, EventArgs e)
{
this.Close();
}

private void btnAdd_Click (object sender, EventArgs e)
{
if (txtEntry.Text.Trim().Length > 0)
{
SetValues();

Currency c = new Currency();
c.Name = strEntry;
c.HandlesGSC = blnGSC;
c.Amount = 0;

datCurrencies.Add(c);
}

ResetFields();
DisplayData();
}

private void btnRemove_Click (object sender, EventArgs e)
{
intIndex = lstCurrencies.SelectedIndex;

if (intIndex >= 0)
datCurrencies.RemoveAt(intIndex);

ResetFields();
DisplayData();
}

private void btnApply_Click (object sender, EventArgs e)
{
frmMain.DATA.Currencies = datCurrencies;
}

private void btnAccept_Click (object sender, EventArgs e)
{
frmMain.DATA.Currencies = datCurrencies;
this.Close();
}

private void lstCurrencies_SelectedIndexChanged (object sender, EventArgs e)
{
intIndex = lstCurrencies.SelectedIndex;
Currency c = datCurrencies[intIndex];

txtEntry.Text = c.Name;
chkGSC.Checked = c.HandlesGSC;
}

#endregion

#region Methods

private void DisplayData ()
{
lstCurrencies.Items.Clear();

for (int i = 0; i < datCurrencies.Count; i++)
{
string gsc = "";
if (datCurrencies[i].HandlesGSC)
gsc = "*";
else
gsc = " ";

lstCurrencies.Items.Add("[ " + gsc + " ] " + datCurrencies[i].Name);
}
}

private void ResetFields ()
{
strEntry = "";
blnGSC = false;

txtEntry.Text = strEntry;
chkGSC.Checked = blnGSC;

txtEntry.Focus();
}

private void SetValues ()
{
string entry = ToAllUpper(txtEntry.Text);

strEntry = entry;
blnGSC = chkGSC.Checked;
}

private string ToAllUpper (string str)
{
string entry = "";

for (int i = 0; i < str.Length; i++)
{
string c = "";

if (i == 0)
c = str.Substring(0, 1).ToUpper();

else if (str.Substring(i, 1) == " ")
{
c = " ";
blnUpperFlag = true;
}

else if (blnUpperFlag)
{
c = str.Substring(i, 1).ToUpper();
blnUpperFlag = false;
}
else
c = str.Substring(i, 1);

entry += c;
}

return entry;
}

#endregion
}
}

如果你能给我任何帮助,帮助我理解可能发生的事情,那就太好了(或者你看到了我没有看到的错误或错误)。

谢谢!

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com