03 Klassen


01 String
using System;
using System.Windows.Forms;
using Eplan.EplApi.Scripting;

public class _03_Klassen_01_String
{
  [Start]
  public void Function()
  {
    MessageBox.Show("Ich bin ein Text (aber eigentlich ein String)!");

    string message1 = string.Empty;
    message1 = "Ich bin ein String mit\nZeilenumbruch!";
    MessageBox.Show(message1);
    MessageBox.Show("Ich" + Environment.NewLine + "auch!");

    string message2 = "Ich bin auch ein String!";
    MessageBox.Show(message2);
    message2 = "Mir kann man auch einen neuen Text übergeben!";
    MessageBox.Show(message2);

    string message3_1 = "Und ich ";
    string message3_2 = "bin auch ";
    string message3_3 = "einer!";
    MessageBox.Show(message3_1 + message3_2 + message3_3);

    MessageBox.Show("Zeilenumbruch im Code "
                    + "wird nicht angezeigt!");

    string message4 = "Der {0} ist im {1}.";
    string message4_1 = string.Format(message4, "Kamm", "Schrank");
    string message4_2 = string.Format(message4, "Schrank", "Badezimmer");
    MessageBox.Show(message4_1);
    MessageBox.Show(message4_2);
  }
}


02 String Pfadvariable
using System.Windows.Forms;
using Eplan.EplApi.Base;
using Eplan.EplApi.Scripting;

public class _03_Klassen_02_String_Pfadvariable
{
  [Start]
  public void Function()
  {
    string projectName = PathMap.SubstitutePath("$(PROJECTNAME)");
    MessageBox.Show(projectName);
  }
}


03 Integer
using System.Windows.Forms;
using Eplan.EplApi.Scripting;

public class _03_Klassen_03_Integer
{
  [Start]
  public void Function()
  {
    int result;
    int number1 = 6;
    int number2 = 3;

    MessageBox.Show(number1.ToString());

    result = number1 + number2;
    MessageBox.Show(result.ToString());

    result = number1 - number2;
    MessageBox.Show(result.ToString());

    result = number1 * number2;
    MessageBox.Show(result.ToString());

    result = number1 / number2;
    MessageBox.Show(result.ToString());
  }
}


04 Fehler Integer
using System.Windows.Forms;
using Eplan.EplApi.Scripting;

public class _03_Klassen_04_Fehler_Integer
{
  [Start]
  public void Function()
  {
    string number1String = "10";
    string number2String = "2";
    string resultString = number1String + number2String;
    MessageBox.Show(resultString);

    int result;
    int number1 = 10;
    int number2 = 0;
    result = number1 / number2;
    MessageBox.Show(result.ToString());
  }
}


05 Float
using System.Windows.Forms;
using Eplan.EplApi.Scripting;

public class _03_Klassen_05_Float
{
  [Start]
  public void Function()
  {
    float result;
    float number1 = 10;
    float number2 = 3;

    result = number1 / number2;
    MessageBox.Show(result.ToString());
  }
}


06 Fehler Float
using System.Windows.Forms;
using Eplan.EplApi.Scripting;

public class _03_06_Fehler_Float
{
  [Start]
  public void Function()
  {
    float result;
    result = 10 / 3;
    MessageBox.Show("10 / 3 = " + result.ToString());
  }
}


07 TryCatch
using System.Windows.Forms;
using Eplan.EplApi.Scripting;
using System;
using System.Diagnostics;

public class _03_Klassen_07_TryCatch
{
  [Start]
  public void Function()
  {
    if (Debugger.IsAttached)
    {
      Debugger.Break();
    }

    int result;
    int number1 = 10;
    int number2 = 0;

    if (Debugger.IsAttached)
    {
      Debug.WriteLine(number1 + ":" + number2);
    }

    try
    {
      result = number1 / number2;

      // Ab hier wird kein Code mehr ausgeführt
      MessageBox.Show(result.ToString());
      MessageBox.Show("Berechnung erfolgreich beendet");
    }
    catch (Exception exception)
    {
      MessageBox.Show(exception.Message);
    }
  }
}


08 BaseException
using Eplan.EplApi.ApplicationFramework;
using Eplan.EplApi.Base;
using Eplan.EplApi.Scripting;

public class _03_Klassen_08_BaseException
{
  [Start]
  public void Function()
  {
    BaseException bexAssert =
      new BaseException("Assert", MessageLevel.Assert);
    bexAssert.FixMessage();

    BaseException bexError =
      new BaseException("Error", MessageLevel.Error);
    bexError.FixMessage();

    BaseException bexFatalError =
      new BaseException("FatalError", MessageLevel.FatalError);                      
    bexFatalError.FixMessage();

    BaseException bexMessage =
      new BaseException("Message", MessageLevel.Message);
    bexMessage.FixMessage();

    BaseException bexTrace =
      new BaseException("Trace", MessageLevel.Trace);
    bexTrace.FixMessage();

    BaseException bexWarning =
      new BaseException("Warning", MessageLevel.Warning);
    bexWarning.FixMessage();

    CommandLineInterpreter cli = new CommandLineInterpreter();
    cli.Execute("SystemErrDialog");
  }
}


09 Parameter String
using System.Windows.Forms;
using Eplan.EplApi.Scripting;

public class _03_Klassen_09_Parameter_String
{
  [DeclareAction("StringParameter")]
  public void Function(string parameter)
  {
    MessageBox.Show(parameter);
  }
}


10 Parameter Integer
using System.Windows.Forms;
using Eplan.EplApi.Scripting;

public class _03_Klassen_10_Parameter_Integer
{
  [DeclareAction("IntParameter")]
  public void Function(int int1, int int2)
  {
    int result = int1 + int2;

    string message = int1.ToString() +
        " + " + int2.ToString() +
        " = " + result.ToString();

    MessageBox.Show(message);
  }
}


11 MessageBox
using System.Windows.Forms;
using Eplan.EplApi.Base;
using Eplan.EplApi.Scripting;

public class _03_Klassen_11_Messagebox
{
  [Start]
  public void Function()
  {
    string projectName = PathMap.SubstitutePath("$(PROJECTNAME)");

    MessageBox.Show("Text", projectName);

    MessageBox.Show(
        "Text",
        projectName,
        MessageBoxButtons.YesNo
        );

    MessageBox.Show(
        "Text",
        projectName,
        MessageBoxButtons.AbortRetryIgnore,
        MessageBoxIcon.Information
        );
  }
}


12 Eigene Klasse
using System.Windows.Forms;
using Eplan.EplApi.Scripting;

class _03_Klassen_12_Eigene_Klasse
{
  [Start]
  public void Function()
  {
    Person max = new Person();
    max.FirstName = "Max";
    max.LastName = "Mustermann";
    MessageBox.Show(max.FirstName + " " + max.LastName);

    Person maria = new Person("Maria", "Musterfrau");
    maria.DisplayFullName();
  }

  class Person
  {
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public string FullName
    {
      get { return FirstName + " " + LastName; }
    }

    public Person()
    {
    }

    public Person(string firstName, string lastName)
    {
      FirstName = firstName;
      LastName = lastName;
    }

    public void DisplayFullName()
    {
      MessageBox.Show(FullName);
    }
  }
}