System::Object cast in events 
Author Message
 System::Object cast in events

Hi all.

I just started using VC7 (switched from C#) and what was simple in C# seems hard in VC7. Basically, when event is raised from the component, I get it provided through:

  static void sftp_StateChangedEvent(Object __gc * Sender, SFTP::StateChangedArgs __gc * Args)

all I want to do is cast Sender to WeOnlyDo::Client::SFTP, but when I try to do it directly using

SFTP __gc *sftp = (SFTP __gc *)Sender

I get an error "direct cast deprecated, use static_cast, dynamic_cast or __try_cast". Tried all of them - either they don't cast it properly, or I get exception when I do so. How can I do it? I'm most certain Sender is of WeOnlyDo::Client::SFTP type - since I fire the event (component written in C#).

If you feel like testing, you can get component from
http://www.*-*-*.com/

and my code looks like this:
__gc class CSFTPEvents
{
 public:
  static void sftp_StateChangedEvent(Object __gc * Sender, SFTP::StateChangedArgs __gc * Args)
  {
   SFTP __gc * sftp;
   sftp = static_cast<SFTP __gc *>(Sender);
   Console::Write(S"State changed to ");
   Console::Write(sftp->StateText(Args->NewState));
  }

Quote:
};

  int _tmain(void)
  {
   SFTP __gc *sftp = new SFTP();
   CSFTPEvents __gc *events = new CSFTPEvents;
   sftp->add_StateChangedEvent(new SFTP::StateChangedDelegate(sftp, &events->sftp_StateChangedEvent));

   sftp->set_Hostname(S"linux");
   sftp->set_Login(S"kreso");
   sftp->set_Password(S"test124");
   sftp->set_Blocking(true);

   sftp->Connect();
   return 0;
  }

Any feedback is appreciated!
Thanks,
Kreso
www.weonlydo.com



Mon, 17 Oct 2005 06:26:17 GMT  
 System::Object cast in events

I was able to fix the problem, thanks.

Kreso


  Hi all.

  I just started using VC7 (switched from C#) and what was simple in C# seems hard in VC7. Basically, when event is raised from the component, I get it provided through:

    static void sftp_StateChangedEvent(Object __gc * Sender, SFTP::StateChangedArgs __gc * Args)

  all I want to do is cast Sender to WeOnlyDo::Client::SFTP, but when I try to do it directly using

  SFTP __gc *sftp = (SFTP __gc *)Sender

  I get an error "direct cast deprecated, use static_cast, dynamic_cast or __try_cast". Tried all of them - either they don't cast it properly, or I get exception when I do so. How can I do it? I'm most certain Sender is of WeOnlyDo::Client::SFTP type - since I fire the event (component written in C#).

  If you feel like testing, you can get component from
  http://www.weonlydo.com/Samples/wodSFTP.NET.msi

  and my code looks like this:
  __gc class CSFTPEvents
  {
   public:
    static void sftp_StateChangedEvent(Object __gc * Sender, SFTP::StateChangedArgs __gc * Args)
    {
     SFTP __gc * sftp;
     sftp = static_cast<SFTP __gc *>(Sender);
     Console::Write(S"State changed to ");
     Console::Write(sftp->StateText(Args->NewState));
    }
  };

    int _tmain(void)
    {
     SFTP __gc *sftp = new SFTP();
     CSFTPEvents __gc *events = new CSFTPEvents;
     sftp->add_StateChangedEvent(new SFTP::StateChangedDelegate(sftp, &events->sftp_StateChangedEvent));

     sftp->set_Hostname(S"linux");
     sftp->set_Login(S"kreso");
     sftp->set_Password(S"test124");
     sftp->set_Blocking(true);

     sftp->Connect();
     return 0;
    }

  Any feedback is appreciated!
  Thanks,
  Kreso
  www.weonlydo.com



Mon, 17 Oct 2005 09:27:23 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Casting manged object to unmanaged object

2. Custom Socket Object (object casting)

3. Event Sink for a COM object which raises events - Proof of Concept

4. System::Object* to System::Array*

5. object vs System.Object

6. Casting issue with event handlers

7. Cast a System::Guid to REFCLSID ?

8. using System.Type in cast expression

9. Trying to cast System.DateTime to double

10. casting a System.__ComObject

11. how to cast OLE-COM-objects

12. Reflection - Deserialize XML (to re-hydrate object) - Casting Fails

 

 
Powered by phpBB® Forum Software