Jump to content
  • Advertisement
Sign in to follow this  
chbfiv

Which is more correct? C# implementation

This topic is 3607 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm just looking for some feedback. Alternatives would also be interesting! I have a base class with a Size and Location. I also have a class that inherits from that base, but it doesn't care about the Size and Location being logged to Console. Of the two options below, which is more correct way to prevent the Console.WriteLine? Size and Location var's must remain on the base class, and the Console.WriteLine must happen for all classes except SomeFoo if you choose to write a 3rd option=) .NET 2.0 1:
    public class FooBase
    {
        Size mySize = new Size();
        Point myLocation = new Point();

        
        protected virtual void CallMeSomeTimeAfterMyClassIsConstructed()
        {   
            if (myLocation.IsEmpty && mySize.IsEmpty)
            {
                Console.WriteLine("Size and Location are empty.");
            }
        }
    }

    public class SomeFoo : FooBase
    {
        protected override void CallMeSomeTimeAfterMyClassIsConstructed()
        {
            
        }
    }






2:
    public class FooBase
    {
        protected bool hasSizeAndLocation = true;
        Size mySize = new Size();
        Point myLocation = new Point();

        private void CallMeSomeTimeAfterMyClassIsConstructed()
        {
            if (hasSizeAndLocation && myLocation.IsEmpty && mySize.IsEmpty)
            {
                Console.WriteLine("Size and Location are empty.");
            }
        }
    }

    public class SomeFoo : FooBase
    {
        public SomeFoo()
        {
            this.hasSizeAndLocation = false;
        }

    }







Share this post


Link to post
Share on other sites
Advertisement
The second. Since the derived class doesn't need to know of the logging console, you can just keep that function as a private method of the base class.

Share this post


Link to post
Share on other sites
Neither. If the subtype doesn't care about the base member variables (a direct departure from the base class' purpose), it shouldn't be a subtype. Without more info, I'd be inclined to think that they might be better as siblings of a common base class which actually is the common behavior.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!