[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

C# variabel type problem på tværs af klasser



Den kryptiske emnelinie viser nok det faktum at jeg er rimeligt ny i
objektorienteret programmering og ikke mindst C#

I forbindelse med at lære brugen af klasser har jeg lavet et lille
program, som består af 2 klasser.

class spusDB - et database objekt der kan hente nogle data fra min
PostgreSQL server
I spusDB har jeg en ArrayList som indeholder nogle resultater fra
databasen. I denne ArrayList er hvert objekt en struktur med to
strenge. ArrayListen er "public" og kan derfor læses fra andre klasser
(dårlig objektorientering... ved det godt - men jeg er amatørnybegynder)

Da jeg fra en anden klasse skal udlæse dataene fra den af spusDB
genererede ArrayList løber jeg ind i et problem.
Jeg forsøger at gå igennem ArrayList med 

    foreach (EmployeeType emp in spusAccess.employeeList)
      Console.WriteLine("Id: {0}\tName: {1}", emp.id, emp.name);

Får jeg følgende runtime fejl :

Unhandled Exception: System.InvalidCastException: Cannot cast from
source type to destination type. at MyProg.Main (System.String[] args)
[0x00000]

Dette fungerer dog udemærket hvis min løkke kaldes fra en metode i
spusDB klassen.

Hvis main() flyttes fra MyProg til spusDB virker det som det skal.


--------------------------------------------------
Her er mit komplette eksempel (er ikke kønt):
--------------------------------------------------
using System;
using System.Data;
using System.Collections;
using Npgsql;

public class MyProg
{
  public struct EmployeeType
  {
    public string id, name;
  }

  public static void Main(string[] args)
  {
    string connectionString =
      "Server=rhea.solnet;" +
      "Database=spus;" +
      "User ID=jkp;" +
      "Password=******;"; // Password er ændret for at beskytte de
uskyldige

    spusDB spusAccess = new spusDB();

    int dbConRes = spusAccess.InitConnection(connectionString);

    if (dbConRes == 1) {
      Console.WriteLine("Could not connect to database error!\nError :
{0}", spusAccess.GetLastErrorMSG()); return;
    }

    spusAccess.FillEmployee();

    Console.WriteLine("Count: " + spusAccess.employeeList.Count);
    foreach (EmployeeType emp in spusAccess.employeeList)
      Console.WriteLine("Id: {0}\t\tName: {1}", emp.id, emp.name);
      
    spusAccess.CloseConnection();
  }
}
  

public class spusDB
{
  private IDbConnection dbcon;
  private string lastErrorMsg="";

  private struct EmployeeType
  {
    public string id, name;
  }
			  
  private EmployeeType employee;
  public ArrayList employeeList = new ArrayList();


  public void FillEmployee()
  {
    IDbCommand dbcmd = dbcon.CreateCommand();

    string sql = "SELECT id, name FROM employee order by id";
    dbcmd.CommandText = sql;
    IDataReader reader = dbcmd.ExecuteReader();
    while(reader.Read()) {
      employee.id = (string) reader["id"];
      employee.name = (string) reader["name"];
      employeeList.Add(employee);
    }

    reader.Close();
    reader = null;
    dbcmd.Dispose();
    dbcmd = null;
  }
  
  public string GetLastErrorMSG()
  {
    return lastErrorMsg;
  }
  
  public void CloseConnection()
  {
    dbcon.Close();
    dbcon = null;
  }
  
  public int InitConnection(string connString)
  {
    try
      {
	dbcon = new NpgsqlConnection(connString);
	dbcon.Open();
      }
    catch (NpgsqlException dbException)
      {
	lastErrorMsg = dbException.BaseMessage;
	return 1;
      }
    return 0;
  }
}


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2007-10-01, 02:01 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *