Solved

How do I create properties in a model class that are derived from other values in that class?

Posted on 2015-02-20
2
83 Views
Last Modified: 2015-02-20
I have a ViewModel that contains two entities as shown below:

public class ClientMaxActivityViewModel
    {
        public Client clientData { get; set; }
        public Activity activityData { get; set; }

    }

I want to create a property in this model that is dependent on the values of properties in both the Client and Activity entities.   It would be a string with a value of either 'Compliant' or 'Non-compliant'. Here is some pseudo-code for the logic (I  know the syntax is all wrong, but hopefully the principal is evident):

public string Compliant
{
      if(Client.VisitFrequency == "Monthly" && DateDiff(Activity.ActivityDate - Date(Now)) > 31)
           { return "Non-compliant"
             else
          return "Compliant"}
}

Thanks.
0
Comment
Question by:MinirvaTwo
[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
2 Comments
 
LVL 34

Accepted Solution

by:
it_saige earned 500 total points
ID: 40621791
If you will be using this property in any decision logic, may I recommend using a boolean property instead:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EE_Q28621250
{
	class Program
	{
		static void Main(string[] args)
		{
			ClientMaxActivityViewModel model = new ClientMaxActivityViewModel() { clientData = new Client() { VisitFrequency = VisitFrequency.Monthly }, activityData = new Activity() { ActivityDate = DateTime.Now.AddMonths(1) } };
			Console.WriteLine("Is model compliant for Activity Date of {0} - {1}", model.activityData.ActivityDate.ToString("MM/dd/yyyy"), model.IsCompliant);
			model.activityData.ActivityDate = DateTime.Now.AddMonths(-1);
			Console.WriteLine("Is model compliant for Activity Date of {0} - {1}", model.activityData.ActivityDate.ToString("MM/dd/yyyy"), model.IsCompliant);
			Console.ReadLine();
		}
	}

	class ClientMaxActivityViewModel
	{
		public Client clientData { get; set; }
		public Activity activityData { get; set; }
		public bool IsCompliant
		{
			get 
			{
				if (clientData != null && activityData != null)
				{
					switch (clientData.VisitFrequency)
					{
						case VisitFrequency.Daily:
							return activityData.ActivityDate.AddDays(1) > DateTime.Now;
						case VisitFrequency.Weekly:
							return activityData.ActivityDate.AddDays(7) > DateTime.Now;
						case VisitFrequency.Monthly:
							return activityData.ActivityDate.AddMonths(1) > DateTime.Now;
						case VisitFrequency.Annually:
							return activityData.ActivityDate.AddYears(1) > DateTime.Now;
						case VisitFrequency.None:
						default:
							return false;
					}
				}
				return false;
			}
		}
	}

	class Client
	{
		public VisitFrequency VisitFrequency { get; set; }
	}

	class Activity
	{
		public DateTime ActivityDate { get; set; }
	}

	enum VisitFrequency
	{
		None = 0,
		Daily = 1,
		Weekly = 2,
		Monthly = 3,
		Annually = 4
	}
}

Open in new window

-saige-
0
 

Author Closing Comment

by:MinirvaTwo
ID: 40622001
Thanks it_saige!  You rock!  Worked perfectly.  I used text values in my switch/case statements instead of the enums, but it all worked great.  I could reference all of the properties of the ViewModel in the View, as shown below:

<td>
            @Html.DisplayFor(modelItem => item.clientData.Status)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.activityData.ActivityDate)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.IsCompliant)
        </td>

Thanks!
0

Featured Post

Independent Software Vendors: 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

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

738 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