Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8639985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:09:51+00:00 2026-06-12T11:09:51+00:00

I am writing a Metro App. I am trying to read a file and

  • 0

I am writing a Metro App.
I am trying to read a file and return a float[] from the data. But no matter what I do, the function seems to return null. I have tried the solutions to similar questions to no luck.

For example if I use:
float[] floatArray = new ModelReader(“filename.txt”).ReadModel()
The result will be a null array.

However if I use:
new ModelReader(“filename.txt”)
The correct array will be printed to the console because “Test” also prints the array before returning it. This seems very weird to me.

Please give me some guidance, I have no idea what is wrong.

public class ModelReader
{
    float[] array;

    public ModelReader(String name)
    {
        ReadModelAsync(name);
    }

    public float[] ReadModel()
    {
        return array;
    }

    private async Task ReadModelAsync(String name)
    {
        await readFile(name);
    }

    async Task readFile(String name)
    {
        // settings
        var path = @"Assets\models\" + name;
        var folder = Windows.ApplicationModel.Package.Current.InstalledLocation;

        // acquire file
        var file = await folder.GetFileAsync(path);

        // read content
        var read = await Windows.Storage.FileIO.ReadTextAsync(file);
        using (StringReader sr = new StringReader(read))
        {
            Test test = new Test(getFloatArray(sr));
            this.array = test.printArray();
        }
    }

    private float[] getFloatArray(StringReader sr) { ... }


public class Test
{
    public float[] floatArray;

    public Test(float[] floatArray) 
    {
        this.floatArray = floatArray;
    }


    public float[] printArray()
    {
        for (int i = 0; i < floatArray.Length; i++)
        {
            Debug.WriteLine(floatArray[i]);
        }

        return floatArray;
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:09:52+00:00Added an answer on June 12, 2026 at 11:09 am

    You’re trying to get the result of an asynchronous operation before it has completed. I recommend you read my intro to async / await and follow-up with the async / await FAQ.

    In particular, your constructor:

    public ModelReader(String name)
    {
      ReadModelAsync(name);
    }
    

    is returning before ReadModelAsync is complete. Since constructors cannot be asynchronous, I recommend you use an asynchronous factory or asynchronous lazy initialization as described on my blog (also available in my AsyncEx library).

    Here’s a simple example using an asynchronous factory approach:

    public class ModelReader
    {
      float[] array;
    
      private ModelReader()
      {
      }
    
      public static async Task<ModelReader> Create(string name)
      {
        var ret = new ModelReader();
        await ret.ReadModelAsync(name);
        return ret;
      }
    
      ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Writing an iPhone app, and I'm getting my data from a REST API that
I am writing a metro app. This works: HttpClient client = new HttpClient(); var
I'm writing a Windows 8 Metro app (client-server) and would like to allow my
I am writing a metro app where it makes sense for focus to jump
I'm writing a Metro app in Windows 8 Consumer Preview. However, I'm unable to
I'm writing a Windows 8 Metro app in C#/XAML. One of the core functions
Writing a file utility to strip out all non-ASCII characters from files. I have
I'm currently writing a C# metro app for the Windows 8 consumer preview which
I'm writing a Windows 8 Metro app with a GridView with several pages worth
I'm writing an app for Windows 8 Metro and am having a problem understanding

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.