Home > Enterprise >  Can't link two methods C#
Can't link two methods C#

Time:09-27

I have code:

using System;
using System.Globalization;
using System.IO;
using System.Linq;

namespace TimeTress
{
    class Program
    {
        static void Main(string[] args)
        {
            var timelineCSV = GetListOfStringsFromTextFile("../../../../timeline.csv");
            var peopleCSV = GetListOfStringsFromTextFile("../../../../people.csv");

            var timeline = ReadData(timelineCSV);
            var people = ReadData(peopleCSV);
        }
        static string GetNameOfPeoples(string[][] people)
        {
            var nowDate = DateTime.Now;
            var countYears = 20;
            if ((DateTime.IsLeapYear(yearBirthday)) && (bitrthdayElement.Year - nowDate.Year) <= countYears) return namePeople;
        }
        static (string, int, DateTime) GetNamePeoplesAndYears(string[][] people)
        {
            foreach (var elements in people)
            {
                DateTime bitrthdayElement = ParseDate(elements[2]);
                var yearBirthday = bitrthdayElement.Year;
                var namePeople = elements[1];
            }
            return (namePeople, yearBirthday, birthdayElement);
        }
        static string[] GetListOfStringsFromTextFile(string filePath)
        {
            string[] stringArray;

            if (File.Exists(filePath))
            {
                stringArray = File.ReadAllLines(filePath);
            }
            else
            {
                stringArray = new string[0];
            }

            return stringArray;
        }
        static void OutputString(string[] getString)
        {
            foreach (var lines in getString)
            {
                Console.WriteLine(lines);
            }
        }

        static string[][] ReadData(string[] array)
        {
            string[][] splitData = new string[array.Length][];
            for (var i = 0; i < array.Length; i  )
            {
                var line = array[i];
                string[] parts = line.Split(";");
                splitData[i] = parts;
            }

            return splitData;
        }
        static DateTime ParseDate(string value)
        {
            DateTime date;
            if (!DateTime.TryParseExact(value, "yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
            {
                if (!DateTime.TryParseExact(value, "yyyy-mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
                {
                    if (!DateTime.TryParseExact(value, "yyyy-mm-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
                    {
                        throw new Exception("WRONG FORMAT");
                    }
                }
            }

            return date;
        }

    }
}

In GetListOfStringsFromTextFile i get string[]. In ReadData i get a string[][].

The whole program works correctly, the problem is in two methods.

GetNamePeoplesAndYears which takes values from a file like {1; name; date} and returns three values name, year of birth and full date of birth

The second method GetNameOfPeoples checks the year of birth for leap years and then checks if the person is under 20 years old

CodePudding user response:

If you want to link the two methods GetNamePeoplesAndYears and GetNameOfPeoples and return the values you may need to change the parameter passing to the GetNameOfPeoples function. By passing the respective values or a class having the needed data can solve your issue. For example:

 var result =  GetNamePeoplesAndYears(people);
 GetNameOfPeoples(result.Item1, result.Item2, result.Item3);


      static string GetNameOfPeoples(string namePeople, int yearBirthday, DateTime bitrthdayElement )
        {
            var nowDate = DateTime.Now;
            var countYears = 20;
            if ((DateTime.IsLeapYear(yearBirthday)) && (bitrthdayElement.Year - nowDate.Year) <= countYears) 
                return namePeople;
             return String.Empty; // if your data doesn't met the condion it needs to return some string. So add needful 

            }

by using a class you can do the same. then the parameter will be the object of that class. Hope it will help you to solve this issue.

Edit: If you want the string[][] as input parameter and return all the names and age you can try this

static List<propleData> NamePeoples(string[][] people) {
            
        var listPeople = new List<propleData> { };
        foreach (var elements in people) 
        {
            var item = new propleData();
           
            item.names = elements[1];
            item.years = ParseDate(elements[2]).Year;
            listPeople.Add(item);
        }
        return listPeople;
   
}
//create a class or you can use dictionary instead of list
        public class propleData
        {
            public string names { get; set; }
            public int years { get; set; }


        }

you can retrieve the data from the list by iterating the list. I'll give an example.

var namePeople = NamePeoples(people);
            string name = string.Empty;
            int year = 0;
            foreach (propleData propleData in namePeople)
            {
                name = propleData.names;
                year = propleData.years;

                // you can print or save the data to array
            }
  • Related