2014-08-31 33 views
1

我在存儲來自我的抽象類的屬性時存在一些問題,構造函數似乎工作得很好。不過,我無法將基本屬性存儲在我的子類數據庫表中。在子類db-table中存儲基本抽象類屬性

public abstract class Vehicle : IComparable<Vehicle>, IComparable { 
    public Int16 VehicleID; 
    public DateTime ProductionDate; 

    public Vehicle(Int16 _ Vehicle ID,DateTime _ProductionDate) 
    { 
     this.AccidentID = _ AccidentID; 
     this.ProductionDate = _ProductionDate; 
    } 

    int IComparable.CompareTo(object other) { 
     return CompareTo((Vehicle)other); 
    } 
    public int CompareTo(Vehicle other){ 
     return this.ProductionDate.CompareTo(other.ProductionDate); 
    } 

    public Vehicle() 
    {} 
} 

public class Car : Vehicle 
{ 
    public Car() 
    { 
    } 

    public Car (Int16 _VehicleID,DateTime _ProductionDate, Int16 _CarAttribute1, Int16 _CarAttribute2):base(_Vehicle ID,_ProductionDate) 
    { 
     this.AccidentID = _ AccidentID; 
     this.ProductionDate = _ProductionDate; 
     this.CarAttribute1 = _CarAttribute1 
     this.CarAttribute2 = _CarAttribute2 

    } 

    [PrimaryKey, AutoIncrement, Column("Attribute1")] 
    public Int16 CarAttribute1{ get; set;} 
    [Column("Attribute2")] 
    public Int16 CarAttribute2{ get; set;} 
} 

我對C#很新,所以一些指導讚賞:)我錯過了什麼?

+2

1.您使用哪種框架來使用數據庫? 2.你有什麼問題與你的代碼?異常,數據庫中的錯誤或NULL數據? – ntl 2014-08-31 21:00:43

+0

嗨,我使用單聲道和Xamarin IDE,我使用的組件稱爲SQLite.net異步(https://www.nuget.org/packages/SQLite.Net.Async-PCL/)基類屬性爲null在我的數據庫列。 – RaddyMcKey 2014-09-01 05:49:12

回答

0

在基類,你應該使用屬性而不是字段,因此調整的基礎類是這樣的:

public abstract class Vehicle : IComparable<Vehicle>, IComparable { 

public Int16 AccidentID { get; set; } 
public DateTime ProductionDate { get; set;} 

public Vehicle(Int16 _ Vehicle ID,DateTime _ProductionDate) 
{ 
    this.AccidentID = _ AccidentID; 
    this.ProductionDate = _ProductionDate; 
} 

int IComparable.CompareTo(object other) { 
    return CompareTo((Vehicle)other); 
} 
public int CompareTo(Vehicle other){ 
    return this.ProductionDate.CompareTo(other.ProductionDate); 
} 

public Vehicle() 
{} 

}

,所以我改變:

public Int16 VehicleID; 
public DateTime ProductionDate; 

到:

public Int16 AccidentID { get; set; } 
public DateTime ProductionDate { get; set;} 

BTW:您在基類中有VehicleID字段,但在構造函數中,您將值設置爲AccidentID而不是VehicleID。我認爲這只是描述中的一個錯字,對吧?所以我用AccidentID作爲屬性名稱,所以請檢查它是否正確。

+0

1.是的,只是一個錯字:) 2.非常感謝,解決了我的問題! – RaddyMcKey 2014-09-01 06:17:56

+0

很高興爲您效力! – ntl 2014-09-01 06:26:30