Get-Set属性返回null

发布于 2025-01-25 10:35:17 字数 1085 浏览 3 评论 0 原文

这是一件非常简单的事情,我尝试了与从表格中发现的相同的事情,几次我将其无效,

public class Variables_Log
    {   
        private int_Type_Parameter_Log;
        public int Type_Parameter_Log
        {
            get
            {
                return _Type_Parameter_Log;
            }
            set
            {
                _Type_Parameter_Log= value;
            }
        }
    }

这是我在表单中使用的部分。

 public partial class Form1 : Form
 {
 Variables_Log variables_log = new Variables_Log();


 public void Write_Parameter_Log()
  {
    public string Log_Raw = "";
    FileStream fs = new FileStream("C:\\Users\\ASUS\\Desktop\\CAS_CW1 -                                     
    ORJINAL\\CAS_CW1\\Log_Parameter.txt", FileMode.Append, 
    FileAccess.Write, FileShare.Write);
    variables_Log.Type_Parameter_Log = "INFO";

    StreamWriter sw = new StreamWriter(fs);

    if (fs != null)
    {
        Log_Raw = (variables_Log.Type_Parameter_Log);
        sw.WriteLine(Log_Raw);
        sw.Close();
    }
 }

}

在这里,我正在更改值,但在其他类中没有更改。 我无法解决问题

It's a very simple thing, I try the same thing as I found from the forms, a few times I get null

public class Variables_Log
    {   
        private int_Type_Parameter_Log;
        public int Type_Parameter_Log
        {
            get
            {
                return _Type_Parameter_Log;
            }
            set
            {
                _Type_Parameter_Log= value;
            }
        }
    }

This is the part I use in the form.

 public partial class Form1 : Form
 {
 Variables_Log variables_log = new Variables_Log();


 public void Write_Parameter_Log()
  {
    public string Log_Raw = "";
    FileStream fs = new FileStream("C:\\Users\\ASUS\\Desktop\\CAS_CW1 -                                     
    ORJINAL\\CAS_CW1\\Log_Parameter.txt", FileMode.Append, 
    FileAccess.Write, FileShare.Write);
    variables_Log.Type_Parameter_Log = "INFO";

    StreamWriter sw = new StreamWriter(fs);

    if (fs != null)
    {
        Log_Raw = (variables_Log.Type_Parameter_Log);
        sw.WriteLine(Log_Raw);
        sw.Close();
    }
 }

}

Here, I am changing the value but not in the other class.
I could not resolve the issue

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

罗罗贝儿 2025-02-01 10:35:17

这是简单的记录器。您可以将此概念用于问题。

public class Logger
{
    private string _path;

    public Logger(string path)
    {
        _path = path;
    }

    public void Log(string row, string lable)
    {
        using (var fs = new FileStream(_path, FileMode.Append))
        {
            StreamWriter sw = new StreamWriter(fs);
            var logString = $"#{lable} : {row}";
            sw.WriteLine(logString);
            sw.Close();
        }
    }

    public void LogInfo(string row)
    {
        Log(row, "Info");
    }

    public void LogError(string row)
    {
        Log(row, "Error");
    }
}

程序类项目的变量位置

internal static class Program
{
    public static Logger Logger;

    [STAThread]
    static void Main()
    {
        Logger = new Logger("C:\\Users\\ASUS\\Desktop\\CAS_CW1-ORJINAL\\CAS_CW1\\Log_Parameter.txt");

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}

现在您可以在任何形式上使用logger

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Program.Logger.LogInfo("Hello World, Logger. I'm in Form 1");
    }
}

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    private void btnForm2Log_Click(object sender, EventArgs e)
    {
        Program.Logger.LogError("Hello World, Logger. I'm in Form 2");
    }
}

Here is simple logger. you can use this concept for your problem.

public class Logger
{
    private string _path;

    public Logger(string path)
    {
        _path = path;
    }

    public void Log(string row, string lable)
    {
        using (var fs = new FileStream(_path, FileMode.Append))
        {
            StreamWriter sw = new StreamWriter(fs);
            var logString = 
quot;#{lable} : {row}";
            sw.WriteLine(logString);
            sw.Close();
        }
    }

    public void LogInfo(string row)
    {
        Log(row, "Info");
    }

    public void LogError(string row)
    {
        Log(row, "Error");
    }
}

Variable Location on Project in Program class

internal static class Program
{
    public static Logger Logger;

    [STAThread]
    static void Main()
    {
        Logger = new Logger("C:\\Users\\ASUS\\Desktop\\CAS_CW1-ORJINAL\\CAS_CW1\\Log_Parameter.txt");

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}

Now you can use logger on any form

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Program.Logger.LogInfo("Hello World, Logger. I'm in Form 1");
    }
}

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    private void btnForm2Log_Click(object sender, EventArgs e)
    {
        Program.Logger.LogError("Hello World, Logger. I'm in Form 2");
    }
}

Github code

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文