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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:22:25+00:00 2026-06-17T09:22:25+00:00

I want to create a class (eg. Person) with a member function (eg. giveCharity),

  • 0

I want to create a class (eg. Person) with a member function (eg. giveCharity), but I want the contents of that method to be different for each instance of the class, imitating artificial intelligence. Is this possible? When and how would I populate the code for each instance’s method?

Here’s an example:

public class Person
{
    // data members
    private int myNumOfKids;
    private int myIncome;
    private int myCash;

    // constructor
    public Person(int kids, int income, int cash)
    {
        myNumOfKids = kids;
        myIncome = income;
        myCash = cash;
    }

    // member function in question
    public int giveCharity(Person friend)
    {
        int myCharity;
        // This is where I want to input different code for each person
        // that determines how much charity they will give their friend
        // based on their friend's info (kids, income, cash, etc...),
        // as well as their own tendency for compassion.
        myCash -= myCharity;
        return myCharity;
    }
}

Person John = new Person(0, 35000, 500);
Person Gary = new Person(3, 40000, 100);

// John gives Gary some charity
Gary.myCash += John.giveCharity(Gary);
  • 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-17T09:22:26+00:00Added an answer on June 17, 2026 at 9:22 am

    There are two primary approaches that come to mind:

    1) Give each person a delegate that defines the function:

    public Func<int> CharityFunction{get;set;}
    

    Then you just need to figure out how to set it, and ensure it’s always set before using it. To call it just say:

    int charityAmount = CharityFunction();
    

    2) Make Person an abstract class. Add an abstract function like int getCharityAmount(). Then create new sub-types that each provide a different implementation of that abstract function.

    As for which to use, that will depend more on specifics. Do you have a lot of different definitions of functions? The first option takes less effort to add a new one. Does the function ever change after an object is created? That’s not possible with the second option, only the first. Do you re-use the same functions a lot? The second option is better in that case, so that the callers aren’t constantly re-defining the same small handful of functions. The second is also a bit safer in that the function will always have a definition, and you know it won’t be changed once the object is created, etc.

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

Sidebar

Related Questions

I want to create a class method that takes a block of method definitions
I want to create a class which is derived from QLineEdit ,but I can
I want to create a class that initializes a timer which will be used
I want to create whois class like that public class DomainInfo { public string
I want to create a table Person using JPA, it is a requirement that
I have a Person class, and I want to create a tree. Here is
I want to create a method that will execute a query and return a
I want to create a list of class instances that automatically updates itself following
I want to create a class for storing attributes of the many data files
I want to create a class with a nested enum. public class Foo {

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.