Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
So im practising creating small Windows Forms Apps and im having an issue with a method that is supposted to get the input from the user. I made a similar Project a while ago and it works just fine. The error code says: Non invocable member "Task" cannot be used as a method. Note that the one that gives me error is returning DateTime.





Error one:


private Task GetTaskFromUserInputs (out bool success)
            {
                success = false;

                Task newTask = new Task(); 


                newTask.Description = ReadDescription(out success);
            
                if (!success)
                    return null;

                newTask.DateTime = ReadDate(out success);
                if (!success)
                    return null;

                newTask.Priority = ReadPriority(out success);
                    return null;
            }





working just fine one:





private ShoppingItem ReadInput (out bool success)
        {
            success = false;

            ShoppingItem item = new ShoppingItem();

            item.Description = ReadDescription(out success);
            if (!success)
                return null;

            item.Amount = ReadAmount(out success);
            if (!success)
                return null;

            item.Unit = ReadUnit(out success);
                return item;


What I have tried:

Just started Learning to code so im not sure how to handle the error






using System.Text;
using System.Threading.Tasks;

namespace FriendlyReminder
{
    class Task
    {
        DateTime date;
        private string description;
        PriorityType priority;

        public string Description
        {
            get { return description; }
            set { description = value; }
        }

        public string DateTime;

        public PriorityType Priority
        {
            get { return priority; }
            set { priority = value; }
        }

        public DateTime Date
        {
            get { return date; }
            set { date = value; }
        }

        public Task (DateTime date, PriorityType priority, string description)
        {
            this.date = date;
            this.priority = priority;
            this.description = description;
        }






        public string GetPriorityString()
        {
            return string.Empty;

        }

        public string GetTimeString()
        {
             return date.ToShortDateString();
        }

        

        public string ToString()
        {
            return $"{date.ToShortDateString(),-12}" +
                   $"{GetTimeString(),-7}" +
                   $"{GetPriorityString(),-16}" +
                   $"{description}";


        }

    }
}
Posted
Updated 28-May-20 12:25pm
v2

1 solution

Why ignoring the success for the last step?

You could try either
C#
newTask.Priority = ReadPriority(out success);
return newTask; // ignoring success status
or
C#
newTask.Priority = ReadPriority(out success);
if (!success)
   return null;
return newTask; // returning the Task instance only on success
 
Share this answer
 
Comments
Helin1 28-May-20 17:31pm    
@phil.o Thanks, didnt notice that one. Any clue why i cannot use Task newTask = new Task(); in the first snippet? The error code says: Non invocable member "Task" cannot be used as a method.
phil.o 28-May-20 17:38pm    
I don't know. System.Threading.Tasks.Task class does not have any empty constructor (a constructor without any argument). But then, you wouldn't get the error message you actually get (it would rather be something along "No constructor accepting zero argument is defined").
What is this task class you are using exactly? Did you create it yourself? If so, could you please use the green "Improve Question" button and provide the definition of this class?
Edit: the error message suggests that Task would rather be a property, but then you would get a compilation error since the Task class would appear non-existent. None of this makes really sense.
Helin1 28-May-20 18:39pm    
Should be updated now. Yeah i cant really tell since im just starting to learn the do´s and don´ts of c# and programming in general. Really appreciating the help!
phil.o 28-May-20 18:46pm    
You're welcome. You could try two things: first, explicitly declaring the Task class as public. Second: giving it a parameterless constructor, which assigns default values for date, priority and description; or use in the calling code the existing constructor (the one with parameters).
Edit: as there already exists a Task class in the System.Threading.Tasks namespace, you should give your own class another name; or, if you want to keep that name, then use the fully qualified name (FriendlyReminder.Task) in every file where the System.Threading.Tasks namespace has been imported.
Helin1 28-May-20 20:48pm    
Thank you for your help, i had to create Another constructor

public Task() : base()
{
}

and that solved it :)

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