Resolved I am so confused.. Creating a task but keep getting errors in VS

Program4Food

Active member
Joined
Nov 22, 2021
Messages
40
Programming Experience
10+
Before I post the question, please keep in mind I am still fresh to C# and am self taught; I came from Visual Basic and got petty good with it but am now transitioning to C#.

So I create a new project in VS2012/DotNet 4.5.1 and try to add an async task, I type it just like examples I am seeing from Google, but no matter where I put the code,
in form.load or a button.press event VS gives me errors, which I *do not* understand so none of it makes sense to me. :-(

I apologize in advance for being a thorn (me) on a rose (this forum) but I really want to get proficient; the only way I know to do that is bang out code and figure out how it works, however when I reach a dead end in my ability to resolve an issue due to limited knowledge and/or ability to comprehend, I have to ask for help.

I have attached a screen shot of the VS error.

All I added to a blank project was async task..:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace createtask
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

            private async Task SomeJob()
            {
                //  some work here
            }




        }
    }
}
 

Attachments

  • 2021-11-22 11_54_24-Window.jpg
    2021-11-22 11_54_24-Window.jpg
    22.6 KB · Views: 17
You can't put a method into another method (until you start using C# 7.0 which is available in VS2017). See your lines 25-36.

 
You can't put a method into another method (until you start using C# 7.0 which is available in VS2017). See your lines 25-36.


I think my lack of understanding of WHERE to put it must be the problem then.

What is your recommendation for where to place my code?

Thank you for your reply, that gives me some insight!!

Andrew
 
Put lines 28-31 after line 36.
 
The "void button1_Click" is a method and the following { } defines the borders for "inside the method". Your new "Task SomeJob" method need to go outside that.
 
Ahhh.... I understand better now.

Allow me to clear my understanding (Im old and my mind is not as sharp as it once was).

I cannot put a method inside of a method.

Hence, my original code was putting method SomeJob() inside of method button1_Click(object sender, EventArgs e)
Am I correct thus far?

I need to put my method inside an area that is not a method, but within the namespace.
Am I correct thus far?

Thus placing my method SomeJob() inside section public partial class Form1 : Form is proper programming
Am I correct thus far?

I program for the joy, and to keep my mind stimulated. One of the few things I truly enjoy.

Many kind thanks for your valuable time!
Andrew
 
Methods live within classes. Classes live within namespaces.
 
Back
Top Bottom