How to keep the global variable not changed when passed to a function? C#
Problem
I am developing an add-in for OneNote. In the sinppet below, a global variable AllPageXML
is claimed first.
Then the very first time when a window is loaded, AllPageXML
is initinized with a value.
After that, the value of AllPageXML
is supposed to not be changed.
When the user input some keywords in a textbox, search in AllPageXML
will be excuted.
And AllPageXML
will be passed to an function FilterXml
. After returnning, the value of AllPageXML
has been changed.
How can I do to keep the value of AllPageXML
not changed? Any comments will be appreciated.
Code
internal partial class NaviPages : LocalizableForm
{
public XElement AllPageXML;
public NaviPages()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
using var one = new OneNote();
var AllPageXML = one.GetAllPages();
//...//
}
private void tBoxKw_TextChanged(object sender, EventArgs e)
{
try
{
string strKeyWords = tBoxKw.Text.ToLower();
FilteredPageXml = FilterXml(AllPageXML, strKeyWords);
}
catch
{
MessageBox.Show("Failed to filter!");
}
}
public XElement FilterXml(XElement inXML, string strKeyWords)
{
string strSecKw, strPageKw;
XElement root = inXML;
///...///
//delete empty notebook
foreach (var child1 in root.Elements().Reverse())
{
if (!child1.HasElements) child1.Remove();
}
return root;
}
}
Answer
You should change 2 things:
- Remove the redeclaration of
AllPageXML
in theOnLoad
method, so it refers to the instance variable instead (remove the var keyword):
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
using var one = new OneNote();
AllPageXML = one.GetAllPages();
//...//
}
You should change 2 things:
- Remove the redeclaration of
AllPageXML
in the OnLoad
method, so it refers to the instance variable instead (remove the var keyword):
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
using var one = new OneNote();
AllPageXML = one.GetAllPages();
//...//
}
The issue here is that inside the method, a local variable named AllPageXML is declared and assigned a value from one.GetAllPages(). However, this local variable shadows the class-level variable with the same name.
Instead of assigning the value to the class-level variable, the code assigns it to the local variable, which has a limited scope and does not affect the class-level variable. Therefore, when FilterXml is called later in the tBoxKw_TextChanged method, it uses the uninitialized class-level variable, resulting in unexpected behavior.
- Add the readonly keyword to the declaration of
AllPageXML
to indicate that its value should not be modified once initialized. If you do this you can only set the value in the constructor and it can not be set after:
public readonly XElement AllPageXML;
1 Comments
If you want to read more about this post please follow the link
ReplyDeletehttps://stackoverflow.com/questions/76415279/c-how-to-keep-the-global-variable-not-changed-when-passed-to-a-function
If you have any doubts, Please let me know