3

This does not appear to be a duplicate of the many hundreds of other questions with the same error. I have looked at them all and have found them to be unrelated.

I am making a little note app and am trying to read files from a directory. Following the MSDN example, I have the following code, but it is giving me an Error of:


Error 1 The modifier 'async' is not valid for this item C:\Users\Jase\documents\visual studio 2012\Projects\AppNameHere\AppNameHere\DataModel\AppNameHereDataSource.cs 192 9 AppNameHere


The code I have is:
    async public NotesDataSource()
    {
        StorageFolder documentsFolder = KnownFolders.DocumentsLibrary;
        StringBuilder outputText = new StringBuilder();

        IReadOnlyList<StorageFile> fileList =
            await documentsFolder.GetFilesAsync();
        outputText.AppendLine("Files:");

        foreach (StorageFile file in fileList)
        {
            if (file.FileType == "txt")
            {
                outputText.Append(file.Name + "\n");
            }
        }
        // lots of irrelevant code removed.
    }


I don't understand what's going on here. I followed everything to a "T". Can somebody please help?

Thank you!

Arrow
  • 2,784
  • 8
  • 38
  • 61
  • What .NET Framework are you trying to target. `async` is only a keyword if an extension of the .NET Framework is installed and/or if you trying to target .NET Framework 4.5. Please provide a link to the MSDN example. – Security Hound Jun 12 '12 at 13:01
  • It's for a Metro app in Windows 8 release preview: http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh758323.aspx - and this error is not because Windows 8 is not finished yet (as I've been told many times) - because *many* other apps are reading from and writing to files in Metro apps. – Arrow Jun 12 '12 at 13:03
  • 3
    possible duplicate of [Can constructors be async](http://stackoverflow.com/questions/8145479/can-constructors-be-async) – Kendall Frey Jun 12 '12 at 13:04
  • 1
    As per the duplicate question, you can't have an async constructor. – Kendall Frey Jun 12 '12 at 13:05
  • I am trying some suggestions in comments and answers and will be back shortly. Thanks all :-) – Arrow Jun 12 '12 at 13:10

2 Answers2

13

Your method signature is incorrect. Look at it:

async public NotesDataSource()

Firstly, async has to come after the access modifier IIRC.

Secondly, either you're trying to create an async constructor (which you can't do) or you're trying to write a method without a return type (which is equally invalid).

Try this:

public async Task NotesDataSource()

That's if you meant it to be a method. If you want to effectively create an async constructor (or something close to it) you'd have to use an async static method:

public static async Task<NotesDataSource> CreateInstance()
{
    // Do async stuff here which fetches all the necessary data...

    return new NotesDataSource(...);
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I can't use async after the access modifier. The compiler throws a hissy fit and tells me not to do it. So I tried putting it before the modifier and it got it working (more than it was, anyway) – Arrow Jun 12 '12 at 13:08
  • 1
    @Jason: Well you've still got a compilation error, so it's not really working... you *should* have `async` after the access modifier, so I think you were just shifting the problem. – Jon Skeet Jun 12 '12 at 13:09
  • @JonSkeet: Out of curiosity, (if you don't mind) I want to know : how do you manage to post so many answers (and comments) on daily basis? How much time do you spend on SO on an average? – Nawaz Jun 12 '12 at 15:11
  • @Nawaz: It's unclear - it's very much a background activity that I fit in between other things. – Jon Skeet Jun 12 '12 at 15:16
  • 1
    @JonSkeet: Surely, it is a background activity. I'm just wondering how it is even humanly possible to post so many answers; and on the top of it, you don't get bored of answering trivial questions. I must say you've great passion for C#. BTW, can I assume you spend almost 4 hours in total, daily? – Nawaz Jun 12 '12 at 15:19
  • @Nawaz: Again, it's hard to really say how much time I spend, I'm afraid. – Jon Skeet Jun 12 '12 at 15:20
  • 1
    @JonSkeet: So you've no idea at all? Not even a range, something like, 4-6 hours, or something? :|.. or at least, how frequently you visit SO, or how many times, etc. Any such thing? – Nawaz Jun 12 '12 at 15:22
  • @Nawaz: No, not really. How much of your day is taken by breathing? It happens while you're doing other things. How many times do you breathe? It's not something you count. – Jon Skeet Jun 12 '12 at 15:30
  • @JonSkeet: But if you ask me, I can answer that doing some little calculation, for example, how many times do I breathe in a minute? How many minutes are there in a single day? That gives pretty much good idea of the number of breathe I take in a single day, doesn't it? – Nawaz Jun 12 '12 at 15:34
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/12451/discussion-between-jon-skeet-and-nawaz) – Jon Skeet Jun 12 '12 at 15:42
1

I think you need to place async after public:

public async NotesDataSource() 
{
    //your code here
}
Widor
  • 13,003
  • 7
  • 42
  • 64
  • That's what I thought. Compiler doesn't like that for some reason. – Arrow Jun 12 '12 at 13:09
  • 1
    @Jason That's because in addition, you need to also implement one or the other of Jon Skeet's suggestions regarding Constructors and Static Methods. – Widor Jun 12 '12 at 13:10