Standardkonstruktor



  • Hi,

    ich möchte meine Frage etwas präzesieren:

    muss der Standardkonstruktor nun geschrieben werden, wenn ich noch andere Objekte erstelle?

    Ich versteh das mit den Konstruktoren noch nicht ganz so..



  • Welche anderen Objekte? 😕

    Wovon redest Du? Was ist die Frage?



  • @C#-Tester
    Deine "präzisierte" Frage ist vollkommen unverständlich.

    Und die "nicht-präzisierte" Variante (=der Thread-Titel?) ist keine Frage sondern einfach nur ein Wort.



  • using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace AutomobilBeispiel
    {
    public class Automobil
    {
    //Attribute
    public int kilometerstand = 0;
    public double verbrauch = 0; //Verbrauch = 8,3l/100km
    public double tankinhalt = 45;
    public string autoname;
    public string art;

    //Methoden

    public void tanken(double getankteMenge) //Berechnet Tankfüllung für eingegebene Tankmenge
    {
    tankinhalt = tankinhalt + getankteMenge;
    }

    public void fahren(int wegstrecke) //Berechnet Kilometerstand und Tankfüllung für eingegebene Wegstrecke
    {
    kilometerstand = kilometerstand + wegstrecke;
    tankinhalt = tankinhalt - (wegstrecke * (verbrauch / 100));
    }

    public void zuweit(int wegstrecke) //Prüft, ob Wegstrecke zu groß ist für Tankfüllung
    {
    kilometerstand = kilometerstand - wegstrecke;
    tankinhalt = tankinhalt + (wegstrecke * (verbrauch/100));
    }

    //Konstruktoren

    public Automobil(string startautoname, int startkilometerstand, double startverbrauch, double starttankinhalt)
    {
    autoname = startautoname;
    kilometerstand = startkilometerstand;
    verbrauch = startverbrauch;
    tankinhalt = starttankinhalt;
    }

    public Automobil()
    {
    }

    }
    }



  • Google: http://msdn.microsoft.com/en-us/library/vstudio/ms173115.aspx

    Constructors are class methods that are executed when an object of a given type is created. Constructors have the same name as the class, and usually initialize the data members of the new object.

    Das sollte doch jeder verstehen können, oder hapert es bereits daran?

    A constructor that takes no parameters is called a default constructor. Default constructors are invoked whenever an object is instantiated by using the new operator and no arguments are provided to new

    Damit ist erklärt woran man einen Default Constructor erkennt und wann er benutzt wird.

    Google: http://msdn.microsoft.com/en-us/library/vstudio/k6sa6h87.aspx

    If a class does not have a constructor, a default constructor is automatically generated and default values are used to initialize the object fields.

    Und das sollte doch vollständig die Frage beantworten ob man einen default-CTor anlegen muss.

    C#-Tester schrieb:

    muss der Standardkonstruktor nun geschrieben werden, wenn ich noch andere Objekte erstelle?

    Genaugenommen mußt Du überhaupt keinen CTor ertsllen wenn Du "Andere" Objekte erstellst, weil der CTor nur Objekte der eigenen Klasse betrifft. Jaja, Du hast das anders gemeint, aber am Ende zählt nicht was Du bei der Frage meinst, sondern was Du tatsächlich schreibst.



  • //Autor: Felix Walter
    //Datum: 19.11.2012
    //Kurzbeschreibung: Dieses Programm erstellt eine Tabelle als Vorbereitung für ACLs

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;

    namespace WindowsFormsApplication1
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

    private void btn_Hinzufügen_Click(object sender, EventArgs e)
    {
    // Variablen deklarieren um sie später in die Listbox zu schreiben
    string x=" ";
    string y=" ";
    string z=" ";
    int a = 0;

    try //Fehler in Radioboxen abfangen
    {
    //Variablen mit Text aus den Radiobuttons füllen

    if(rbn_Deny.Checked == Enabled)
    {
    x = "Deny";
    }

    else
    {
    x = "Permit";
    }

    if(rbn_TCP.Checked == Enabled)
    {
    y = "TCP";
    }

    if(rbn_UDP.Checked == Enabled)
    {
    y = "UDP";
    }

    if(rbn_IP.Checked == Enabled)
    {
    y = "IP";
    }

    if(rbn_IN.Checked == Enabled)
    {
    z = "IN";
    }

    if(rbn_OUT.Checked == Enabled)
    {
    z = "OUT";
    }

    if(rbn_INOUT.Checked == Enabled)
    {
    z= "IN/OUT";
    }
    }

    catch
    {
    MessageBox.Show ("Bitte füllen Sie alle Radioboxen aus!", "Formfehler"); //Messagebox bei Fehler
    }

    //Fehler in Textboxen abfangen

    if (Convert.ToInt32(this.tbx_ZielPort.Text) > 1024 || this.tbx_QuellPort.Text != "Any")
    {
    MessageBox.Show("Bitte geben Sie als Quellport nur die Standardports oder ANY an!", "Formfehler"); //Messagebox bei Fehler
    a = 1;
    }

    if (Convert.ToInt32(this.tbx_ZielPort.Text) > 1024 || this.tbx_QuellPort.Text != "Any")
    {
    MessageBox.Show("Bitte geben Sie als Zielport nur die Standardports oder ANY an!", "Formfehler"); //Messagebox bei Fehler
    a = 1;
    }

    if (a == 0)
    {
    //Text aus Textboxen und Variablen in die Listbox schreiben
    this.listBox1.Items.Add(x + " " + y + " " + this.tbx_QuellIP.Text + " " + this.tbx_ZielIP.Text + " " + this.tbx_QuellPort.Text + " " + this.tbx_ZielPort.Text + " " + this.tbx_Interface.Text + " " + z);
    }
    else
    {
    MessageBox.Show("Bitte korrigieren Sie alle Fehler!", "Fehler");
    a = 0;
    }

    }
    }
    }



  • C#- Tester schrieb:

    //Autor: Felix Walter
    //Datum: 19.11.2012
    //Kurzbeschreibung: Dieses Programm erstellt eine Tabelle als Vorbereitung für ACLs
    
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void btn_Hinzufügen_Click(object sender, EventArgs e)
            {
                // Variablen deklarieren um sie später in die Listbox zu schreiben
                string x=" ";
                string y=" ";
                string z=" ";
                int a = 0;
    
            
               
    
               try //Fehler in Radioboxen abfangen
               {
                   //Variablen mit Text aus den Radiobuttons füllen
    
                   if(rbn_Deny.Checked == Enabled)
                   {
                       x = "Deny";
                   }
    
                   else
                   {
                       x = "Permit";
                   }
    
                   if(rbn_TCP.Checked == Enabled)
                   {
                       y = "TCP";
                   }
    
                   if(rbn_UDP.Checked == Enabled)
                   {
                       y = "UDP";
                   }
    
                   if(rbn_IP.Checked == Enabled)
                   {
                       y = "IP";
                   }
    
                   if(rbn_IN.Checked == Enabled)
                   {
                       z = "IN";
                   }
    
                   if(rbn_OUT.Checked == Enabled)
                   {
                       z = "OUT";
                   }
    
                   if(rbn_INOUT.Checked == Enabled)
                   {
                       z= "IN/OUT";
                   }
              }
            
            catch
               {
                MessageBox.Show ("Bitte füllen Sie alle Radioboxen aus!", "Formfehler"); //Messagebox bei Fehler
               }
    
               //Fehler in Textboxen abfangen
    
               if (Convert.ToInt32(this.tbx_ZielPort.Text) > 1024 || this.tbx_QuellPort.Text != "Any")
               {
                   MessageBox.Show("Bitte geben Sie als Quellport nur die Standardports oder ANY an!", "Formfehler"); //Messagebox bei Fehler
                   a = 1;
               }
    
               if (Convert.ToInt32(this.tbx_ZielPort.Text) > 1024 || this.tbx_QuellPort.Text != "Any")
               {
                   MessageBox.Show("Bitte geben Sie als Zielport nur die Standardports oder ANY an!", "Formfehler"); //Messagebox bei Fehler
                   a = 1;
               }
    
               if (a == 0)
               {
                   //Text aus Textboxen und Variablen in die Listbox schreiben
                   this.listBox1.Items.Add(x + "   " + y + "   " + this.tbx_QuellIP.Text + "   " + this.tbx_ZielIP.Text + "   " + this.tbx_QuellPort.Text + "   " + this.tbx_ZielPort.Text + "   " + this.tbx_Interface.Text + "   " + z);
               }
               else
               {
                   MessageBox.Show("Bitte korrigieren Sie alle Fehler!", "Fehler");
                   a = 0;
               }
            
            }
        }
    }
    

    1. Bitte verwende Codetags
    2. Was willst du uns mit dem Code sagen? Es fehlt eine Frage etc.


Anmelden zum Antworten