- Home /
Initialisation of array across classes
Hey folks, I've created a few different classes to get details of a book:
[System.Serializable]
public class Textbook
{
public Chapter[] book;
public Textbook(int NumOfChapters)
{
book = new Chapter[NumOfChapters];
}
}
[System.Serializable]
public class Chapter
{
public string chapter;
public SubChapter[] subChapter;
public void SetNumber (int NumOfSubchapters)
{
subchapter = new SubChapter[NumOfSubchapters];
}
}
[System.Serializable]
public class SubChapter
{
public string subchapter;
public string imageVideo;
public string textFile;
}
What I'm trying to do is initialise the subChapter array held within the Chapter class, with a specific number of sub chapters. So for example when initialising the textbook object, I use:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
public class BookManager : MonoBehaviour {
public static BookManager ins;
public Textbook chemistry1 = new Textbook(3);
// Use this for initialization
void Start () {
ins = this;
}
}
and the constructor works fine. However, I'm scratching my head at how to then in turn set the number of the subchapter array for each chapter...if that makes sense?
Thanks in advance :)
Answer by bellicapax · Oct 05, 2016 at 08:02 PM
If you wanted to keep the initialization of your objects within the constructors and using the "new" keyword, you could simply add that information to your Textbook and Chapter constructors as an array of integers. The size of the array denotes how many chapters there are total and the item in the array denotes how many subchapters are in that chapter, so:
[System.Serializable]
public class Textbook
{
public Chapter[] book;
public Textbook(int[] NumOfSubchapters)
{
book = new Chapter[NumOfSubchapters.Length];
for (int i = 0; i < book.Length; i++)
{
book[i] = new Chapter(NumOfSubchapters[i]);
}
}
}
[System.Serializable]
public class Chapter
{
public string chapter;
public SubChapter[] subChapter;
public Chapter(int NumOfSubchapters)
{
subchapter = new SubChapter[NumOfSubchapters];
}
}
and then instantiate a book like so:
public class BookManager : MonoBehaviour
{
public static BookManager ins;
public Textbook chemistry1 = new Textbook(new int[] { 1, 3, 1, 5, 3, 5, 3 });
// Use this for initialization
void Start()
{
ins = this;
}
}
Answer by elenzil · Oct 05, 2016 at 07:53 PM
you could do something like this -
public Textbook(int[] subChapterCounts)
{
book = new Chapter[subChapterCounts.Length];
for (int n = 0; n < subChapterCounts.Length; ++n) {
book[n] = new Chapter(); // thanks to bellicapax
book[n].SetNumber(subChapterCounts[n]);
}
}
then you would initialize Textbook like:
chemistry1 = new Textbook(new int[]{10, 2, 5});
that would get you a book w/ 3 chapters, and 10 sub-chapters in the first, 2 in the second, etc.
This line is going to throw a NullReferenceException because although you initialized the array, you have no instance of a Chapter at book[n].
book[n].SetNumber(subChapterCounts[n]);
good point. will edit. our approaches are nearly identical.