I initialize a string, then I set it to something else, I later get it and print it with a button click BUT the first time I click the button I get the original string " ". A few seconds later I click again and this time the string is the new modified one (which I want). Why would this happen?

string addy = " ";
public void ClientDownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {            
            string addy = e.Result;
            SetName(addy);
                      
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
           
            WebClient client = new WebClient();
            client.DownloadStringCompleted += ClientDownloadStringCompleted;
            client.DownloadStringAsync(new Uri("http://www.google.com"));

            string synop = GetName();
            textBlock1.Text = synop;}
public string GetName()
        {
            return addy;
        }

        public void SetName(string name)
        {
            addy = name;
            
        }

Wow, Ive been trying to figure this out for quite a while now, and the answer was staring me right in the face. I had declared addy again in the method with e.result...my bad!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.