Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Passing delegate from another class

Posted on 2007-03-19
12
Medium Priority
?
376 Views
Last Modified: 2012-08-14
Hello Experts
I need help figuring out how to pass the delegate from another class. I have code that looks something like this:


Class1
{
public delegate void test(string)

Method1()
{
test delTest=new test(Method2)
class2.method(delTest)
}

BOOL Method 2(string)
{
blah...
}

}

Class2 // Independent stand alone class
{
  Method(NOT SURE HOW TO receive delegate from Class1 here)
  {
  If (PASSED DELEGATE==true)
     do something
  }
}

Also, if the method my delegate is pointing to is a BOOL type... Do I need to make my delegate a BOOL type instead of Void as well ? I would appreciate any comments/suggestions as I'm new and learning.
Thank you.
0
Comment
Question by:compulaw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 4
12 Comments
 
LVL 29

Assisted Solution

by:Gautham Janardhan
Gautham Janardhan earned 800 total points
ID: 18754286
Method(delegate Test)
  {
  If (Test("value") ==true)
     do something
  }
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 18754306
     public class Class2
      {
            public delegate bool FDelegate (string Test);
            public Class2()
            {
                  FDelegate Fnew = new FDelegate(Target);
                  Class3.CallMe(Fnew,this);
            }
            bool Target(string Test)
            {
                  return true;
                  
            }
            
      }
      public class Class3
      {
            public static void CallMe(Delegate FTemp,Class2 FObj)
            {
                  if( (bool)FTemp.Method.Invoke(FObj,new string[]{""}))
                  {
                        Console.WriteLine(true);
                  }
            }                                    
      }
0
 

Author Comment

by:compulaw
ID: 18754350
Class2 ( well Class3 in gauthammpj's example ) is a separate class and it doesn't see "Delegate FTemp" from the first class. Do I need to reinitialize the delegate in the second class ?
Could you explain this line " public static void CallMe(Delegate FTemp,Class2 FObj)" a little bit more please ?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 18754364
CallMe(Delegate FTemp,Class2 FObj)

Delegate is a struct in the .net Frame work. this will be the base object of ur deleagate .. u need to pass the object of class 2 to class 3 because then only u can invoke the method containied in the delegate that u are passing thru.

<it doesn't see "Delegate FTemp"> ?
what d'u mena by this

and

<reinitialize the delegate in the second class >
No

0
 
LVL 12

Expert Comment

by:andrewjb
ID: 18754874
Try this:

  public delegate bool MyDelegate( int a , string b );

  public class ClassA
  {
    public void Do()
    {
      ClassB b = new ClassB();

      b.ProcessDelegate( new MyDelegate( IWillBeCalled ) );

      // Alternative, shorthand way to call
      b.ProcessDelegate( IWillBeCalled );
    }

    private bool IWillBeCalled( int x , string toTest )
    {
      return ( x == 10 ) && ( toTest.Length > 2 );
    }
  }

  public class ClassB
  {
    public void ProcessDelegate( MyDelegate d )
    {
      if ( d( 10 , "Hello there" ) )
      {
        Console.WriteLine( "It's true!" );
      }
    }
  }



and, gauthampi, a Delegate is a class, not a struct.
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 18754931
sorry :-)
0
 

Author Comment

by:compulaw
ID: 18757232
Thank you for another example but the same problem remains.
The error message comes up in Class B " The type or namespace name 'MyDelegate' ( or 'Delegate' in gauthampj's example ) could not be found ( you are missing a using directive or an assembly reference?)

0
 
LVL 12

Expert Comment

by:andrewjb
ID: 18757303
Not if you've copied exactly what I posted, and put both ClassA and ClassB in the same namespace it doesn't :-)
0
 

Author Comment

by:compulaw
ID: 18757871
Very confused :-/
Here guys, i'll paste the actual code.

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;

namespace importer
{
    class Program
    {
        public delegate bool asciiRecordSender(string asciiRecord);
       
        static void Main(string[] args)
        {
           
            string asciiRecord;

            readconifg.readconfig();

            recordprocessor asciiProcessor = new recordprocessor(readconifg.FilePath);
            asciiRecord=asciiProcessor.recordProcessor(); // gets the asciiRecord for processing


            asciiRecordSender recordSender = new asciiRecordSender(ProcessAsciiRecord); //delegate

            recordprocessor asciiReader = new recordprocessor(readconifg.FilePath);
            asciiReader.Execute(recordSender,asciiRecord); //delegate


     
        }

        static bool ProcessAsciiRecord(string asciiRecord)
        {

           
            List<string> tempList= new List<string>();
            string[] dummy = new string[1] { "" };
           

            tempList = new List<string>();
            tempList.AddRange(splitter.splitting(asciiRecord, "\"", ","));
            if (tempList.Count != 9)
            {
                Console.WriteLine("ERROR,ERROR,ERROR,ERROR,ERROR!");
               
            }

            Console.WriteLine("Line Added");
            for (int i = 0; i < tempList.Count; i++)
            {
                Console.WriteLine(tempList[i]);
            }

           
        }//end of bool Processing AsciiRecord method


SECOND CLASS:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;

namespace importer
{
    class recordprocessor
    {
        StreamReader m_reader = null;
        int m_count = 0;
        string error;


        public recordprocessor(string dbFilePath)
        {
                m_reader = new StreamReader(dbFilePath);
        }//end of recordprocessor constructor


        public void Execute( asciiRecordSender recordSender,string asciiRecord)  /* THIS IS WHERE THE ERROR COMES UP saying that asciiRecordSender could not be found) */
        {
            if (recordSender(asciiRecord) == true)
            {
                while ((record = m_reader.ReadLine()) != null)
                {

                    // Do something with the ascii record.

                }//end of while loop
            }// end of if statement
        }//end of void Execute


        public string recordProcessor()
        {
            string record;
            while ((record=m_reader.ReadLine()) != null)
            {
                return record;
               
            }//end of while, adding to the ArrayList
       }//end of recordProcessor method



    }//end of class
}
Whats wrong with this code and passing the delegate ?
0
 
LVL 12

Expert Comment

by:andrewjb
ID: 18757913
Move your delegate definition outside of the 'Program' class. Just put it directly into the namespace.
0
 
LVL 12

Accepted Solution

by:
andrewjb earned 1200 total points
ID: 18757919
i.e...

namespace importer
{
    class Program
    {
        public delegate bool asciiRecordSender(string asciiRecord);
        ....      

becomes

namespace importer
{
    public delegate bool asciiRecordSender(string asciiRecord);

    class Program
    {
       ...
0
 

Author Comment

by:compulaw
ID: 18758226
Ahhhhhh... Thank you :)
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question