'GET-SET property return null
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
Solution 1:[1]
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 = $"#{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");
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | thisisnabi |
