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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:26:03+00:00 2026-06-15T04:26:03+00:00

I am writing one line of text in a file every time a button

  • 0

I am writing one line of text in a file every time a button is pressed.

I have defined a static class like this:

public static class PalletRecord
{
    public static string MachineName { get; set; }
    public static DateTime TimeStamp { get; set; }
    public static string BranchPlant { get; set; }
    public static string Location { get; set; }
    public static string Data { get; set; }
    public static string ItemCode { get; set; }
    public static string ItemDescription { get; set; }
    public static decimal Quantity { get; set; }
}

Every time i need to write to a file i populate the static class properties and i use File.AppendAllLines to write to text file.

Should i have done it with a non-static class?

  • 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-15T04:26:04+00:00Added an answer on June 15, 2026 at 4:26 am

    Yes ! Using statics to keep state like this is going to get you in trouble. This might especially be true if you might need multi threading now or in the future, since the static state is shared by all threads. Another problem could be an incomplete initialization bug, leaving you with ½ the state from a previous record and ½ the state from a new record.

    A much better approach would be to make the properties non-static, and create one instance of the class with the correct properties for each record. You could make a constructor which sets the properties, so that you know you have a valid record whenever you have an instance (also consider making the property setters private).

    This also better models the problem you are working with. You write multiple, different records in the file, this is best represented by instances of a class that represents a record. In my opinion, using an instance of your class for each record you need to write is the proper clean object oriented way to do this.

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

Sidebar

Related Questions

What is a good way of writing something like this in one line. The
I have a text file like this: 11 2 3 4 11 111 Using
I have a text file containing 21000 strings (one line each) and 500 MB
I have a text file fold.txt that contains one line fold_nam : $ cat
We have a command line exe that takes input from a text file and
Is there a faster way to search each line of one text file for
I have my project writing to two text files. one for input and one
i have a (perhaps stupid) question: im using 2 threads, one is writing floats
I've got a JS array which is writing to a text file on the
I read in a text file that is tab delimited, i then have a

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.