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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:09:33+00:00 2026-05-11T08:09:33+00:00

I feel the answer to this may lie with delegates, but I am having

  • 0

I feel the answer to this may lie with delegates, but I am having a hard time grasping the concept of delegates. The main problem is that every explanation and example of delegates I have ever read are always round about ways of doing something you could accomplish without delegates so to me it does not teach me anything. I learn best by seeing real world examples.

Now that that is out of the way, here is what I want to accomplish. I have a Data Context (.dbml) with numerous stored procedures. I also have mutliple situations where I am using the exact same 20 lines of code to update one column in a table, but the only difference other than using a different datagrid, is the stored procedure being called. In an effort of reducing the amount of code used, I was hoping for a way to pass the stored procedure call from the data context object as a parameter. That way I can move all that code to one reusable function. Is this even possible? I am using Visual Studio 2008 and C#.

Thanks for any guidance.

  • 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. 2026-05-11T08:09:34+00:00Added an answer on May 11, 2026 at 8:09 am

    While I can’t help you with the sql / stored proc side of things, I can try explain delegates, at least from the C# point of view.

    While normally you declare functions as being part of a class (and hence they are strongly attached to the class), sometimes you want to put them in a variable. Once you do this, you can then pass it around, much like you would with any other variable.

    So we know that a string is the kind of variable that you stick text into. Following that, a delegate is the kind of variable that you stick functions into. This however is very confusing, as C# isn’t consistent or clear with how it names things in your code. Observe:

    public void WriteText() {   Console.WriteLine('Hello'); }  ... Action x = WriteText; x(); // will invoke the WriteText function 

    Note we’re using ‘Action’ where logic would imply the code should read delegate x = WriteText. The reason we need this extra mess is because ‘delegate’ itself is like System.Object. It doesn’t contain any information, and it’s kind of the ‘base class’ behind everything. If we actually want to use one, we have to attach some Type information. This is where Action comes in. The definition of Action is as follows:

    public delegate void Action(); 

    What this code says is ‘we’re declaring a new delegate called Action, and it takes no parameters and returns void‘. Thereafter if you have any functions which also take no parameters and return void, you put them in variables of type Action.

    Now, you can stick a normal function into a delegate, but you can also stick an ‘anonymous’ function into a delegate. An ‘anonymous’ function is something that you declare inline, so rather than attaching the already-declared WriteText function, we could build a new one up in the middle of our code like this:

    Action x = () => { Console.WriteLine('Hello'); }; x(); // invoke our anonymous function. 

    What this is doing is using the C# ‘lambda syntax’ to declare a new anonymous function. The code that runs as part of the function (when we invoke it) is the Console.WriteLine.

    SO

    To put it all together, you could have a ‘SaveData’ function, and pass it a delegate. It could do it’s 20 lines of table building, then pass that table to the delegate, and the delegate could invoke the appropriate stored-proc. Here’s a simple example:

    public void SaveData(Action<Table> saveFunc){     var t = new Table();     ... 20 lines of code which put stuff into t ...     saveFunc(t); }  SaveData( t => StoredProc1.Invoke(t) ); // save using StoredProc1 SaveData( t => StoredProc37.Invoke(t) ); // save using StoredProc37 

    SO

    Having said ALL OF THAT. This isn’t how I’d actually solve the problem. Rather than passing the delegate into your savedata function, it would make more sense to have your SaveData function simply return the table, and then you could then invoke the appropriate StoredProc without needing delegates at all

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

Sidebar

Related Questions

This is a question I intend to answer myself, but please feel free to
I feel like this should have a simple answer, but I can't find it.
I feel like I should know this, but I haven't been able to figure
In a C# (feel free to answer for other languages) loop, what's the difference
In C#, (and feel free to answer for other languages), what order does the
I feel that my shop has a hole because we don't have a solid
I feel like a fool, but here goes: public interface IHasErrorController{ ErrorController ErrorController {
I feel that using GetEnumerator() and casting IEnumerator.Current is expensive. Any better suggestions? I'm
I feel like I'm missing a fairly fundamental concept to WPF when it comes
I feel like I'm re-inventing the wheel here, but I need to find a

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.