Выбор if else внутри конструктора

238
15 июня 2018, 02:50

Нужно в конструкторе присвоить полям класса определенные значения и реализовать выбор присваиваемого значения на основе данных пользовательского ввода, которые организованы в структуру и переданы из метода.

    public struct Format
     {
          public string formatID;
          public int multiplier;
          public double height;
          public double width;
          public string orientation;
          public string type;
          //Constructor
          public Format(string formatID, int multiplier, double height, double width, string orientation, string type)
          {
               this.formatID = formatID;
               this.multiplier = multiplier;
               this.height = height;
               this.width = width;
               this.orientation = orientation;
               this.type = type;
          }
     }
     public class FormatsHandler
     {
          private static double height;
          private static double width;
          // все в портретной ориентации
          public static List<Format> listOfFormats = new List<Format>
                                                     {
                                                     new Format { formatID = "A4", height = 297, width = 210 },
                                                     new Format { formatID = "A3", height = 420, width = 297 },
                                                     new Format { formatID = "A2", height = 594, width = 420 },
                                                     new Format { formatID = "A1", height = 841, width = 594 },
                                                     new Format { formatID = "A0", height = 1189, width = 841 }
                                                     };
          //Points of inner frame
          private Point2d keyPoint1;
          private readonly Point2d keyPoint2;
          private readonly Point2d keyPoint3;
          private readonly Point2d keyPoint4;
          //Points of outer frame
          private readonly Point2d frPoint1;
          private readonly Point2d frPoint2;
          private readonly Point2d frPoint3;
          private readonly Point2d frPoint4;
public FormatsHandler()
          {
               this.keyPoint1 = new Point2d(-width + 25, height - 10);
               this.keyPoint2 = new Point2d(0, height - 10);
               this.keyPoint3 = new Point2d(0, 0);
               this.keyPoint4 = new Point2d(-width + 25, 0);
               this.frPoint1 = new Point2d(-width + 5, height - 5);
               this.frPoint2 = new Point2d(5, height - 5);
               this.frPoint3 = new Point2d(5, -5);
               this.frPoint4 = new Point2d(-width + 5, -5);
          }
          public static void DrawTitles()
          {
               Format curFormat = GetDataFromUser();
               if (curFormat.formatID == "A3" && curFormat.orientation == "Portrait" && curFormat.type == "Title")
               {
                    height = listOfFormats [1].height;
                    width = listOfFormats [1].width;
               }
               else if (curFormat.formatID == "A3" && curFormat.orientation == "Landscape" && curFormat.type == "Title")
               {
                    height = listOfFormats [1].width;
                    width = listOfFormats [1].height;
               }

          }

Использую код приведенный выше, но он почему то не присваивает значения height и width. В отладчике в переменные keyPoint 1-4, приходит 0. В чем я неправ?

Answer 1

Потому что Вы не вызываете конструктор FormatsHandler.

height и width назначаются в DrawTitles, и только при определенных условиях. Это должно произойти до вызова конструктора FormatsHandler. Это происходит?

READ ALSO
Запрос ввода из нового окна

Запрос ввода из нового окна

Как в Windows Forms организовать ввод данных из нового окна? При этом, нужно, чтобы введенные данные после нажатия кнопки, допустим "ОК", выводились...

197
Хранение бд в папке с программой

Хранение бд в папке с программой

Появилась необходимость переносной проги, без использования Managment StudioВроде как создал локальную бд с типом

187
Построение сети Петри

Построение сети Петри

Есть проблема с графическим отображением сети ПетриЕсть ли какая-нибудь открытая библиотека, которая может решить данную проблему(интересует...

208
Вложеный запрос mysql

Вложеный запрос mysql

Подскажите как сделать вложеный запрос, так что бы после обновления

178