1

I want to check if my array hasn't stored any data on the array yet then I'll write "No Data Yet" I tried myArray.Length == 0 but it doesn't seem to work.

Result

using System;

namespace IntegMidTerm
{
    class Program
    {
        static String[] fName = new string[10];
        static long[] cNumber = new long[10];
        static String[] numbers = new string[10] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
        static String fullName;
        static int i = 0, k = 0;
        static long contactNumber;

    static void Main(string[] args)
    {
        int optNum;

        while (true)
        {
            Console.WriteLine("\n---Welcome to my C# Phonebook Application---\n");
            Console.WriteLine("\tMain Menu" +
            "\n[1] Create New Contact" +
            "\n[2] View All Contancts" +
            "\n[3] Exit Application\n");

            Console.Write("Enter option number: ");
            optNum = Convert.ToInt32(Console.ReadLine());

            switch (optNum)
            {
                case 1:
                    createContact();
                    break;

                case 2:
                    displayContact();
                    break;

                case 3:
                    Console.WriteLine("Thank you for using my application! Terminating program...");
                    Environment.Exit(0);
                    break;
            }
        }
    }

    public static void createContact()
    {
        Console.Write("\nEnter your full name: ");
        fullName = Console.ReadLine();
        fName[i] = fullName;
        i++;

        Console.Write("\nEnter your contact number: ");
        contactNumber = Convert.ToInt64(Console.ReadLine());
        cNumber[k] = contactNumber;
        k++;
    }

    public static void displayContact()
    {
        if (fName.Length == 0 && cNumber[i] == 0) 
        {
            Console.WriteLine("No Data Yet!");
        }

        for (int j = 0; j < numbers.Length; j++)
        {
            Console.WriteLine(numbers[j] + ". " + fName[i] + " - " + cNumber[k]);
        }
    }
}
}

The above result I've shown is somewhat wrong it is because I haven't created new contact yet, then I press number 2 which will show the data stored but since I use if statement which is if array is empty or no data stored yet "No Data yet" will show. but in my case it shows the data.

8
  • 3
    fName.Length will never be 0 because you're declaring it as containing 10 items: static String[] fName = new string[10]; Did you actually intend to check fName[i].Length like cNumber[i]? Commented Apr 27, 2021 at 9:28
  • Yes, I tried doing what you've said but it throw exception saying System.NullReferenceException Commented Apr 27, 2021 at 9:41
  • 1
    If you haven't assigned a string value to fName[i], then it will be the default value: null. Do you just want to check if it's != null? Commented Apr 27, 2021 at 9:43
  • 1
    If you would like to check whether fName contains at least one item which is not null (null is the default value if the item is not set yet), so at least item was set to a new value in the array, you could check that with this: bool containsNonNullItem = a.Count(s => s != null) != 0; But if you set a value to null the bool will still say false, even though you set the items value, so it's not a perfect solution. Commented Apr 27, 2021 at 9:45
  • 1
    As an aside, assuming that "Contact number" means something like "telephone number" don't use a type such as long/int to store it. A phone number is not an integral number. For this task I'd say just use a string. Commented Apr 27, 2021 at 10:13

1 Answer 1

3

As you are using arrays, when an array is being initialized, its length will be constant. In your case, fName and cNumber will have a length of 10 always. If you would like to have dynamic length including zero, use List in C#. Like this:

static List<string> fName = new List<string>();
if (fName.Count == 0)
{
  // do something
}
// to add an item to list you can do:
fName.Add("john");
Sign up to request clarification or add additional context in comments.

12 Comments

Does arraylist and list the same? We're only allowed to use arraylist or array depends on what we want to use so I tried using Array.
@Testtest Yes array list is almost the same as List. If you need more, see microsoft docs here: learn.microsoft.com/en-us/dotnet/api/…
If your teacher is telling you to use arraylist, find a new teacher @Testtest.
@mjwills , She says if it is needed only so I use array to store the data.
@Testtest My point is that arraylist is a terrible type to suggest anyone should use. I would be upset if someone suggested it 10 years ago, let alone today.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.