Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    C# Problem with my controller and Form

    Hi,

    I got some problem to initialize my Controller-class in my Form-class. It works fine in my console application but I get this error when I try to use it in my Form-application
    Code:
    A field initializer cannot reference the non-static field, method, or property 'Adb.Form1.hanteraKund'
    I Understand what the error means, but I can't figure out to work around it.

    This the beginning of my Form-class
    Code:
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace Adb
    {
        public partial class Form1 : Form
        {
            private HanteraKund hanteraKund = new HanteraKund();
            Controller controller = new Controller(hanteraKund);
    My Controller
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.SqlClient;
    
    
    namespace Adb
    {
        class Controller
        {
            DBConnection Connection = new DBConnection();
            HanteraKund hanteraKund = new HanteraKund();
    
            public Controller (HanteraKund hanteraKund)
            {
                this.hanteraKund = hanteraKund;
            }
     
            public void laggTillKund(int kundNr, string orgNr, string kundNamn, string     kundAdress, string kundPostNr, string kundOrt, string kundMail, string kundTele)
            {
                hanteraKund.LaggTillKund(kundNr, orgNr, kundNamn, kundAdress, kundPostNr, kundOrt, kundMail, kundTele);
            }
    
            public void taBortKund(int kundNr)
            {
                hanteraKund.taBortKund(kundNr);
            }
       
            public List<Kund> allaKunder()
            {
                List<Kund> kunder = hanteraKund.allaKunder();
                return kunder;
            }
    
            public string[] hittaKund(int kundNr)
            {
                string[] tmpKund = hanteraKund.hittaKund(kundNr);
                return tmpKund;
            }
    
            public void uppdateraKund(int kundNr, string orgNr, string kundNamn, string kundAdress, string kundPostNr, string kundOrt, string kundMail, string kundTele)
            {
                hanteraKund.uppdateraKund(kundNr, orgNr, kundNamn, kundAdress, kundPostNr, kundOrt, kundMail, kundTele);
            }
            
        }
    }
    And finally my Class with methods
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.SqlClient;
    
    namespace Adb
    {
        class HanteraKund
        {
            DBConnection Connection = new DBConnection();
    
            public void LaggTillKund(int kundNr, string orgNr, string kundNamn, string kundAdress, string kundPostNr, string kundOrt, string kundMail, string kundTele)
            {
                SqlConnection connect = Connection.Connect();
                SqlCommand command = new SqlCommand("insert into kund values('" + kundNr + "','" + orgNr + "','" + kundNamn + "','" + kundAdress + "','" + kundPostNr + "','" + kundOrt + "','" + kundMail + "','" + kundTele + "');", connect);
                connect.Open();
                try
                {
                    SqlDataReader reader = command.ExecuteReader();
                    reader.Close();
                    connect.Close();
                }
                catch
                {
                    connect.Close();
                    Console.WriteLine("fonkar inte");
                }
            }
    
            public void TaBortKund(int kundNr)
            {
                SqlConnection connect = Connection.Connect();
                SqlCommand command = new SqlCommand("delete from kund where kundNr = '" + kundNr + "'", connect);
                connect.Open();
                try
                {
                    SqlDataReader reader = command.ExecuteReader();
                    reader.Close();
                    connect.Close();
                }
                catch
                {
                    connect.Close();
                    Console.WriteLine("fonkar inte");
                }
            }
    
            public List<Kund> AllaKunder()
            {
                SqlConnection connect = Connection.Connect();
                SqlCommand command = new SqlCommand("select * from kund", connect);
                connect.Open();
                try
                {
                    SqlDataReader reader = command.ExecuteReader();
                    List<Kund> kunder = new List<Kund>();
    
                    while (reader.Read())
                    {
                        kunder.Add(new Kund(int.Parse(reader[0].ToString()), reader[1].ToString(), reader[2].ToString(), reader[3].ToString(), reader[4].ToString(), reader[5].ToString(), reader[6].ToString(), reader[7].ToString()));
                    }
                    reader.Close();
                    connect.Close();
                    return kunder;
                }
                catch
                {
                    connect.Close();
                    Console.WriteLine("fonkar inte");
                    return null;
                }
            }
    
            public string[] HittaKund(int kundNr)
            {
                SqlConnection connect = Connection.Connect();
                SqlCommand command = new SqlCommand("select * from kund where kundNr = '" + kundNr + "'", connect);
                connect.Open();
                try
                {
                    SqlDataReader reader = command.ExecuteReader();
                    string[] kund = new string[8];
    
                    while (reader.Read())
                    {
                        kund[0] = reader[0].ToString();
                        kund[1] = reader[1].ToString();
                        kund[2] = reader[2].ToString();
                        kund[3] = reader[3].ToString();
                        kund[4] = reader[4].ToString();
                        kund[5] = reader[5].ToString();
                        kund[6] = reader[6].ToString();
                        kund[7] = reader[7].ToString();
                    }
    
                    reader.Close();
                    connect.Close();
                    return kund;
                }
                catch
                {
                    connect.Close();
                    Console.WriteLine("fonkar inte");
                    return null;
                }
            }
            public void UppdateraKund(int kundNr, string orgNr, string kundNamn, string kundAdress, string kundPostNr, string kundOrt, string kundMail, string kundTele)
            {
                SqlConnection connect = Connection.Connect();
                SqlCommand command = new SqlCommand("update kund set kundnr='" + kundNr + "', korgnr='" + orgNr + "', knamn='" + kundNamn + "',kadress='" + kundAdress + "',kpostnr='" + kundPostNr + "',kort='" + kundOrt + "',kmail='" + kundMail + "',ktel='" + kundTele + "' where kundnr='" + kundNr + "'", connect);
                connect.Open();
                try
                {
                    SqlDataReader reader = command.ExecuteReader();
                    reader.Close();
                    connect.Close();
                }
                catch
                {
                    connect.Close();
                    Console.WriteLine("fonkar inte");
                }
            }
        }
    }
    Any thoughts would be much appreciated

  • #2
    Regular Coder bobleny's Avatar
    Join Date
    May 2007
    Posts
    258
    Thanks
    3
    Thanked 11 Times in 11 Posts
    Is the compiler giving you a line number?

    Have you tried putting your controller initialization within the Form1's constructor.
    Code:
    namespace Adb
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                private HanteraKund hanteraKund = new HanteraKund();
                Controller controller = new Controller(hanteraKund);
            }
    ...
    It is my understanding that you are trying to initialize Controller while initializing Form1, which could cause unexpected results. The fix would be to initialize Controller after Form1 has been initialized.
    Last edited by bobleny; 11-03-2011 at 04:09 PM.
    --www.firemelt.net--
    * No good deed goes unpunished.
    * Cheer up, the worst has yet to come...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •