简体   繁体   中英

Dynamic 3D Arrays C#

Hi I am currently making a program and one aspect of it is to open other programs at a certain time and date.

I am wanting to add the details into a 3D dynamic array as there could be as many programs as the user wishes to add in.

    public static string[,,] programData = new string[,,]
    {
        //Program                   Monday          Tuesday     Wednesday       Thursday        Friday        Saturday      Sunday          Once
     {{"File Path","File Name"},{"Day","Time"},{"Day","Time"},{"Day","Time"},{"Day","Time"},{"Day","Time"},{"Day","Time"},{"Day","Time"},{"Day","Time"}},
     {{"File Path","File Name"},{"Day","Time"},{"Day","Time"},{"Day","Time"},{"Day","Time"},{"Day","Time"},{"Day","Time"},{"Day","Time"},{"Day","Time"}},
     {{"File Path","File Name"},{"Day","Time"},{"Day","Time"},{"Day","Time"},{"Day","Time"},{"Day","Time"},{"Day","Time"},{"Day","Time"},{"Day","Time"}},

    };

The array should have a fixed value of 9 rows and 2 columns but there could be many programs[x,9,2].

Please message is this is not enough information to go off thanks.

You could use a nested dictionary.

Example being:

Dictionary<string, Dictionary<string, DateTime>> fileLaunchTimes = new Dictionary<string,       Dictionary<string, DateTime>>();
Dictionary<string, DateTime> dict = new Dictionary<string, DateTime>();
dict.Add("Monday", DateTime.Now); //etc (replace datetime.now with whatever time you want
dict.Add("Tuesday", DateTime.Now);
fileLaunchTimes.Add("filename", dict);

I dont know the performance issues with jagged arrays but your case i think you need to store

details of programs and its associated available time in your program. first create a model object of your data some thing like

 public enum Days
    {
        Sunday = 0,
        Monday,
        Tuesday,
        WednessDay,
        Thursday,
        Friday,
        Saturday,
        Once
    }
    public class Day
    {

        public Days AssociateDay { get; set; } // used enum for better coding
        public DateTime Time { get; set; }
    }
public class Program
{
    public Program()
    {
        Days=new List<Day>();
    }
    public int Id { get; set; }
    public string FilePath { get; set; }
    public string FileName { get; set; }
    public List<Day> Days { get; private set; }

    public void AddDay(Day day)
    {
        if(Days.Any(x=>x.AssociateDay==day.AssociateDay))
        { // i dont know whats ur logic  here i am  returning without doing anything

            return;

        }

        Days.Add(day);

    }

}

public class MyProgram
{
    private static void Main(string[] args)
    {

        List<Program> Programs = new List<Program>();

        // Code to add new Program

        Program urProgram = new Program {FileName = "UrFile.aspx", FilePath = "Ur System Drive",Id = 1};
        urProgram.AddDay(new Day{AssociateDay = Days.Sunday,Time = DateTime.Now});

        Program myprogram = new Program { FileName = "MyFile.aspx", FilePath = "My System Drive" ,Id = 2};
        urProgram.AddDay(new Day { AssociateDay = Days.Monday, Time = DateTime.Now });



        Program theireePrgram = new Program { FileName = "theireeFile.aspx", FilePath = "their System Drive", Id = 3 };
        theireePrgram.AddDay(new Day { AssociateDay = Days.Monday, Time = DateTime.Now });

        // Your program object created now you can add to list collection

        Programs.Add(urProgram);
        Programs.Add(myprogram);
        Programs.Add(theireePrgram);

        // Update a program 

        int id = 1;

        var pgmUpdate = Programs.First(x => x.Id == 1);

        pgmUpdate.AddDay(new Day{AssociateDay = Days.Monday,Time = DateTime.Now});


        // Delete a program

        id = 3;
        Programs.Remove(Programs.First(x => x.Id == id));


        // List all programs


        foreach (var program in Programs)
        {
            Console.WriteLine(program.Id);
            Console.WriteLine(program.FileName);
            Console.WriteLine(program.FilePath);

            foreach (var day in program.Days)
            {
                Console.WriteLine(day.AssociateDay);
                Console.WriteLine(day.Time);
                Console.WriteLine("............................");
            }

            Console.WriteLine("*************************");
        }

        Console.ReadLine();



    }


}

it is only a suggestion because this kind of coding give you much sense , here you can easly access the data. dealing with array is bit tricky..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM