Hello. I already asked a question with practically the same code, but another problem that I managed to solve successfully, therefore I ask for help a second time. I have 4 classes in the program:
Basic Figure:
class Figure { private List<Figure> figures = new List<Figure>(); public void Add(Figure f) { figures.Add(f); } public string coords = ""; public virtual void getCoords() { } public virtual IEnumerable myItr() { for (int i = 0; i < 8; i++) { yield return "there are must be coords"; } } public static int X; public static int Y; public static int Z; public double SideSize; public string Name; } And derivatives, Round:
class Round : Figure { public override IEnumerable myItr() { yield return this.coords; } public Round(int sideSize, int x, int y) { X = x; Y = y; SideSize = sideSize; //getPerimeter(); getCoords(); } } Cube and Line are identical to the class round
In the main program, when reading from a file in a loop, objects with different parameters are created.
File contents:
round 10 2 3 round 5 4 6 round 3 5 -1 cube 4 -3 -2 3 cube 3 3 5 -2 cube 7 6 8 9 line 1 1 2 line 6 7 4 line 19 4 9 the program itself:
static void Main(string[] args) { StreamReader sr = new StreamReader("File.txt"); string text = sr.ReadToEnd(); string[] figuresParam = text.Split(' '); List<Round> rounds = new List<Round>(); List<Cube> cubes = new List<Cube>(); List<Line> lines = new List<Line>(); for (int i = 0, j = 0, c = 0, l = 0, r = 0; i < figuresParam.Length; i += 4, j += 1) { if (figuresParam[i] == "round") { rounds.Add(new Round(Convert.ToInt32(figuresParam[i+1]), Convert.ToInt32(figuresParam[i + 2]), Convert.ToInt32(figuresParam[i+3]))); rounds[r].Name = "round"; r++; }//Аналогично к первому еще два таких же if } Figure figures = new Figure(); int a = 0; for (int i = 0; i < lines.Count; i++) { figures.Add(lines[i]); a++; } for (int i = 0; i < rounds.Count; i++) { figures.Add(rounds[i]); a++; } for (int i = 0; i < cubes.Count; i++) { figures.Add(cubes[i]); a++; } foreach (string s in figures.myItr()) { Console.WriteLine(s); } Console.ReadKey(); } Foreach should (by my logic) implement iterators that are individual for each derived class, but only the same iterator is executed in the main class. What is the reason for this?
getPerimeter- are they important for this question, or will the problem remain if you remove them? The less code you have, the more chances for a good response from an experienced participant. This, by the way, is in your best interest. - VladD