PDA

View Full Version : C# Problem with my controller and Form



Mecklenburger
11-02-2011, 01:16 AM
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

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

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


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

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

bobleny
11-03-2011, 04:58 PM
Is the compiler giving you a line number?

Have you tried putting your controller initialization within the Form1's constructor.


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.