2

I am a new programmer and my new project is to make a journal of sort.

I want the journal itself to be a list, but every new post to be a new array which can contain 2 items (an title and the post). I should add that this is for a school project, so a bump in the right direction is all I need.

I hope I made myself clear in what im trying to do, and thanks in advance!

LePelican
  • 47
  • 8
  • 2
    Instead of an array I'd suggest you create a class with a `Title` and `Post` properties. – juharr Dec 15 '16 at 19:03
  • Im not sure if I am allowed to do that since they asked for an array. But is that something that generally is better to do? and why? :) – LePelican Dec 15 '16 at 19:05
  • 6
    Using array to act as a struct to hold different types of data is an extremely bad approach. The school should not even encourage that for transition learning. – user3437460 Dec 15 '16 at 19:07
  • The primary benefit is that you will define something that is exactly what you are working with instead of an array that could have more or fewer values than 2 values. Also it makes it very clear what value is the title and which is the post. – juharr Dec 15 '16 at 19:07
  • 1
    @Patrik Its not generally better; its *always* better. C# is an object oriented language, using objects to represent your data is a far superior approach to arrays – BradleyDotNET Dec 15 '16 at 19:10
  • 1
    Alright, i might have to look into the class system then! I mean.. they cannot be mad at me for using a more efficient method to solve the problem, I hope. – LePelican Dec 15 '16 at 19:12
  • You should study the various collection types .NET offers. Array is aways the least unlikely candidate for a good choice. To combine two strings the built-in choice would be a Tuple but classes are so much more extensible.. – TaW Dec 15 '16 at 19:23

3 Answers3

5

According to your comment, what you're looking for is probably:

List<string[]> theList = new List<string[]>();

However, juharr is correct that if you didn't have to use an array - you should create a class like he suggested.

ispiro
  • 26,556
  • 38
  • 136
  • 291
2

I know you have been asking for a list of string array. But this is another alternative.

List<Post> journal = new List<Post>();

class Post{
    private String title;
    private String content;
}

I am not sure whether you school had asked you to use a String array to hold data of different nature. Such as:

+-------+-------+
| title | post  | postArray
+-------+-------+ 

But I see it as something that should be discouraged. I would use a class to group the data, then use a relevant data structure to hold them.

user3437460
  • 17,253
  • 15
  • 58
  • 106
  • Thank you, all the comments seem to agree that the class way is the way to go, so thanks for this comment aswell :) – LePelican Dec 15 '16 at 19:14
0

Another way to go about this could be to use Structs.

Here's an example:

internal class Program {

    struct Page { //Like an inline class, usually used if you need an object that won't be reused often
        /// <summary>
        /// Name of Page
        /// </summary>
        public String Name { get; set; }
        /// <summary>
        /// Text Page contains
        /// </summary>
        public String Text { get; set; }
        /// <summary>
        /// Date the Page was wrote
        /// </summary>
        public DateTime Date { get; set; }

        /// <summary>
        /// Creates a new Page
        /// </summary>
        /// <param name="n">Name of the Page</param>
        /// <param name="t">Text contained in the page</param>
        /// <param name="d">Date of the Page</param>
        public Page(String n, String t, DateTime d) {
            this.Name = n; //Name
            this.Text = t; //Text
            this.Date = d; //Date      Use DateTime.Now to get the current time
        }
    }

    public static void Main(string[] args) {

        List<Page> Book = new List<Page>();

        Book.Add(new Page("Page Name", "I'm Some Text!", DateTime.Now)); //An example entry
        Book.Add(new Page("Page2", "And some more!", DateTime.Today));
        Book.Add(new Page("Page 3", "And a bit more", DateTime.MaxValue));

        foreach (var p in Book) { //for each variable in book -> var can be used if you don't know the object's type
            string str = string.Format("{0} | {1} : {2}", p.Date, p.Name, p.Text); //returns a string where the numbers replaced by the parameters
            Console.WriteLine(str); //writes the string to the console
        }

        Console.Read(); //Stops console from automatically closing

        /*RESULT
         * 
         * 15/12/2016 19:42:46 | Page Name : I'm Some Text!
         * 15/12/2016 00:00:00 | Page2 : And some more!
         * 31/12/9999 23:59:59 | Page 3 : And a bit more
         * 
         */
    }


}

You could replace the

public String Text { get; set; }

with

public List<String> Text { get; set; }

but if you were to do that, you would need to iterate through all the lines to get the full text on the page.

You would also need to use a new list or an existing list when you create the page, instead of passing through a string as we did here.

artman41
  • 402
  • 2
  • 5
  • 15
  • 1
    Mutable structs (structs with public `set` accessors) are the cause of [nightmarish problems](http://stackoverflow.com/questions/441309/) like values not changing after you set them. Only create them if you really really need them. Patrik doesn't need them. – Dour High Arch Dec 15 '16 at 19:56
  • @DourHighArch never knew that, hmm. I typically only use them if I need an object quickly, that isn't worth a class, which is why I suggested it as another alternative in this example – artman41 Dec 15 '16 at 20:06