blah blah blah is here! blah blah » Close

up0down
link

I had to split up my function with a if but the function does not allow returning values defined in the if()

if (nodeHeader == "SUBJ:"){
XmlNode T1 = d.CreateNode(XmlNodeType.Element, "ap", "Topic", "x");
} else {
XmlNode T1 = d.CreateNode(XmlNodeType.Element, "ap", "NotesGroup", "x");
}
Return T1;

I hope somebody can help me.
Pascal.

last answered one year ago

1 answers

up0down
link

The reason this code doesn't work is that you have distinct variables both called T1 which are local to the 'if' and 'else' blocks but are not accessible outside those blocks.

To make it work, I'd refactor it as follows:

XmlNode T1;
if (nodeHeader == "SUBJ:"){
T1 = d.CreateNode(XmlNodeType.Element, "ap", "Topic", "x");
} else {
T1 = d.CreateNode(XmlNodeType.Element, "ap", "NotesGroup", "x");
}
return T1;

You could also eliminate the T1 variable with:
if (nodeHeader == "SUBJ:"){
return d.CreateNode(XmlNodeType.Element, "ap", "Topic", "x");
} else {
return d.CreateNode(XmlNodeType.Element, "ap", "NotesGroup", "x");
}

Feedback