Trevor Dunnbar
Trevor Dunnbar

Reputation: 1

C# How to add records

This an assignment my teacher gave to us, the situation has to be I add one record of the name,age,gender first and then after, i will choose from the basic functions, then I will add the records,

Is there anyone who has a good idea to do this? My idea was to add number of added records first but it doesnt work, another idea i have is to ask user input to add records by yes or no.

Does anyone have any idea to do this especially the enjoining of the added record to the first record?

Console.WriteLine("Record Management System");
Console.WriteLine("--------------------");
        int max = 1;
        String[] name = new string[max];
        String[] age = new string[max];
        String[] gender = new string[max];
        var rand = new Random();
        var id = new byte[max];
        rand.NextBytes(id);

        for (int i = 0; i < max; i++)
        {
            Console.Write("Enter name: ");
            name[i] = Console.ReadLine();
            Console.Write("Enter age: ");
            age[i] = Console.ReadLine();
            Console.Write("Enter gender: ");
            gender[i] = Console.ReadLine();
        }
            Console.WriteLine("BASIC FUNCTIONS");
            Console.WriteLine("a. Display All Records");
            Console.WriteLine("b. Add Record");
        while (true)
            {
                Console.Write("Enter letter of basic function you want to use: ");
                String function = Console.ReadLine();

                if (function.Equals("a", StringComparison.CurrentCultureIgnoreCase))
                {Console.WriteLine("ID" + "\t" + "Name" + "\t" + "Age" + "\t" + "Gender" + "\t");
                for (int a = 0; a < max; a++)
                {Console.WriteLine(id[a] + "\t" + name[a] + "\t" + age[a] + "\t" + gender[a]+"\t");}}
                
                else if (function.Equals("b", StringComparison.CurrentCultureIgnoreCase))
                {Console.Write("Enter number of registers to add: ");
                 int max2 = Convert.ToInt32(Console.ReadLine());
                 max += max2;
                    for (int i = 0; i < max2; i++)
                    {
                      Console.Write("Enter name: ");
                      name[i] = Console.ReadLine();
                      Console.Write("Enter age: ");
                      age[i] = Console.ReadLine();
                      Console.Write("Enter gender: ");
                      gender[i] = Console.ReadLine();
                    }
                }
            }

Upvotes: 0

Views: 358

Answers (1)

Caius Jard
Caius Jard

Reputation: 74605

This here is a method:

public void PrintMenu(){
  Console.WriteLine("a. Display all records");
  Console.WriteLine("b. Add a record");
}

It represents several statements that have one purpose. It makes it easy to re-use logic without generating very long programs that repeat themselves pointlessly. It means you can extend the menu with more options without having to go to many places in the code that printed the menu as individual items

You actually already know what methods are in the sense of how to use them: WriteLine("some text here") is a method on the Console object. It writes text to the console. Main() is a method too. Methods should be peers of each other, inside a class:

class X{
  void Main(){

  } 
  void PrintMenu(){

  }
}

i.e. they should be inside the class's { } brackets but not inside each other's { } brackets

Here is another method:

public string Ask(string question){
  Console.Write(question);
  return Console.ReadLine();
}

It's a bit more useful, because it nicely encapsulates asking a question and getting the answer, so you could use it elsewhere in your code:

static void Main(){

  PrintMenu();

  string choice = Ask("Enter a choice: ");

}

You could have even more methods:

void PrintAllRecords(){

  for(int i = 0; i < age.Length; i++){
    Console.WriteLine("Record " + i + " is a person with Name " + name[i] " and Age " + age[i]);
  }

}

void AskForAllPersonDetails(int index){ //take a parameter of what index to store the details in
   _names[index] = Ask("What name?");
   _ages[index] = Ask("What age?");
   _genders[index] = Ask("What gender?");
}

In order for this method to have access to the _names, _ages and _genders arrays they will have to be declared inside the class, not inside another method:

class X{
  private string[] _names = new string[10];

  void Method1(){
    //i can access _names here
    _names[0] = "John";

    //the method can also have its own variables
    string job = "Software";
  }

  void Method2(){
    //i can access _names here too
    _names[1] = "Jane";

    //but I cannot access variables that are declared inside another method
    Console.WriteLine(job); //won't work
  }
}

These are the rules of scope. Simplistically, brackets { } define the start and end of a scope. Anything declared inside a scope is visible within the scope, including for child scopes that are inside the parent scope. Sibling scopes can't see each other's variables. The methods are siblings, and they are children of the class. They can hence see variables declared as part of the class but not variables declared in a sibling scope (Method2 is a sibling of Method1; Method2 cannot see the job variable declared in Method1)

Personally (and in common with a lot of the C# community) I tend to prefix variables declared at the class level with an underscore. It actually denotes "private or protected class level variable", but realistically because we call public things with an initial capital LikeThis, the underscore essentially means "variable with class-wide scope" and it becomes a handy discriminator for variables declared in a method (i.e. only accessible inside the method) vs variables declared in a class (i.e. accessible inside any method within the class, so a way to share data between methods)


Diversion into scoping rules aside, you can hook all these things up:

class Program{

  private string[] _names = new string[10];
  private int _nextEmptyIndex = 0;

  void Main(){

    string choice = Ask("what choice?");

    if(choice == "a"){
      PrintAllRecords();
    }
    else if(choice == "b"){
      AskForAllPersonDetails(_nextEmptyIndex);
      _nextEmptyIndex = _nextEmptyIndex + 1;
    }
  }

  string Ask(string question){
    ...
  }

  void PrintAllNames(){
    ...
  }

  void AskForAllPersonDetails(int index){
    ...
  }
}

Hopefully you can see how using methods means you can make a program built up from collections of smaller blocks of functionality, rather than trying to make the whole thing as one massive 1000 lines splurge of code in the Main. By using good names for your methods you can make the program read like plain English. We call this "being self documenting" => there isn't any need for comments if the way the program is written reads like a story. Methods should do one thing, and be named something that adequately describes that one thing

Upvotes: 1

Related Questions