vikramnr
vikramnr

Reputation: 93

How to store the string with function in C#

I'm beginner in C# programming. I'm struck with this simple program where I want to display the eligible candidates. My question is that how can I store the name of candidate after knowing that he/she is eligible.

    int eligble = 0;    //For counting the eligble Number of candidates

    bool retry;         //For trying until the Number of eligble candidates is reached
    retry = true;
    while (retry)
    {
        string candidatename;   //Intilization for Candidate Name ,Date of Birth ,10th and 12th Percentages
        int tper, twper;
        string  dob;


        Console.WriteLine("Please enter your Name");    //Getting user input values
        candidatename = Console.ReadLine();
        Console.WriteLine("Please enter your date of birth in dd/mm/yyyy format");
        dob = Console.ReadLine();
        DateTime dt = Convert.ToDateTime(dob);
        Console.WriteLine("Please enter your 12th percentange");
        twper = Convert.ToInt16(Console.ReadLine());
        Console.WriteLine("Please enter your 10th percentange");
        tper = Convert.ToInt16(Console.ReadLine());

        int age1 = age(dt);
        if (eligble > 5)        //Checking whether we have selected the Eligble amount of candidates
        {
            Console.WriteLine("We have selected five eligble candidtes");
            retry = false;
            Console.WriteLine("n");
        }
        else
        {
            if (age1 > 20 && twper > 65 && tper > 60)   //Checking Whether the candidate have satisfiyed the Conditions
            {
                eligble += 1;
                string grad = rade(twper, tper);
            }
            else
            {
                eligble -= 1;
            }
        }
    }
}

static int age(DateTime _dt)                                   //Function for calculating the age of the candidate
{
    DateTime n = DateTime.Now;                                  // To avoid a race condition around midnight
    int age = n.Year - _dt.Year;

    if (n.Month < _dt.Month || (n.Month == _dt.Month && n.Day < _dt.Day))
        age--;

    return age;
}

static string rade(int _tper, int _twper)
{
    string grade1;
    int avg= ( _tper+_twper)/ 2;
    if (avg > 90)
    {
         grade1 = "a";
        return grade1;
    }
    else if (avg > 80 && avg < 80)
    {
       grade1 = "b";
        return grade1;
    }
    else if (avg > 70 && avg < 79)
    {
         grade1 = "c";
        return grade1;
    }
    else
    {
         grade1 ="d";
        return grade1;
    }
}

Upvotes: 1

Views: 634

Answers (2)

PiotrWolkowski
PiotrWolkowski

Reputation: 8782

"Store" has broad meaning. You can store your data in memory for the time your program is running. In that case C# offers plenty of collections. List will work if you just want to keep them.

var names = new List<string>();
names.Add(candidate.Name);

If you prefer to store them with some kind of a key and then use the key to get the value from the collection better choice would be a dictionary:

var myEligibleCandidates = new Dictionary<string, string>();
myEligibleCandidates[candidate.Id] = candidate.Name;

These option will preserve the values for the time the application is running. If you want your values to be stored also after the program is not running you can do that using a file. A static File class can be a good start:

public void WriteToFile(string path, List<string> names)
{
    File.WriteAllText(path, string.Join(";", names));
}

This method will take a list of names as a parameter and will write them separated by semicolon to a file. path is the path of the file.

Then there is option to save your data in the database. If that's what you want to do then take a look at Entity Framework and ADO.NET. Though, I would wait with these two options till you get better understanding of the first and second solution.

Upvotes: 2

asdfasdfadsf
asdfasdfadsf

Reputation: 391

make a new List object, to do so do:

List<string> eligableCandidates = new List<string>();

and then when you want to add something to the list do:

eligableCandidates.Add(candidateName);

Hope this helps,

Jason.

Upvotes: 1

Related Questions