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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:15:02+00:00 2026-06-09T17:15:02+00:00

I am attempting to use a delegate in a way which I have never

  • 0

I am attempting to use a delegate in a way which I have never seen described in my various books.

My questions are:

Is it possible to use delegates in this way? and

If so, how should I alter the code to make use of the delegate?

Specifically I want one function to call another function from a choice of two possible functions.

class Profile
{
   private List<verticalCurve> allVCs;
   // create allVCs in the constructor

   private double nonTrivialFunctionToFindTheRightVCin_allVCs
                      (double lengthAlong, getSwitchForProfile aDel)
   { // about thirty lines of code which I want to reuse }


   public double getElevation(double distanceAlongPfl)
   {
         // compiler error on the following line:
      getSwitchForProfile myDelEL = 
            new verticalCurve.getSwitchForProfile(verticalCurve.getElevation);

      return nonTrivialFunctionToFindTheRightVCin_allVCs
                 (distanceAlongPfl, myDelEL);
   }

   public double getSlope(double distanceAlongPfl)
   {
         // compiler error on the following line:
      getSwitchForProfile myDelSL = 
            new verticalCurve.getSwitchForProfile(verticalCurve.getSlope);

      return nonTrivialFunctionToFindTheRightVCin_allVCs
                 (distanceAlongPfl, myDelSL);
   }

}  // end class Profile

class verticalCurve
{
   private double elevation;
   private double slope;

   static internal delegate double getSwitchForProfiles(double distanceAlongPfl);

   public double getElevation(double distanceAlong)
   { computeElevation then return elevation; }

   public slope getSlope(double distanceAlong)
   { compute slope then return slope;}
}  // end class verticalCurve

The compiler error states

An object reference is required for the non-static field, method, or property ‘Profile.verticalCurve.getElevation(distanceAlong)’

It appears that my problem is that at the moment I assign a method to the delegate, I do not yet know which instance of verticalCurve it will be called on. But I can’t make verticalCurve.getElevation static because it has to know which verticalCurve it is on.

Sorry for the long setup to the questions. I did try to simplify it, but it seems irreducible beyond this point.

Thanks in advance for your help.

  • Paul Schrum
  • 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-09T17:15:04+00:00Added an answer on June 9, 2026 at 5:15 pm

    It sounds like you want to create a delegate which takes the object to call it on as a parameter:

    delegate double DistanceFunction(VerticalCurve curve, double distance);
    
    void SomeFunction(DistanceFunction func) {
        double result = func(someCurve, 42);
    }
    
    SomeFunction((c, dist) => c.GetSlope(dist));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Attempting to use the data series from this example no longer passes the JSONLint
I have been attempting to use ASP.NET MVC remote validation for username login access
I have written a tab based application in Xcode/RestKit and am attempting to use
I'm fairly new to this, and don't have anyone else to ask. I'm attempting
I am attempting use Java Pathfinder and I have pathfinder working. import gov.nasa.jpf.jvm.Verify; user.java:2:
I am attempting to use the DELETE clause in MS Access and have an
I am attempting to use the google visualization library - intensity map. I have
attempting to use this railscast as a guide: http://railscasts.com/episodes/197-nested-model-form-part-2?view=asciicast and running into this error:
In attempting to use the Performance Tools on an ASP.NET website I'm getting various
While attempting to use the CopyFile() function I have encountered a strange error. It

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.