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 548169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:03:03+00:00 2026-05-13T11:03:03+00:00

I have a method that returns an array of custom class objects that are

  • 0

I have a method that returns an array of custom class objects that are created by parsing a text file. At the moment every time I use it I am rereading the file which isn’t very efficient.

What I wat to do is create an array containing the objects when the page loads and store them in an array which can then be used later.

The method is:

public Album[] readArray(string sTextFilePath)
    {
        string[] allLines = File.ReadAllLines(Server.MapPath(sTextFilePath));
        Album[] Albums = new Album[allLines.Length];
        for (int i = 0; i < allLines.Length; i++)
        {
            string[] lineSplit = allLines[i].Split(',');
            Albums[i] = new Album();
            Albums[i].ID = Convert.ToInt32(lineSplit[0]);
            Albums[i].title = lineSplit[1];
            Albums[i].keyName = lineSplit[2];
        }

        return Albums;
    }

and the class is simple, just:

    public class Album
{
    public int ID { get; set; }
    public string title { get; set; }
    public string keyName { get; set; }
}

I thought I could create the static object using something like:

static Album myAlbums[] = readArray("Albums.txt");

but I am getting the following error:

A field initializer cannot reference the nonstatic field, method, or property ‘B2M._Default.readArray(string)’

I am new to C# so this is probably something dumb. (Feel free to poke fun in my general direction if this is the case!)

Thanks in advance for any assistance,

Ben

  • 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-05-13T11:03:03+00:00Added an answer on May 13, 2026 at 11:03 am

    “An error” isn’t very descriptive. When you’re posting a question and you’re getting an error, please state what that error is.

    In this case the problem is that you’re trying to call an instance method (readArray) from a static context, without specifying an instance.

    The method doesn’t appear to require any instance data, so you should be able to just make the method static and all will be well.

    I’d encourage you to read up on .NET naming conventions, btw. You might also want to think about using a List<T> instead of an array. When you feel more confident, you could also make this code simpler using LINQ – but I’d wait until you’re more familiar with C# first.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method (C++) that returns a character and takes an array of
I have a Ruby method that searches an array of hashes and returns a
I have method that returns module path of given class name def findModulePath(path, className):
I have a class that I use to setup objects in an array. In
I have a custom object that contains an array (called children) where objects of
I have method that returns Drawable , and if its Bitmap object is recycled
I have a method that returns a list of type string. I want to
I have a method that returns lot of data, should I use @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED) for
I have a method that returns an IEnumerable<KeyValuePair<string, ArrayList>> , but some of the
I have a method that returns a new list (it pertains to a multiple

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.