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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:15:59+00:00 2026-06-18T14:15:59+00:00

I’m working on app for managing tasks. Every task has its own status. And

  • 0

I’m working on app for managing tasks. Every task has its own status. And every status has to be easily assignedable (like enum — task1.status = statuses.started). And every status has to know its displayColor, name, etc. So it cant be simple enum (I need something like — task1.status.color). I dont want to use switch, or a lot of ifs. Everithing has to be very quick (because this will be iterated a lot of times) and code has to be clean.

What I did:

public class BaseStatusType {

        public Color color;
        private string name;

        public BaseStatusType() { 

        }

        public override string ToString()
        {
           return "status" + name;
        }

        [Serializable]
        public class Untaken : BaseStatusType
        {

            public Untaken()
            {

                color = Appname.Core.App.Default.statusUntaken;
                name = "Untaken";

            }

        }

… and several task types(Taken, Started, Ended, Billing) more like this ..

Then there is Status class

    public class Status
        {

            public BaseStatusType Type;

            public Status()
            {

                this.Type = StatusType.statusUntaken;

            }
     }

And most important static part. Thanks to this part, it can be easily assigned.

[Serializable]
    public static class StatusType {

        public static BaseStatusType.Untaken statusUntaken = new BaseStatusType.Untaken();
        public static BaseStatusType.Taken statusTaken = new BaseStatusType.Taken();
        public static BaseStatusType.Started statusStarted = new BaseStatusType.Started();
        public static BaseStatusType.Ended statusEnded = new BaseStatusType.Ended();
        public static BaseStatusType.Billing statusBilling = new BaseStatusType.Billing();            

    }

Now, are all statuses initialized once, while app start. Not every time Status is created. While creating new Task, new Status is created, but its Type is only assigned, not created new.

Status status1 = new Status();
            status1.Type = StatusType.statusEnded;
            Color somecolor = status1.Type.color;

And now to the problem. Everithing work fine, until I deepClone object Task. My deepClone uses serialization/deserialization. The problem can be well described like this:

            Task task1 = new Task();
            task1.Status.Type = StatusType.statusEnded;

            Task task2 = new Task();
            task2 = task1.DeepClone();

            if (task1.Status.Type == StatusType.statusEnded) { 

                //this returns true

            }

            if (task2.Status.Type == StatusType.statusEnded)
            {

                //this returns false

            }

            if (task2.Status.Type.ToString() == StatusType.statusEnded.ToString())
            {

                //this returns true

            }

It’s probably because it creates its own StatusType.statusEnded while DeepCopy. But I dont understand why. Shouldn’t Status.Type holds only reference to a static object? I tought so. Thats why I wasn’t worry about DeepCopy. It should made only copy of reference, not copy of static object.

So what holds property Status if no address to a static value?

  • 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-18T14:16:00+00:00Added an answer on June 18, 2026 at 2:16 pm

    Your Status.Type property is not static. When you DeepClone the object a new instance is created and assigned to the cloned instance.

    You have two ways around this:

    1. Modify DeepClone to find an existing instance and assign that to the clone instead of creating a copy
    2. Implement IEquatable and override the usual suspects: GetHashCode, Equals, == and !=

    The advantage of #2 is that you do not have to ensure that your status objects are immutable singletons (i.e. guarantee that there is only exactly one instance and that it is never modified once created). This is a simple programming model and I doubt that it will affect performance of your application in the slightest.

    The following is an example of a class implementing the IEquatable interface:

    public class Identity : IEquatable<Identity>
    {
        public Guid UniqueIdentifier { get; set; }
        public string Name { get; set; }
    
        public bool IsEmpty
        {
            get { return UniqueIdentifier == Guid.Empty; }
        }
    
        #region Construction
        public Identity()
        {
        }
    
        public Identity( Guid uniqueIdentifier, string name )
        {
            UniqueIdentifier = uniqueIdentifier;
            Name = name;
        } 
        #endregion
    
        public override string ToString()
        {
            return string.IsNullOrWhiteSpace( Name ) ? UniqueIdentifier.ToString() : Name;
        }
    
        #region IEquatable
        public override int GetHashCode()
        {
            return ToString().GetHashCode();
        }
    
        public override bool Equals( object obj )
        {
            return Equals( obj as Identity );
        }
    
        public static bool operator ==( Identity left, Identity right )
        {
            if( ReferenceEquals( null, left ) )
                return ReferenceEquals( null, right );
            return left.Equals( right );
        }
    
        public static bool operator !=( Identity left, Identity right )
        {
            if( ReferenceEquals( null, left ) )
                return !ReferenceEquals( null, right );
            return !left.Equals( right );
        }
    
        public bool Equals( Identity other )
        {
            if( ReferenceEquals( null, other ) )
                return false;
            return ToString() == other.ToString();
        }
        #endregion
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
In my XML file chapters tag has more chapter tag.i need to display chapters
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words

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.