0

I'm not an expert programmer, mostly self-trained. Currently my Project is to create a XML parser for an application im writing in C# for Windows phone. To learn that, I'm trying the demo posted here: http://www.developer.nokia.com/Community/Wiki/Parse_Local_XML_file_in_Windows_Phone

all is good until I get to the following part:

     private void btnparse_Click(object sender, RoutedEventArgs e) 
    {
             this._parser = XMLParser.Instance;        
             StreamResourceInfo strm = Application.GetResourceStream(new Uri("/LocalXmlParsing;component/States.xml",UriKind.Relative));
     //needs to be done only once
             StreamReader reader = new StreamReader(strm.Stream);
             string data = reader.ReadToEnd();
             _parser.DataToParse = data;
             _parser.ParseStateData();
             lstStates.ItemsSource = _parser.StateCollection; 
}

I get the Error: "Error 2 The name '_parser' does not exist in the current context" I will take any advice you guys can give me.

2
  • 1
    Did you define field or property on the class named _parser? Commented Mar 21, 2013 at 20:26
  • 1
    I don't mean to be harsh; my advice is that you learn a little more about C# before copy-pasting code and wondering why it doesn't work. It may take some time up front but it will save you a lot of time in the long run. Commented Mar 21, 2013 at 20:30

2 Answers 2

3

this is always a reference to the current object (whose member functions are executing against). So this._parser is called a "field" or "member variable". These fields have to be defined in the class definition. You're not showing us the full class definition, but it certainly doesn't exist. And that's what the compiler is complaining about.

It should look something like this:

class Foo {
    private XMLParser _parser;

    // your functions, like btnparse_Click
}

Or, if you're only going to use the parser in the context of that one function, just make it a local variable:

private void btnparse_Click(object sender, RoutedEventArgs e) 
{
    XMLParser parser = XMLParser.Instance;
    ...

You can also use an implicitly typed local variable:

private void btnparse_Click(object sender, RoutedEventArgs e) 
{
    var parser = XMLParser.Instance;
    ...
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I get it now., I know that maybe I gotta get to read more about programming in C#, bit Try and Error has worked for me.
0

check if the _parser is defined as class variable Or if it is a variable in the parent class (in case your class has inherited a base class). I have not looked at the entire code but that is what the error suggests.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.