Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to override a get in c#

Posted on 2011-02-24
3
Medium Priority
?
320 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 2000 total points
ID: 34968670
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
ID: 34968697
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
ID: 34970575
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

721 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