Click here to Skip to main content
15,947,389 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is a very basic question. From my understanding, += can be used to concatenate two strings. I've stopped using it because there's something I'm not understanding about it.

I downloaded a code sample from Microsoft. The variable Shown is not declared as a string and is nowhere else in the program. I am aware that this is a partial class.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading.Tasks;
using System.Net;
using System.Threading;
using System.Diagnostics;

namespace DownloadAsyncProgress
{
	public partial class frmDownloadAsync : Form
	{
		public frmDownloadAsync()
		{
			InitializeComponent();

			Shown += async ( s, e ) => { txtResult.Text = ( await DownloadAsync() ).Replace( "\n", "\r\n" ) + "Done!"; };
		}

		async Task<string> DownloadAsync()
		{
			Debug.WriteLine( "\nDownloadAsync()" );

			using ( var wc = new WebClient() )
			{
				var progress = new Progress<DownloadStringTaskAsyncExProgress>();

				progress.ProgressChanged += ( s, e ) =>
					{
						Debug.WriteLine( "Progress: " + e.ProgressPercentage + "%" );

						progressBar.Value = e.ProgressPercentage;

						txtResult.Text += e.Text.Replace( "\n", "\r\n" );
					};

				return await wc.DownloadStringTaskAsyncEx( @"http://www.microsfot.com/", progress );
			}
		}
	}
}


What I have tried:

The code works. I was expecting to see a global variable declaration, but Shown is no where else in the program.
Posted
Updated 22-Jul-18 0:26am

1 solution

For starters, C# doesn't support (or even have a concept of) global variables: everything is contained within a class. The closest it can get is a static variable that is accessed via the class name.

But ... you can't find the definition of Shown because it's not in your code - it's an Event which is part of the Form class: Form.Shown Event (System.Windows.Forms)[^]
 
Share this answer
 
Comments
[no name] 22-Jul-18 7:33am    
Thank you! Can you think of what async ( s, e ) might be? Given your info, I'm a bit closer, but these constructs are new to me and I can't make them out and not sure even what to search for, in this case. I have read up on async, but my brain isn't understanding it for some reason. I just wanted to know what to search for.
OriginalGriff 22-Jul-18 7:42am    
Start here: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/

It's way too much to fit into a tiny textbox like this!
[no name] 22-Jul-18 8:16am    
Ok, so I take it s and e are parameters of the async method. I didn't know it accepted parameters and never saw anyone use async with parameters so wasn't sure.
OriginalGriff 22-Jul-18 8:26am    
:laugh: How else would you tell it what to process?
[no name] 22-Jul-18 8:33am    
I thought async was a keyword though. I'm completely lost, but it's alright. I'm reading this article today.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900