4

Method CanVote returns true if Age >=18. Constructor of the class assigns default values to all properties. I added the objects to a hashtable with key as Person Name. I need to iterate through the hashtable object to print Name and whether the person can vote or not.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;*/

namespace HashTable
{
    class theClass
    {
        string name;
        string dob;
        int age;

        public theClass(string name,int age, string dob)
        {
            this.name = name;
            this.age = age;
            this.dob=dob;
        }

        public static string canvote(int age)
        {
            if (age >= 18)
                return "Can Vote";
            else
                return "Cnnot Vote";
        }
    }

    public class Solution
    {
        public static void Main()
        {
            Hashtable h = new Hashtable();
            theClass object1 = new theClass("John",16,"Chennai");
            theClass object2 = new theClass("Smita",22, "Delhi");
            theClass object3 = new theClass("Vincent",25, "Banglore");
            theClass object4 = new theClass("Jothi", 10, "Banglore");
            h.Add("John", object1);
            h.Add("Smita", object2);
            h.Add("Vincent", object3);
            h.Add("Jothi", object4);
            Console.WriteLine("df");
            Console.WriteLine(h.canvote());
            Console.ReadKey();
        }
    }
}
MethodMan
  • 18,625
  • 6
  • 34
  • 52
Swetha
  • 63
  • 1
  • 7

3 Answers3

2

You can iterate through your Hashtable using foreach loop. Also I would suggest to replace static canvote method with public property:

class theClass
{
    string name;
    string dob;
    int age;

    public theClass(string name, int age, string dob)
    {
        this.name = name;
        this.age = age;
        this.dob = dob;
    }

    public string CanVote => age >= 18 ? "Can Vote" : "Cannot Vote";  
}

public static void Main()
{
        Hashtable h = new Hashtable();
        theClass object1 = new theClass("John",16,"Chennai");
        theClass object2 = new theClass("Smita",22, "Delhi");
        theClass object3 = new theClass("Vincent",25, "Banglore");
        theClass object4 = new theClass("Jothi", 10, "Banglore");

        h.Add("John", object1);
        h.Add("Smita", object2);
        h.Add("Vincent", object3);
        h.Add("Jothi", object4);

        foreach(DictionaryEntry item in h){
            Console.WriteLine(item.Key);
            Console.WriteLine((item.Value as theClass).CanVote);
            Console.WriteLine();
        }

        Console.ReadKey();
}

Also it might be better to use strongly typed Dictionary<string, theClass> instead of Hashtable:

public static void Main()
{
        Dictionary<string, theClass> dict = new Dictionary<string, theClass>{
           {"John", new theClass("John", 16, "Chennai")},
           {"Smita", new theClass("Smita", 22, "Delhi")},
           {"Vincent", new theClass("Vincent",25, "Banglore")},
           {"Jothi", new theClass("Jothi", 10, "Banglore")}
        };

        foreach(var item in dict){
            Console.WriteLine(item.Key);
            Console.WriteLine(item.Value.CanVote);
            Console.WriteLine();
        }

        Console.ReadKey();
}

Or even HashSet with explicitly implemented EquilityComparer or overridden Equals and GetHashCode.

Roman Koliada
  • 4,286
  • 2
  • 30
  • 59
2

firstly,you should make age property public in theClass,

 Hashtable h = new Hashtable();
        theClass object1 = new theClass("John", 16, "Chennai");
        theClass object2 = new theClass("Smita", 22, "Delhi");
        theClass object3 = new theClass("Vincent", 25, "Banglore");
        theClass object4 = new theClass("Jothi", 10, "Banglore");

        h.Add("John", object1);
        h.Add("Smita", object2);
        h.Add("Vincent", object3);
        h.Add("Jothi", object4);


        foreach (DictionaryEntry entry in h)
        {
            //get the Class instance
            var tClass = (theClass) entry.Value;

            //call static canvote method, the age property must be public
            var message = theClass.canvote(tClass.age);

            Console.WriteLine("{0} => {1}", entry.Key, message);
        }
Müslüm ÖZTÜRK
  • 961
  • 1
  • 11
  • 30
1

This is how you do it:

foreach (var person in h.OfType<theClass>())
{
    Console.WriteLine($"{person.name} : {theClass.canvote(person.age)}");
}

Two recommendations here.

First, you should use a typed collection like Dictionary<string, theClass> instead of Hashtable. The Hashtable is basically deprecated. Generics in general improves performance and decreases chances of introducing type-safety bugs. See more here: https://learn.microsoft.com/en-us/dotnet/standard/collections/when-to-use-generic-collections

Replace your usage of Hashtable with Dictionary<string, theClass> as follows:

var h = new Dictionary<string, theClass>();
h.Add("John", object1);
h.Add("Smita", object2);
h.Add("Vincent", object3);
h.Add("Jothi", object4);

Provided you make the fields name and age public:

foreach (var person in h.Values)
{
    Console.WriteLine($"{person.name} : {theClass.canvote(person.age)}");
}

Second, I'd recommend changing your class as follows:

  • Convert fields to properties. Since fields are accessed from outside the class, properties are a better mechanism because it prevents outside code from changing your class in an uncontrolled way.

  • Make these properties public, as they have to be accessed from outside of the class.

  • Make canvote an instance method (not static), as already suggested in other answers.

Note that the properties only have getter. This means that your class is now immutable (that is, an object cannot be changed once initialized). If you do want to change these values after the object is initialized, you can make the properties { get; set; }.

Here is the complete listing:

class theClass
{
    public string name { get; }
    public string dob { get; }
    public int age { get; }

    public theClass(string name,int age, string dob)
    {
        this.name = name;
        this.age = age;
        this.dob=dob;
    }

    public string canVote()
    {
        if (age >= 18)
            return "Can Vote";
        else
            return "Cannot Vote";
    }
}

public class Solution
{
    public static void Main()
    {
        Dictionary<string, theClass> d = new Dictionary<string, theClass>();
        theClass object1 = new theClass("John",16,"Chennai");
        theClass object2 = new theClass("Smita",22, "Delhi");
        theClass object3 = new theClass("Vincent",25, "Banglore");
        theClass object4 = new theClass("Jothi", 10, "Banglore");
        d.Add("John", object1);
        d.Add("Smita", object2);
        d.Add("Vincent", object3);
        d.Add("Jothi", object4);

        foreach (var person in d.Values)
        {
            Console.WriteLine($"{person.name} : {person.canVote()}");
        }

        Console.ReadKey();
    }
}
felix-b
  • 8,178
  • 1
  • 26
  • 36