[RESOLVED] Implementing and Instantiating an Abstract Class
I'm currently having problem with creating an abstract class, implementing it, and making a new instance.
Even though I've instantiated it, and set values, its properties still return to null.
This is my abstract class Code:
public abstract class AuthData
{
private string _VoidCode;
private string _SessionID;
private DateTime _SessionExp;
private int _SessionHours;
public abstract string VoidCode
{
get;
set;
}
public abstract string SessionID
{
get;
set;
}
public abstract DateTime SessionExp
{
get;
set;
}
public abstract Int32 SessionHours
{
get;
set;
}
}
And this is my implementation with Constructor Code:
public class AuthDataConst : AuthData
{
private string _VoidCode;
private string _SessionID;
private DateTime _SessionExp;
private int _SessionHours;
public AuthDataConst(string voidcode, string sessionid, DateTime sessionexp, int sessionhrs)
{
string _VoidCode = voidcode;
string _SessionID = sessionid;
DateTime _SessionExp = sessionexp;
int _SessionHours = sessionhrs;
}
public override string VoidCode
{
get
{
return this._VoidCode;
}
set
{
this._VoidCode = this.VoidCode;
}
}
public override string SessionID
{
get
{
return this._SessionID;
}
set
{
this._SessionID = this.SessionID;
}
}
public override DateTime SessionExp
{
get
{
return this._SessionExp;
}
set
{
this._SessionExp = this.SessionExp;
}
}
public override int SessionHours
{
get
{
return this._SessionHours;
}
set
{
this._SessionHours = this.SessionHours;
}
}
}
Then I tried to make a new instance but its properties still returns null values. Code:
AuthDataConst result = new AuthDataConst("0000", "1234567890", DateTime.Now.AddHours(5), 5);
Even manually setting their values returns same result; all properties are still null. Code:
result.VoidCode = "0000";
result.SessionID = "1234567890";
result.SessionExp = DateTime.Now.AddHours(5);
result.SessionHours = 5;
Re: Implementing and Instantiating an Abstract Class
There are 2 mistakes in the code:
This:
Code:
private string _VoidCode;
private string _SessionID;
private DateTime _SessionExp;
private int _SessionHours;
public AuthDataConst(string voidcode, string sessionid, DateTime sessionexp, int sessionhrs)
{
string _VoidCode = voidcode;
string _SessionID = sessionid;
DateTime _SessionExp = sessionexp;
int _SessionHours = sessionhrs;
}
Should be:
Code:
private string _VoidCode;
private string _SessionID;
private DateTime _SessionExp;
private int _SessionHours;
public AuthDataConst(string voidcode, string sessionid, DateTime sessionexp, int sessionhrs)
{
this._VoidCode = voidcode;
this._SessionID = sessionid;
this._SessionExp = sessionexp;
this._SessionHours = sessionhrs;
}
And the property get/set should look like this:
Code:
public override string VoidCode
{
get;
set;
}
Re: Implementing and Instantiating an Abstract Class
Thanks.
I'll re-configure my code.
Re: Implementing and Instantiating an Abstract Class
Your code doesn't make sense. Why do you have private members in your abstract class that are never used, then implementations in your concrete class that do nothing that couldn't have been done in the base class? Why not just have the properties in the base class backed by those private fields in the base class? Then there's no need for those properties to be abstract at all. Furthermore, as you're using C# 3.0 you don't need backing fields at all. You can just declare auto-properties in the base class and then there's no point or need inheriting it at all.
Re: Implementing and Instantiating an Abstract Class
Thanks.
It works.
I'm sorry if there are mistakes with my code, I've left .NET programming for more than a year.
And this is my first time using abstract, I've just used Struct and Enumerate before.
Maybe I've just Googled old codes.
About this part, I've just copy-paste it then forgot to remove the data types. :p Code:
string _VoidCode = voidcode;
string _SessionID = sessionid;
DateTime _SessionExp = sessionexp;
int _SessionHours = sessionhrs;