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

  • SEARCH
  • Home
  • 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 6215475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:03:49+00:00 2026-05-24T07:03:49+00:00

I am writing an application that allows a user to run a test. A

  • 0

I am writing an application that allows a user to run a test. A test consists of a number of different objects, such as configuration, temperature, and benchmark. Settings and the like are saved back and forth between xml. I pass different XElements around in my code so I can build the final xml document differently for different situations. I wish to do something like this:

public abstract class BaseClass<T>
{
    abstract static XElement Save(List<T>);
    abstract static List<T> Load(XElement structure);
}

public class Configuration : BaseClass<Configuration>
{
    public string Property1 { get; set; }
    public string Property2 { get; set; }
    //etc...

    public static XElement Save(List<Configuration>)
    {
        XElement xRoot = new XElement("Root");
        //etc...
        return xRoot;
    }

    public static List<Configuration> Load(XElement structure)
    {
        List<BaseClass> list = new List<BaseClass>();
        //etc...
        return list;
    }
}

public class Temperature : BaseClass<Temperature>
{
    public float Value { get; set; }

    public static XElement Save(List<Temperature>)
    {
        //save
    }

    public static List<Temperature> Load(XElement structure)
    {
        //load
    }
}

[EDIT]: Revising question (Changed signatures of above functions)[/EDIT]

Of course, I am not actually allowed to override the static methods of BaseClass. What is the best way to approach this? I would like as much of the following to be valid as possible:

List<Temperature> mTemps = Temperature.Load(element);
List<Configuration> mConfigs = Configuration.Load(element);

Temperature.Save(mTemps);
Configuration.Save(mConfigs);

[EDIT]Changed intended usage code above[/EDIT]

The only solution I can think of is the following, which is NOT acceptable:

public class File
{
    public static XElement Save(List<Temperature> temps)
    {
        //save temp.Value
    }

    public static XElement Save(List<Configuration> configs)
    {
        //save config.Property1
        //save config.Property2
    }

    //etc...
}
  • 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-24T07:03:50+00:00Added an answer on May 24, 2026 at 7:03 am

    Static methods aren’t part of a class instance. So overriding them doesn’t make any sense anyway. They can’t access any nonstatic part of an instance that they happen to be a member of.

    This is kind of a strategy pattern scenario, e.g. you could just have single static Load & Save methods that check the type of object passed to them, and act accordingly. But here’s another slightly more clever way that uses generic types to create a prototype and call its method, allowing you to keep the logic within each derived object type.

    (edit again)

    Here’s another crack at it, along the same lines as my original suggestion. I actually tested this and it works, so I think this is the best you can do to get all the functionality you are looking for (other than testing types and calling code conditionally). You still need to pass a type for Load, otherwise, the runtime would have no idea what kind of return is expected. But Save works universally. And the subclass implementations are strongly typed.

    This just uses the first object in the list as its prototype, simple enough.

    public interface IBaseObject 
    {
        XmlElement Save(IEnumerable<IBaseObject> list);
        IEnumerable<IBaseObject> Load(XmlElement element);
    }
    public interface IBaseObject<T> where T: IBaseObject 
    {
        XmlElement Save(IEnumerable<T> list);
        IEnumerable<T> Load(XmlElement element);
    }
    
    public class Temperature : IBaseObject<Temperature>, IBaseObject 
    {
    
        public XmlElement Save(IEnumerable<Temperature> list)
        {
            throw new NotImplementedException("Save in Temperature was called");
        }
    
        public IEnumerable<Temperature> Load(XmlElement element)
        {
            throw new NotImplementedException("Load in Temperature was called");
        }
    
        // must implement the nongeneric interface explicitly as well
    
        XmlElement IBaseObject.Save(IEnumerable<IBaseObject> list)
        {
            return Save((IEnumerable<Temperature>)list);
        }
    
        IEnumerable<IBaseObject> IBaseObject.Load(XmlElement element)
        {
            return Load(element);
        }
    }
    
    // or whatever class you want your static methods living in
    
    public class BaseObjectFile
    {
        public static XmlElement Save(IEnumerable<IBaseObject> list)
        {
            IBaseObject obj = list.DefaultIfEmpty(null).First();  // linq
            return obj==null ? null : obj.Save(list);
        }
        public static IEnumerable<IBaseObject> Load<T>(XmlElement element) 
            where T: IBaseObject, new()
        {
            IBaseObject proto = new T();
            return proto.Load(element);
        }
    }
    

    (original edit)

    This has a problem in that you must call the static methods with a type, e.g.

    BaseClass<Temperature>.Load()
    

    There is a way around this for the Save method, but part of what you want is not possible. The Load method cannot know what type of list to return because its only parameter has no information about the return type. Hence, it can’t possibly decide which type to create as a prototype. So no matter what, if you wanted to use common Load method, you would have to pass it a type like the above syntax.

    For the Save method, you could use reflection to create the prototype in the static method, by obtaining the type from the first element, and then call the Save method from the prototype. So if you only need the Save method to be used as you like, that much is possible.

    Ultimately, though, I think it would be a lot simpler to do something like this:

    public static XElement Save(List<IBaseClass> list)
    {       
        if (list is Temperature) {
           // do temperature code
        } else if (list is SomethingElse) {
          // do something else
        } 
    }
    

    Anyway – like I said it’s going to require reflection to make even the Save method work in this way. I’d just use the simple approach.

    (original bad code removed)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a web application that allows a user to get an employee's
I am writing a Delphi application that allows a user to connect to an
I am writing an application that reads in a large number of basic user
I am writing an application that allows a user to submit a query to
I am writing a fairly simple MVC3 application that allows a user to retrieve
I'm writing an Android application that allows a user to maintain a list of
I am writing an application that allows a user to enter a boolean expression.
I'm writing an application that allows users to upload images onto the server. I
I am writing an application that if the user hits back, it may resend
I am writing an application in C# that needs to run as a service

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.