a method that will return a vaule

  • Thread starter Thread starter Doug
  • Start date Start date
D

Doug

Hi

I have a method that I would like to return the value to the calling event.
I have a bad example below that doesnt give me what I want.

What I want is to have a my readxml mthod return the value of a string in an
xml file if that element is in the xml file - but a message saying that the
string doesnt exist if it is not there.

I have used a method that should return a string, but it doesn't work- even
though the string is in the xml file. I often get an error that not all
code paths return a value. If you can fix my code to avoid this error
please help.

public string readxml()
{
XmlTextReader tr = null;
tr = new XmlTextReader(xmlFilename);
while (tr.Read())
{
if (tr.NodeType == XmlNodeType.Element)
{
if (tr.LocalName.Equals("location"))
{
return tr.ReadString();
}
}
else
{
return "Nothing here";
}
tr.Close();
}
}

thanks for you assistance

Doug
 
What should it return if there is nothing to read? i.e. the very first
tr.Read() returns false, or all entries are elements but aren't
"location"s? I'm guessing you should add a final return just before
the last brace.

As an aside - do you intend treating "Nothing here" as a magic value?
If so this is not recommended practice. Perhaps use the "Try{x}"
approach as per TryParse - i.e.

public bool TryReadLocation(out string value) {
value = null;
// your code... success becomes "value = tr.ReadString(); return
true;"

// failure
return false;
}

Marc
 
Hi Doug,

The reason for the compiler error is pointed out below

Hi

I have a method that I would like to return the value to the calling
event.
I have a bad example below that doesnt give me what I want.

What I want is to have a my readxml mthod return the value of a string
in an
xml file if that element is in the xml file - but a message saying that
the
string doesnt exist if it is not there.

I have used a method that should return a string, but it doesn't work-
even
though the string is in the xml file. I often get an error that not all
code paths return a value. If you can fix my code to avoid this error
please help.

public string readxml()
{
XmlTextReader tr = null;
tr = new XmlTextReader(xmlFilename);
while (tr.Read())
{
if (tr.NodeType == XmlNodeType.Element)
{
if (tr.LocalName.Equals("location"))
{
return tr.ReadString();
}


if tr.LocalName is not equal to location it will continue the loop, but
once the loop is over, there is no return

}
else
{
return "Nothing here";
}
tr.Close();
}

put return "Nothing here" as well. If you have abunch of valid xml
elements, but none that are called "location" you will get to this step.
}

thanks for you assistance

Doug

Summing up, the method should look like something like this (nb! haven't
tested the logic). I've taken the liberty to put the XmlTextReader inside
a using block which will automatically close it once you are finished with
it

public string readxml()
{
using (XmlTextReader tr = new XmlTextReader(xmlFilename))
{
while (tr.Read())
{
if (tr.NodeType == XmlNodeType.Element)
{
if (tr.LocalName.Equals("location"))
{
return tr.ReadString();
}
}
else
{
return "Nothing here";
}
}
}
return "Nothing here";
}

Beware that your code will return once it encounters a single non element
node and you may be better off not returning in the else at all, but
traverse all nodes and return at the end of the method.
 
As a final note, you can probably cheat here (combining the last 2
posts and extending):

public bool TryReadLocation(out string value) {
using (XmlTextReader tr = new XmlTextReader(xmlFilename)) {
if (tr.ReadToFollowing("location")) {
value = tr.ReadString();
return true;
} else {
value = null;
return false;
}
}
}

Marc
 
Thanks Marc,

But how do i call that method? I am currently calling the readXml method in
my form load code.

When I incorporate your method i get a message about no overload taking 0
arugments.

thanks again

doug
 
Hey Doug,

How are you trying to call that method? if your using the one below your
form_load should look something like this (assuming the method is part of
the form class)

form_load()
{
string myString = "";

if(TryReadLocation(myString))
{
//if we are here then TryRead worked and myString now contains
your value
Console.WriteLine("MyString is " + myString);
}
else
{
//if we are here then TryRead failed
Console.WriteLine("MyString didn't get found");
}


}

I wrote that quick but it should work (i also read the post quick so i hope
it helps), if you do that and look at the output tab in Visual Studio,
you'll see the name of your string if it read it correctly from the xml. I
hope that helps.
 
Close...

string myString; // no need to initialise
if(TryReadLocation(out myString)) {
// found and has value as-per myString
} else {
// not found
}

Marc
 
the code is going to look at the first node only (if there is one). and if
that node is an element, but the local name is not "location", it will not
return. and if there is nothing in the xmlfile, it will not return.

hope that helps.
 
Back
Top