Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4007642
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:39:24+00:00 2026-05-20T08:39:24+00:00

I have a question regarding inheritance, so I will describe the scenario below: I

  • 0

I have a question regarding inheritance, so I will describe the scenario below:

I am reading a text file containing logs. (One log per line)
Each log-line will have the following format:
“Date Type Description”

However, depending on the “Type” of log, I will have to parse the “Description” differently and pull out different fields.

Here are some examples:

5/1/2011 Information Field1, Field2, Field3
5/2/2011 Error       Field1

—
So, what I tried to do was this:
-Get a line out of the log
-Parse it according to the pattern “Date Type Description”
-Look at the “Type” field, and create new objects/parse description as necessary

public class Log
{
   public DateTime Date;
   public String Type;
   public String Description;

   public Log(String line)
   {
      this.Date = GetDate();
      this.Type = GetType();
      this.Description = GetDescription();
   }
}

public class InformationLog : Log
{
   public String Field1;
   public String Field2;
   public String Field3;

   public InformationLog(Log log)
   {
      this.Field1 = GetField1(log.Description);
      this.Field1 = GetField2(log.Description);
      this.Field1 = GetField3(log.Description);
   }
}

public class Client
{
   public void Main()
   {
       String line = ReadFileAndGetLine();  // Get a line from the file
       Log log = new Log(line);
       if(log.Type == "Information")
          log = new InformationLog(log);    // Is this right?
   }
}

This works how I want it to, but it seems like this cannot be a good practice. The “log” variable is using itself as a parameter to its own constructor.

My question is:
Is there a standard way of doing this? Or, is there anything wrong with this implemenation?

—
Edit:
Also, I should mention: My reasoning was that I would parse the line once to get out the date and type, and then parse it again to get the finer details.
I decided to use inheritance so I wouldn’t have to parse out the Date and Type fields twice.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T08:39:24+00:00Added an answer on May 20, 2026 at 8:39 am

    As per my comment, why not just do something a little like this:

        public enum LogEntryType
        {
            Error = -1,
            Information = 0,
        }
    
        public class LogEntry
        {
            public string Raw;
            public DateTime Date;
            public LogEntryType Type;
            public string Description;
    
            public LogEntry(String line)
            {
                Raw = line;
                Date = ParseDate();
                Type = ParseType();
                Description = ParseDescription();
            }
    
            public string ParseDescription()
            {
               var result = string.Empty;
               switch(Type)
               {
                   case LogEntryType.Error:
                       //parse here
                       break;
                   case LogEntryType.Information:
                       //parse here
                       break;
               }
               return result;
            }
        }
    

    I notice you have fields in the derivative class, but the description could be parsed here; though, I can see why people may want to shift it to the place that actually knows how the description should be parsed, in which case you could use a factory pattern suggested in another answer, or implement a ‘property bag’ type scenario – but drifting away from strong typing is generally frowned upon these days, I reckon.

    Another suggestion, though very similar to your initial attempt, tends to encapsulate management of the types, as opposed to having a detached class handle such stuff – a pattern a little (superficially) like Exception where you have a root entry and inner entries:

        public enum LogEntryType
        {
            Error = -1,
            Information = 0,
        }
    
        public class LogEntry
        {
            public string Raw;
            public DateTime Date;
            public LogEntryType Type;
            public string Description;
    
            public InnerLogEntry InnerEntry;
    
            public LogEntry(String line)
            {
                Raw = line;
                Date = ParseDate();
                Type = ParseType();
                //parse the 'raw' description...
                Description = ParseDescription();
                //determine the inner entry type...
                switch (Type)
                {
                    case LogEntryType.Error:
                        InnerEntry = new ErrorLogEntry(this);
                        break;
                    case LogEntryType.Information:
                        InnerEntry = new InformationLogEntry(this);
                        break;
                }                
            }
        }
    
        public abstract class InnerLogEntry
        {
            protected LogEntry Parent;
    
            public InnerLogEntry(LogEntry logEntry)
            {
                Parent = logEntry;
            }
        }
    
        public class InformationLogEntry : InnerLogEntry
        {
            public InformationLogEntry(LogEntry logEntry)
                : base(logEntry)
            {
                //parse custom data
            }
        }
    
        public class ErrorLogEntry : InnerLogEntry
        {
            public ErrorLogEntry(LogEntry logEntry)
                : base(logEntry)
            {
                //parse custom data
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question regarding a platform I'm developing called e-cidadania (GPL). One of
I have a question regarding how to use the formula property to be used
I have a question regarding the execution of the following program. #include<stdio.h> int main(void)
I have a question regarding a session variable. I have session variable which needs
I have a pretty simple Rails question regarding encoding that I can't find an
This is a design question regarding an MVC implementation. I am creating a 2D
I'm using CodeIgniter for an application that I'm writing and I've got a question
Based on this tutorial I have built a page which functions correctly. I added
I have Unity 2.0 working well within the App.xaml.cs to register and resolve within

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.