?
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
Medium Priority
?
85 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 2000 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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

752 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