473,513 Members | 2,563 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

a method that will return a vaule

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
Jan 17 '07 #1
9 1488
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
Jan 17 '07 #2
Hi Doug,

The reason for the compiler error is pointed out below

On Wed, 17 Jan 2007 09:51:26 +0100, Doug <go**********@optusnet.com.au
wrote:
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.

--
Happy Coding!
Morten Wennevik [C# MVP]
Jan 17 '07 #3
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
Jan 17 '07 #4
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
"Marc Gravell" <ma**********@gmail.comwrote in message
news:O2**************@TK2MSFTNGP02.phx.gbl...
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

Jan 17 '07 #5
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.
"Doug" <go**********@optusnet.com.auwrote in message
news:45**********************@news.optusnet.com.au ...
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
"Marc Gravell" <ma**********@gmail.comwrote in message
news:O2**************@TK2MSFTNGP02.phx.gbl...
>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


Jan 17 '07 #6
Close...

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

Marc
Jan 17 '07 #7
oops, yes well spotted.
"Marc Gravell" <ma**********@gmail.comwrote in message
news:eQ*************@TK2MSFTNGP06.phx.gbl...
Close...

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

Marc

Jan 17 '07 #8
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.
Jan 18 '07 #9
Thanks Daniel and Marc.

Works well - and I learned something too.

Cheers

Doug

"Daniel" <Da*****@vestryonline.comwrote in message
news:e5**************@TK2MSFTNGP03.phx.gbl...
oops, yes well spotted.
"Marc Gravell" <ma**********@gmail.comwrote in message
news:eQ*************@TK2MSFTNGP06.phx.gbl...
>Close...

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

Marc


Jan 18 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2780
by: Max Ischenko | last post by:
Hi, I wrote simple implementation of the "synchronized" methods (a-la Java), could you please check if it is OK: def synchronized(method): """ Guards method execution, similar to Java's...
31
2885
by: Chris S. | last post by:
Is there a purpose for using trailing and leading double underscores for built-in method names? My impression was that underscores are supposed to imply some sort of pseudo-privatization, but would...
10
1637
by: lkrubner | last post by:
I killed last night and a good chunk of today trying to figure out this one particular attempt to get a class and initialize it. My code is using a class method called getObject to include() a file...
4
2889
by: daniel.w.gelder | last post by:
I wrote a template class that takes a function prototype and lets you store and call a C-level function, like this: inline string SampleFunction(int, bool) {..} functor<string (int, bool)>...
10
3524
by: John Brock | last post by:
I have a base class with several derived classes (I'm writing in VB.NET). I want each derived class to have a unique class ID (a String), and I want the derived classes to inherit from the base...
18
4705
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
2
15722
by: Christophe | last post by:
class A {} class B {} interface MyInterface { void method(A a); void method(B b); }
3
1631
by: comp.lang.php | last post by:
class ThumbView extends PaginationView { function ThumbView() {} /** * Determine if the original image is actually an image and thus a thumbnail can be generated at all * * @access public...
4
2968
by: =?Utf-8?B?R3JlZw==?= | last post by:
I am a newbie to WCF so please forgive if this is an obvious question. I have the following. (Service contract) IEmployee public interface IEmployee { List<EmployeeGetEmployeeByID(string...
0
7259
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7380
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7535
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7523
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5085
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3232
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.