1

There is a form that each user is required to complete, it has 4 fields: date, bill code, amount and currency. Bill code has a drop down menu with a lot of options of 4 that options are valid (Health, Travel, Meal, Hotel). Bill code field cannot be left blank, and it should take only one of these 4 options. A user make 4 entries with each of the 4 bill codes. If user enters only Health and Travel, an error message should fire that Meal and Hotel records need to be added. This is what I got so far:

public bool ValidateBillCode(bills billArray[][]) 
{
    for(int i = 0; i < billArray.Length; i++)
    {
        for(int j = 0; j < billArray[0].Length; j++)
        {
            if(billArray[i][j].IndexOf("Health") >= 0 ||
                billArray[i][j].IndexOf("Travel") >= 0 || 
                billArray[i][j].IndexOf("Meal") >= 0||
                billArray[i][j].IndexOf("Hotel") >= 0)
            {
                return true;
            } 
            else
            {
                return false;
            }
        }
    }
}   

But it doesn't make sure that all four of these are entered, and I'm not sure how to make an error message that would tell the user which of the four are missing. I will appreciate any help with this.

maccettura
  • 10,514
  • 3
  • 28
  • 35
Mainta
  • 35
  • 4
  • 1
    FYI this is called a jagged array. For your use case you seems to want to use it as an actual 2 dimensional array. I would suggest you simply use the 2d array and simply check if the length is 4 – Franck Sep 10 '18 at 18:10
  • In order to create an error message that indicates which values are missing, you'll have to write a separate check for each value. – itsme86 Sep 10 '18 at 18:11
  • Why are you using a two-dimensional array? What does each dimension refer to exactly? And what is `bills` exactly? – Persian Brat Sep 10 '18 at 18:25
  • @MostafaF. there are two array here, one for amount (should be equal or greater than zero), if anything is entered in the amount the must be a bill code. – Mainta Sep 11 '18 at 11:59
  • @itsme86 how to write a separate check for each value? – Mainta Sep 11 '18 at 12:00
  • @Franck How to implement a jagged array? – Mainta Sep 11 '18 at 12:01
  • @Mainta First it's difficult to tell with your code what is inside the jagged array as what you wrote is not enough for us to know what is inside. I only have a feeling that what you need is a 2d array and NOT a jagged array. I am saying that because it sound like you need just a dictionary like 1 string and 1 double(or int). – Franck Sep 11 '18 at 12:11
  • @Franck Yes, right. I have to correct myself. A user can enter multiple entries with the same bill code, but each of the 4 bill codes must be entered in order to submit the form. When a user saves a Log Bill form, retrieve the bill code numbers, and compare to internal array. If any bill code entered does not match these four, open query. How to do it? – Mainta Sep 11 '18 at 12:57
  • 1
    @Mainta I highly suggest you write a sample valid input and a sample invalid input to make sure it's clear – Franck Sep 11 '18 at 13:50

1 Answers1

0

I believe you don't need any 2-dimensional or jagged array. You need to define a struct like:

public struct Bill
{
    public string Date;
    public string BillCode;
    public string Amount;
    public string Currency;
}

Then your ValidateBillCode() becomes like this:

public bool ValidateBillCode(Bill[] billArray)
{
    bool healthEntered = false;
    bool travelEntered = false;
    bool mealEntered = false;
    bool hotelEntered = false;

    for (int i = 0; i < billArray.Length; i++)
    {
        if (billArray[i].BillCode == "Health")
            healthEntered = true;
        else if (billArray[i].BillCode == "Travel")
            travelEntered = true;
        else if (billArray[i].BillCode == "Meal")
            mealEntered = true;
        else if (billArray[i].BillCode == "Hotel")
            hotelEntered = true;
    }

    return healthEntered && travelEntered && mealEntered && hotelEntered;
}

But this is just a very simplistic approach. For a more appropriate solution, you'd better to use class instead of struct, use enum for BillCode, DateTime for Date, and double for Amount.

Persian Brat
  • 394
  • 2
  • 13
  • Thank you, but this gives me 4 error messages: 'bool' does not contain a definition for 'BillCode' and no extension method 'BillCode' accepting a first argument of type 'bool' could be found (are you missing a using directive or an assembly reference?). @Mostafa F. – Mainta Sep 11 '18 at 22:41
  • Did you use the exact code of my solution? Maybe you've mistakenly typed `ValidateBillCode(bool[] billArray)` instead of `ValidateBillCode(Bill[] billArray)`. – Persian Brat Sep 12 '18 at 05:30
  • This is what I get now: 'Bill': cannot have instance property or field initializes in structs. @Mostafa F. – Mainta Sep 12 '18 at 12:32
  • And also: 'Bill' does not contain a definition for 'BillCode' and no extension method 'BillCode' accepting a first argument of type 'Bill' could be found (are you missing a using directive or an assembly reference?) – Mainta Sep 12 '18 at 12:33
  • You're probably on the wrong track. To make sure it works, create a new console project. Then try copying both pieces of code that I have provided, into `Program` class, just after ending `}` of `Main` method. Then you'll see there are no compile errors. And you can also test it through `Main` method by providing a list of `Bill`s. – Persian Brat Sep 13 '18 at 05:44