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 663349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:28:47+00:00 2026-05-13T23:28:47+00:00

actually i refactor some portion of code. what i want to do is to

  • 0

actually i refactor some portion of code.
what i want to do is to initialize an object “Task” with an object “TaskArgument”.
let s say “TaskArgument” is abstract and “Task” implements a method “OnEnterTask(TaskArgument args)” and is sealed (for some special behavior of the existing system, which is out of scope).

old code:

public sealed class Task : SomeSystemBaseTask {
  private int accessMe; 
  private int meToo;

  public void OnEnterTask(TaskArgument args) {
    if (args is SimpleTaskArgument) {
      accessMe = ((SimpleTaskArgument)args).uGotIt;
      meeToo = 0;
    } else if (args is ComplexTaskArgument) {
      accessMe = ((ComplexTaskArgument)args).uGotItValue * ((ComplexTaskArgument)args).multiplier;
      meToo = ((ComplexTaskArgument)args).multiplier - 1;
    }
  }
}

what would be the best practise avoid the typecheck?
my first stupud thought was:

public abstract class TaskArgument {
    internal public abstract Initialize(Task args);
}

public class SimpleTaskArgument : TaskArgument {
    public int uGotIt = 10;

    internal public Initialize(Task task){
        task.accessMe = uGotIt;
    }
}

public class ComplexTaskArgument : TaskArgument {
    public int uGotItValue = 10;
    public int multiplier = 10;

    internal public Initialize(Task task){
        task.accessMe = uGotItValue*multiplier;
        task.meToo = multiplier - 1;
    }
}

public sealed class Task : SomeSystemBaseTask {
    public int accessMe;
    public int meToo;

    public void OnEnterTask(TaskArgument args){
        args.Initialize(this);
    }
}

but then my “accessMe” is public and the “Initialize” method works only with “Task”.
so i moved the typechecking to another place (in future).
is there any best practise or good design idea.

…”internal public”… mmhhmm?

another crazy idea was an inner class, but i dont like those and it make such a simple case more complex or don’t:

public abstract class TaskArgument {
    internal public abstract Initialize(ITaskWrapper wrapper);
}

public class SimpleTaskArgument : TaskArgument {
    ...
}

public class ComplexTaskArgument : TaskArgument {
    ...
}

public interface ITaskWrapper {
    public int AccessIt { set; get; } 
    ...  
}

public sealed class Task : SomeSystemBaseTask {
    private int accessMe;
    ...

    class TaskWrapper : ITaskWrapper {
        ...
    }

    public void OnEnterTask(TaskArgument args){
        args.Initialize(new TaskWrapper(this));
    }
}

where is the best place for initialization when it is based on the given Type of the “TaskArgument”?

kindly excuse my bad english knowledge

greetings
mo

  • 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-13T23:28:48+00:00Added an answer on May 13, 2026 at 11:28 pm

    Use an interface.

    public void OnEnterTask(TaskArgument args) { 
       if (args is SimpleTaskArgument) { 
          accessMe = ((SimpleTaskArgument)args).uGotIt; 
       } else if (args is ComplexTaskArgument) { 
          accessMe = ((ComplexTaskArgument)args).uGotItValue * ((ComplexTaskArgument)args).multiplier; 
       } 
    } 
    

    becomes

    public void OnEnterTask(ITaskArgument args) { 
       accessMe = args.GetAccessMe();
    } 
    

    Then you have your classes implement ITaskArgument and implement the method for each class. In general, when you’re doing something like this:

    accessMe = ((ComplexTaskArgument)args).uGotItValue * ((ComplexTaskArgument)args).multiplier;

    where you’re accessing multiple properties on an object to perform a calculation, it usually makes sense to push that logic into the class itself.

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

Sidebar

Related Questions

Actually I am trying to move some box alternatively with in another box. I
Actually, this question seems to have two parts: How to implement pattern matching? How
Actually, I'm using this way. Do you have a better way? private bool AcceptJson(HttpRequest
Actually here is a similar topic with little practical value. As far as I
Actually my question is all in the title. Anyway: I have a class and
Actually what i am trying to build is like a kind of firewall. It
I actually have an answer to my question but it is not parallelized so
What actually happens to the file system when you do a Subclipse Share Project
I actually have two questions regarding the same problem but I think it is
Disclaimer: This is not actually a programming question, but I feel the audience on

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.