Normally to get the value inside the enum we say variable = type.value. Following example shows this. Problem is highlighted.
using System;
enum Weekday
{
Sun = 1, Mon, Tues, Wed, Thus, Fri, Sat
}
class Program
{
static void Main(string[] args)
{
Weekday wd;
wd = Weekday.Mon;
Console.WriteLine(wd);
Console.ReadKey();
}
}
But in the following example sortOrder differ from SortOrder. Please explain the reason. Problem is highlighted. One is lower case 's" other one is upper case 'S'
using System;
using System.Collections;
public class Person : IComparable
{
public enum SortMethod
{ Firstname = 0, Lastname = 1, Age = 2 };
private string firstname;
private string lastname;
private int age;
private static SortMethod sortOrder;
public string Firstname
{
get { return firstname; }
set { firstname = value; }
}
public string Lastname
{
get { return lastname; }
set { lastname = value; }
}
public int Age
{
get { return age; }
set { age = value; }
}
public static SortMethod SortOrder
{
get { return sortOrder; }
set { sortOrder = value; }
}
public Person(string firstname, string lastname, int age)
{
this.firstname = firstname;
this.lastname = lastname;
this.age = age;
}
public override string ToString()
{
return String.Format("{0} {1}, Age = {2}", firstname, lastname, age.ToString());
}
public int CompareTo(object obj)
{
if (obj is Person)
{
Person p2 = (Person)obj;
switch (sortOrder)
{
case SortMethod.Lastname:
return lastname.CompareTo(p2.Lastname);
case SortMethod.Age:
return age.CompareTo(p2.Age);
case SortMethod.Firstname:
default:
return firstname.CompareTo(p2.Firstname);
}
}
else
throw new ArgumentException("Object is not a Person.");
}
}
public class TestClass
{
public static void Main()
{
ArrayList people = new ArrayList();
people.Add(new Person("John", "Doe", 76));
people.Add(new Person("Abby", "Normal", 25));
people.Add(new Person("Jane", "Doe", 84));
people.Sort();
foreach (Person p in people)
Console.WriteLine(p.ToString());
Console.ReadLine();
Person.SortOrder = Person.SortMethod.Lastname;
people.Sort();
foreach (Person p in people)
Console.WriteLine(p);
Console.ReadLine();
Person.SortOrder = Person.SortMethod.Age;
people.Sort(); //This is not Array.Sort(people)
foreach (Person p in people)
Console.WriteLine(p);
Console.ReadLine();
}
}
Loading

VulpesPosted Oct 19, 2014, 5:30 PM
MahaPosted Oct 20, 2014, 8:12 AM
Thank you for your explanation.
VulpesPosted Oct 20, 2014, 7:54 AM
So, if you're saying that the program contains more detail than it actually needs then, yes, I agree with you.
MahaPosted Oct 20, 2014, 7:45 AM
Retaining the SortMethod property and commenting out Firstname, Lastname and Age propertis are compiling the program. I wish to know whether it is okay to do like that.
VulpesPosted Oct 20, 2014, 7:21 AM
The reason why we have properties in the first place is to hide fields from the calling code. If the value of the field needs to be validated, then this can be done by the property 'setter' before the field itself is set.
Even if no validation is required, it's still considered 'bad practice' to use public read/write fields. If you need to add validation later, then introducing a public property and making the field private changes the interface to your class which can be a problem if the class is part of a library which other code is consuming.
Admittedly this is verbose, but the verbosity can be reduced by using 'automatic' properties which removes the need to provide a private backing field - the compiler does this automatically.
MahaPosted Oct 20, 2014, 6:57 AM
In my view above program got lot of redundant stuff.
MahaPosted Oct 20, 2014, 4:12 AM