1

Im not sure im going about this the right way or not, so hopefully somebody can help me.

Im trying to use a var in a method, which is contained in a different method. As expected, I get the error: The name 'Title1' does not exist in the current context .

first im reading an xml file then populating bing maps with pushpins. One of the variables is the tite of each xml item, I need to use the "Title1" var on my method below.

Here is the code:

 public void OnOpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {


        var document = XDocument.Load(e.Result);

        if (document.Root == null)
            return;

        var xmlns = XNamespace.Get("http://www.blahblah.com");

        var events = from ev in document.Descendants("item")
                     select new
                   {
                       Latitude = Convert.ToDouble(ev.Element(xmlns + "Point").Element(xmlns + "lat").Value),
                       Longitude = Convert.ToDouble(ev.Element(xmlns + "Point").Element(xmlns + "long").Value),
                       Title = (ev.Element("title").Value),
                       Description = (ev.Element("description").Value),
                       Link = (ev.Element("link").Value),
                   };

        QuakeLayer.Children.Clear();

        foreach (var ev in events)
        {

            var accentBrush = (Brush)Application.Current.Resources["PhoneAccentBrush"];
            var Title1 = (ev.Title);
            var pin = new Pushpin

            {
                Location = new GeoCoordinate
                {
                    Latitude = ev.Latitude,
                    Longitude = ev.Longitude
                },
                Background = accentBrush,

                Content = Title1


            };

            QuakeLayer.AddChild(pin, pin.Location);


        }

    }


    public void Pushpin_ManipulationStarted(object sender,  ManipulationStartedEventArgs e)
    {

        NavigationService.Navigate(new Uri("/blahblah.xaml?info=" + Title1, UriKind.Relative));

    }
3
  • What if you pass Title as a parameter to the second method? Commented Dec 23, 2010 at 18:41
  • Are you a geophysicist or geologist by trade, out of interest? Your code snippet's got me interested :) Commented Dec 23, 2010 at 18:47
  • @Tim Barrass - Im just making an program which involves putting traffic information on a map pretty much :) Commented Dec 23, 2010 at 19:11

5 Answers 5

2

If you use an anonymous method inside your loop you will be able to access this variable (magic happens during compilation):

var pin = new Pushpin
{
    ...
    Content = Title1
};

pin.ManipulationStarted += (s, a) =>
{
    // Code for the event here
    // ... do something with Title1
};

QuakeLayer.AddChild(pin, pin.Location);
Sign up to request clarification or add additional context in comments.

1 Comment

This is absolutely perfect. You've actually sovled something ive been trying to do for a few weeks now!! Thank you so much Xavier.
2

Is the PushPin the sender of the event? If so, you can get the title from it since you set that as it's content.

public void Pushpin_ManipulationStarted(object sender,  ManipulationStartedEventArgs e)
{
    var pushPin = (PushPin)sender;
    var title = pushPin.Content;
    NavigationService.Navigate(new Uri("/blahblah.xaml?info=" + title, UriKind.Relative));

}

Comments

1

You can't reference local variables across different methods. For one thing, the local variable no longer exists when the function it's defined in returns. For another thing, it looks like the Title local variable will be assigned a lot of different values over the course of running through the events foreach loop, so moving the Title1 variable out to a class field won't solve anything.

Your best bet is probably to associate the Title1 with the pushpin object. What object is passed into your pushpin event as the sender? If that's the pushpin object itself, or the pushpin object is available via the event args parameter, then you're home free. The pushpin's Content property contains the Title1 value. Use Pushpin.Content instead of Title1.

Comments

0

Make it a class variable. Or pass it as an argument to another function. But the local variable is only valid inside a function. (A function can be called many times recursively. In that case, there are multiple copies of all the local variables in each stack frame. So what you're asking makes no sense.)

1 Comment

It should be noted though that a class variable will NOT work for his current needs.
0

Since Title1 is created inside of a loop the only real "viable" method is for you to pass the item as a parameter to the object, OR to store he value that you need in a location that you could get it from. (Possibly as a Tag on the object that starts your other event).

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.