c# - where is the error. LINQ to XML -
where error here . in bottom of code .
error 1 use of unassigned local variable 'interfaces'
public static void create_interfaces() { xdocument interfaces; list<string> intf = new list<string>{"em0","em1","em2"}; foreach (var in intf) { interfaces = new xdocument( new xelement("interfaces", new xelement("interface", new xelement("name", i), new xelement("vlan-tagging", xelement.emptysequence), new xelement("unit", new xelement("vlan-id", "10"), new xelement("family", new xelement("inet", new xelement("address", new xelement("name", "10.10.1.23/24")))))))); } interfaces.save("interfaces.xml"); }
the c# compiler not smart are. though know there loop through, compiler not.
because of this, compiler thinks there chance intf
empty in case interfaces
never assigned. since trying call save(..);
on interfaces
after loop, compiler complains.
that being said, seems reassigning interfaces
in every loop, without using result previous iterations(thereby throwing away previous iterations xdocuments
). intentional?
Comments
Post a Comment