locked
Multiple Errors Using <Task> RRS feed

  • Question

  • User-1446158388 posted

    When I execute my code below, I get these two compile errors, what do I do to remove them?

    The type or namespace name 'Task' could not be found (are you missing a directive or an assembly)?
    Missing compiler required memeber 'System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Task'
    The return type of an async method must be void, Task or Task<T>

    And here is my code, what do I need to do to remedy?

    namespace nmsp122332
    {
        public partial class HiByeCULater : System.Web.UI.UserControl
        {
            private DataSet gc = new DataSet();
            private DataSet cfg = new DataSet();
            private DataSet app = new DataSet();
    		private DED dogeatdog = new DED();
    
    		protected void Page_Load(object sender, EventArgs e)
    		{
    			try
    			{
    				CheckPermissions();
    				if (!IsPostBack)
    				{
    					Setup();
    					runfast();
    					hideme();
    					eatslow();
    
    					var t1 = runfast();
    					var t2 = hideme();
    					var t3 = eatslow();
    					ShowAllData();
    				}
    			}
    			catch { }
    		}
    
    		async Task runfast()
    		{
    			gc = dogeatdog.runfast();
    		}
    		async Task hideme()
    		{
    			cfg = dogeatdog.hideme();
    		}
    		async Task eatslow()
    		{
    			app = dogeatdog.eatslow();
    		}
    		ShowAllData()
    		{
    			gv1.DataSource = gc;
    			gv1.DataBind();
    			gv2.DataSource = cfg;
    			gv2.DataBind();
    			gv4.DataSource = app;
    			gv4.DataBind();
    		}
    	}
    }
    
    
    

    Monday, March 7, 2016 6:54 PM

Answers

All replies

  • User-434868552 posted

    @ShowFreeFlyi...     welcome to forums.asp.net

    try qualifying Task or adding a reference:

    async  System.Threading.Tasks.Task runfast()

    "The return type of an async method must be void, Task or Task<T>"

    async Task runfast()            //   <===  promise to return a Task
    {
        gc = dogeatdog.runfast();
    return ??????????????' // <=== you are NOT returning a Task
    }
    Monday, March 7, 2016 7:38 PM
  • User-1446158388 posted

    @ShowFreeFlyi...     welcome to forums.asp.net

    try qualifying Task or adding a reference:

    async  System.Threading.Tasks.Task runfast()

    "The return type of an async method must be void, Task or Task<T>"

    async Task runfast()            //   <===  promise to return a Task
    {
        gc = dogeatdog.runfast();
    return ??????????????' // <=== you are NOT returning a Task
    }

    Hello and thank you for your response.  I fully qualified Task and that seems to have removed those compiler errors.  I added a return type, but I must not be understanding what it is asking for.  In the example above, I want to return the dataset being stored in gc so I used this syntax:

    async Task runfast()
    {
    	gc = dogeatdog.runfast();
    	return gc;
    }

    However, that gives me this compiler error:

    Since 'runfast()' is an async method that returns 'Task', a return keyword must not be followed by an object expression. Did you intend to return 'Task<T>'?

    Monday, March 7, 2016 7:46 PM
  • User1633621018 posted

    Hi,

    If you are returning something from your function then it should have a return type like below example. If your gc object is of type int then int, if it is some custom class then the name of the class or anything. More you can read here: https://msdn.microsoft.com/en-us/library/hh191443.aspx

    async Task<int> runfast()
    {
    	gc = dogeatdog.runfast();
    	return gc;
    }
    
    or
    async Task<YourClass> runfast()
    {
    	gc = dogeatdog.runfast();
    	return gc;
    }

    Monday, March 7, 2016 8:22 PM
  • User-1446158388 posted

    Thank you for that link, it helped me :) -- I altered my code to and that remedied one compiler issue, but now a new one presents itself.

    async Task<DataSet> runfast()
    {
    	gc = dogeatdog.runfast();
    	return gc;
    }

    How do I overcome this?

    Missing compiler required member 'System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.Task'

    Monday, March 7, 2016 8:37 PM
  • User1633621018 posted

    Get reference for using System.Runtime.CompilerServices;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 7, 2016 8:41 PM