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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:23:43+00:00 2026-05-31T21:23:43+00:00

I’m trying to learn delegate and not sure what I do wrong. So I

  • 0

I’m trying to learn delegate and not sure what I do wrong. So I have three classes, Form1, Class1, and Class2. Form1 calls Class1 method to set a string for Class1 member and then Form1 starts workerbackground thread to start Class2. But when Class2 calls Form1 delegate method which will call Class1 method to get string data, but it returns back to Class2 as null.

So this is where it returns null,>> string str = form1Obj.gDelegateClass1GetData(();

This is Form1:

public delegate string DelegateClass1GetData();
public delegate void DelegateClass1SetData(string data);

public partial class Form1 : Form
{
    public DelegateClass1GetData gDelegateClass1GetData;
    public DelegateClass1SetData gDelegateClass1SetData;

    public Class1 class1Obj;
    public BackgroundWorker bw_Class2;
    public Class2 class2Obj;


    public Form1()
    {
        InitializeComponent();

        class1Obj = new Class1();
        class2Obj = new Class2(this);


        gDelegateClass1GetData = new DelegateClass1GetData(this.Class1GetData);
        gDelegateClass1SetData = new DelegateClass1SetData(this.Class1SetData);
    }


    private void button1_Click(object sender, EventArgs e)
    {
        class1Obj.setClass1Data("Hello");

        bw_Class2 = new BackgroundWorker();
        bw_Class2.WorkerSupportsCancellation = true;

        bw_Class2.DoWork += new DoWorkEventHandler(ThreadClass2_DoWork);
        bw_Class2.RunWorkerAsync();
    }


    public string Class1GetData()
    {
        if (InvokeRequired)
            Invoke(new DelegateClass1GetData(Class1GetData));
        else
        {
            return class1Obj.GetClass1Data();
        }

        return null;
    }


    public void Class1SetData(string data)
    {
        if (InvokeRequired)
            Invoke(new DelegateClass1SetData(Class1SetData), data);
        else
        {
            class1Obj.setClass1Data(data);
        }
    }


    public void ThreadClass2_DoWork(object sender, DoWorkEventArgs e)
    {
        string str1 = class1Obj.GetClass1Data();
        class2Obj.AddString();
    }

}

This is Class1:

public class Class1
{
    public string gClass1Data;

    public Class1()
    {
        gClass1Data = null;
    }


    public void setClass1Data(string data)
    {
        gClass1Data = data;
    }


    public string GetClass1Data()
    {
        return gClass1Data;
    }
}

This Class2:

public class Class2
{

    public Form1 form1Obj;

    public Class2(Form1 obj)
    {
        form1Obj = obj;
    }


    public void AddString()
    {
        string str = form1Obj.gDelegateClass1GetData();
        str = str + " Delegate";

        form1Obj.gDelegateClass1SetData(str);
        string str2 = form1Obj.gDelegateClass1GetData();
    }
}
  • 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-31T21:23:44+00:00Added an answer on May 31, 2026 at 9:23 pm

    The problem is in Form1.Class1GetData method. When you call form1Obj.gDelegateClass1GetData() in Class2.AddString(), you actually call Form1.Class1GetData. InvokeRequired returns true, and you have to call Form1.Class1GetData again. This time, InvokeRequired return false, and you call class1Obj.GetClass1Data(), and returns “Hello”. It actually returns to the first call to Form1.Class1GetData, but you return null finally. So, you get null to string str = form1Obj.gDelegateClass1GetData(();. Try to fix it as

    public string Class1GetData()
    {
        if (InvokeRequired)
            return (string) Invoke(new DelegateClass1GetData(Class1GetData)); // return the first call to Class1GetData
        else
        {
            return class1Obj.GetClass1Data();
        }
    
        //return null; // Don't return null.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.