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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:42:01+00:00 2026-06-07T06:42:01+00:00

I was tackled by this issue for too many times so i decided to

  • 0

I was tackled by this issue for too many times so i decided to share and see what you guys think, lets look at the following (dumb) exemple:

public delegate void ToRun();
class Runner {
    ToRun tr;
    public Runner(ToRun f) {
        tr=f;
    }
    public void run() {
        tr();
    }
}
class CountingRunner : Runner {
    ToRun tr;
    int i;
    public CountingRunner(ToRun f) : base(f+=inc) {
        i=0;
    }
    private static void inc() {
        i++; //COMPILATION ERROR - i is not (and logically cannot be) static!
    }
}

well, what i want to ask is:

Q1: why do base() parms have to be static?

Q2: what if, as in my exemple, we want to combine nonstatic fields or methods with the call to the base constructor? what is the most OOP way to do that?

Note : try not to give bandaid solutions like “just dont use the base c’tor”, cause there might be more complex situation where using base is unavoidable, so im looking for a reasonable well designed solution for this.

Thanks!

Update:
my exemple was too easy to crack,therefore i feel like i havent learned enough, so lets try to give another (pretty dumb still) exemple:

public delegate int HashFunc<E>(E e);
public interface HashTable<E> {
    void insert(E e);
    bool isMember(E e);
} 
class HashArray<E> : HashTable<E> where E : IComparable<E> {
    private E[] a;
    private bool[] taken;
    public readonly int n;
    public int size {
        get { return n; }
    }
    HashFunc<E> hash;
    public HashArray(int m , HashFunc<E> hash ) {
        n=2*m;
        a=new E[n];
        taken=new bool[n];
        for (int i=0 ; i<n ; i++) taken[i]=false;
        this.hash=hash;
    }
    public void insert(E e) {
        int index=hash(e),i;
        for (i=index ; i<n && taken[i]!=false ; ++i) ;
        if (i>=n)
            for (i=0 ; i<index && taken[i]!=false ; ++i) ;
        if (i>=index) return;
        taken[i]=true;
        a[i]=e;
    }
    public bool isMember(E e) {
        int i=hash(e);
        for ( ; i<n && taken[i]!=false && a[i].CompareTo(e)!=0 ; ++i );
        if (i>=n || taken[i]==false) return false;
        return true;
    }
}
class HashArrayInt : HashArray<int> {
    public HashArrayInt(int n) : base (n,HashFunc) {
    }
    public static int HashFunc(int i) {
        return (i%n);// n is a non static field, every hash table has its own size!
    }
}

in this exemple we are giving some weird implementation for an hash table where the hash function is unknown, and a special class for hash table of ints with predefined hash function, notice that here again we need to combine the non static size of the hashtable n and base c’tor…

  • 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-06-07T06:42:03+00:00Added an answer on June 7, 2026 at 6:42 am

    This is what you want:

    class Runner {
        protected event Action _toRun;
    
        public Runner() {
        }
        public void Run() {
            var r = _toRun;
            if (r != null)
               _toRun();
        }
    
    
    }
    
    class CountingRunner : Runner {
    
        int i;
        public CountingRunner(Action f) : base() {
            _toRun += f;
        }
        public void inc() {
            i++;
        }
    }
    

    EDIT

    For your particular example with hash tables, this problem is solved by the design of the language. Just call GetHashCode() on the elements of your hashtable to determine their hashcode. You don’t need implementations to pass a hashing function.

    To answer your more general question of “How should I send functions manipulating instance data to the base class,” you should either capture your instance variables in a lambda expression and send that to the base class, or consider a design in which the base class doesn’t need access to the instance functions of its derived classes. I would go with the latter 🙂

    One such design would be to have the function a pure virtual call in the base class. That would require derived classes to implement the virtual call in order to be instantiated. So here you would have a abstract int GetHashCode(E item) function in the base class, and just override it in your subclasses. Again, in this specific case the language does this for you with the virtual GetHashCode() function defined for all types.

    Here is a non-abstract example (derived classes aren’t required to override the hashing function).

    class HashArray<E> : HashTable<E> where E : IComparable<E> {
        private E[] a;
        private bool[] taken;
        public readonly int n;
        public int size {
            get { return n; }
        }
    
        public HashArray(int m) {
            n=2*m;
            a=new E[n];
            taken=new bool[n];
            for (int i=0 ; i<n ; i++) taken[i]=false;
    
        }
        public void insert(E e) {
            int index= GetSpecialHashCode(e)%n;
            int i;
            for (i=index ; i<n && taken[i]!=false ; ++i) ;
            if (i>=n)
                for (i=0 ; i<index && taken[i]!=false ; ++i) ;
            if (i>=index) return;
            taken[i]=true;
            a[i]=e;
        }
        public bool isMember(E e) {
            int i= GetSpecialHashCode(e)%n;
            for ( ; i<n && taken[i]!=false && a[i].CompareTo(e)!=0 ; ++i );
            if (i>=n || taken[i]==false) return false;
            return true;
        }
    
        protected virtual int GetSpecialHashCode(E item) {
            return item.GetHashCode();
        }
    }
    

    So you get a default hashcode generating function, but derived classes are also welcome to supply their own.

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

Sidebar

Related Questions

I know this questions has been asked many times before -- but I haven't
I think I know the issue with this, but I do not know how
I'm wondering if this is the best way to tackle this issue. I am
I'm just wondering how other developers tackle this issue of getting 2 or 3
I do not currently have this issue , but you never know, and thought
i'm looking for the best way to tackle this issue and in what order
I'm wondering how to best tackle this, since what I have now works great
I have this problem I've been trying to tackle for a while. I have
While other questions have tackled the broader category of sequences and modules , I
Is there a standardized (or commonly accepted way) to tackle the issue of not

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.