Solved

how to override a get in c#

Posted on 2011-02-24
3
310 Views
Last Modified: 2012-05-11
I have situation in my application where i need to ensure

if (OrderItemStatusId == 0)
{
   OrderItemStatusId =1;
}

so i'm wondering how would i add this override to

 public virtual int OrderItemStatusId { get; set; }
0
Comment
Question by:frosty1
3 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
Comment Utility
If that virtual version is coming from a base class you can do something like:
    public class DerivedClass : BaseClass
    {
        public override int OrderItemStatusId
        {
            get
            {
                return base.OrderItemStatusId;
            }
            set
            {
                if (value == 0)
                    base.OrderItemStatusId = 1;
                else
                    base.OrderItemStatusId = value;
            }
        }
    }

Open in new window

0
 

Author Comment

by:frosty1
Comment Utility
hi carl, well it's just virtual because i'm using nhibernate. it needs that for the lasy loading.
0
 
LVL 33

Expert Comment

by:Todd Gerbert
Comment Utility
The "virtual" keyword is applied to indicate that derived types (that is classes that inherit from the class where the virtual keyword is applied) are able to override the behavior for the given method/property.  Note that in carl_tawn's example he changes the logic of OrderItemStatusId's "set" slightly, but by calling "base.OrderItemStatusId" he is allowing the behavior defined in BaseClass's "set" for this property to do the actual work; he does this because he may not know what should happen when this property is set, i.e. for all he knows when the OrderItemStatusId is set to a particular number certain other properties need to be set to specific values.  He is able to do that because in BaseClass the property OrderItemStatusId was declared with the "virtual" keyword.

class Program
{
	static void Main(string[] args)
	{
		Car c = new Car();
		NormalCar nc = new NormalCar();
		OtherNormalCar onc = new OtherNormalCar();
		FunnyCar fc = new FunnyCar();

		Console.WriteLine("Car wheel count: " + c.Wheels.ToString());
		Console.WriteLine("NormalCar wheel count: " + nc.Wheels.ToString());
		Console.WriteLine("OtherNormalCar wheel count: " + onc.Wheels.ToString());
		Console.WriteLine("FunnyCar wheel count: " + fc.Wheels.ToString());

		Console.ReadKey();
	}
}

class Car
{
	public virtual int Wheels
	{
		get
		{
			return 4;
		}
	}
}

class NormalCar : Car
{
	// Does not alter Wheels property at all
}

class OtherNormalCar : Car
{
	public override int Wheels
	{
		get
		{
			return base.Wheels; // Just does whatever the "get" for the base class, which is Car, does
		}
	}
}

class FunnyCar : Car
{
	public override int Wheels
	{
		get
		{
			return 3; // Overrides the behavior of Car's Wheels "get" for this property
		}
	}
}

Open in new window

0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now