Results 1 to 5 of 5

Thread: [RESOLVED] Implementing and Instantiating an Abstract Class

  1. #1

    Thread Starter
    Fanatic Member eSPiYa's Avatar
    Join Date
    Jun 2006
    Location
    in our house
    Posts
    751

    Resolved [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:
    1. public abstract class AuthData
    2.     {
    3.         private string _VoidCode;
    4.         private string _SessionID;
    5.         private DateTime _SessionExp;
    6.         private int _SessionHours;
    7.                
    8.         public abstract string VoidCode
    9.         {
    10.             get;
    11.             set;
    12.         }
    13.  
    14.         public abstract string SessionID
    15.         {
    16.             get;
    17.             set;
    18.         }
    19.  
    20.         public abstract DateTime SessionExp
    21.         {
    22.             get;
    23.             set;
    24.         }
    25.  
    26.         public abstract Int32 SessionHours
    27.         {
    28.             get;
    29.             set;
    30.         }
    31.     }

    And this is my implementation with Constructor Code:
    1. public class AuthDataConst : AuthData
    2.     {
    3.         private string _VoidCode;
    4.         private string _SessionID;
    5.         private DateTime _SessionExp;
    6.         private int _SessionHours;
    7.  
    8.        
    9.         public AuthDataConst(string voidcode, string sessionid, DateTime sessionexp, int sessionhrs)
    10.         {            
    11.             string _VoidCode = voidcode;
    12.             string _SessionID = sessionid;
    13.             DateTime _SessionExp = sessionexp;
    14.             int _SessionHours = sessionhrs;            
    15.         }
    16.        
    17.         public override string VoidCode
    18.         {
    19.             get
    20.             {
    21.                 return this._VoidCode;
    22.             }
    23.             set
    24.             {
    25.                 this._VoidCode = this.VoidCode;
    26.             }
    27.         }
    28.  
    29.         public override string SessionID
    30.         {
    31.             get
    32.             {
    33.                 return this._SessionID;
    34.             }
    35.             set
    36.             {
    37.                 this._SessionID = this.SessionID;
    38.             }
    39.         }
    40.  
    41.         public override DateTime SessionExp
    42.         {
    43.             get
    44.             {
    45.                 return this._SessionExp;
    46.             }
    47.             set
    48.             {
    49.                 this._SessionExp = this.SessionExp;
    50.             }
    51.         }
    52.  
    53.         public override int SessionHours
    54.         {
    55.             get
    56.             {
    57.                 return this._SessionHours;
    58.             }
    59.             set
    60.             {
    61.                 this._SessionHours = this.SessionHours;
    62.             }
    63.         }
    64.     }

    Then I tried to make a new instance but its properties still returns null values. Code:
    1. 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:
    1. result.VoidCode = "0000";
    2.             result.SessionID = "1234567890";
    3.             result.SessionExp = DateTime.Now.AddHours(5);
    4.             result.SessionHours = 5;

  2. #2
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611

    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;
            }
    VB6 & C# (WCF LINQ) mostly


    If you need help with a WPF/WCF question post in the NEW WPF & WCF forum and we will try help the best we can

    My site

    My blog, couding troubles and solutions

    Free online tools

  3. #3

    Thread Starter
    Fanatic Member eSPiYa's Avatar
    Join Date
    Jun 2006
    Location
    in our house
    Posts
    751

    Re: Implementing and Instantiating an Abstract Class

    Thanks.
    I'll re-configure my code.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member eSPiYa's Avatar
    Join Date
    Jun 2006
    Location
    in our house
    Posts
    751

    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:
    1. string _VoidCode = voidcode;
    2.             string _SessionID = sessionid;
    3.             DateTime _SessionExp = sessionexp;
    4.             int _SessionHours = sessionhrs;

Posting Permissions

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



Click Here to Expand Forum to Full Width