How to create a Basic4ppc library

Discorez

Member
Licensed User
Longtime User
Please, help me with events...

I created C# DLL with simple class for testing event:
B4X:
using System;
using System.Collections.Generic;
using System.Text;

namespace EventHandlersTest
{
    public delegate void MyEventHandler();
        
    public class MyEvent
    {
        public event MyEventHandler SomeEvent;

        public void OnSomeEvent()
        {
            if (SomeEvent != null)
                SomeEvent();
        }

        public void Test(int X)
        {
                if (X == 10)
                    OnSomeEvent();
        }
    }
}

B4PPC code:
(ME - MyEvent.dll)
B4X:
Sub App_Start
   Form1.Show
   ME.New1
End Sub

Sub Button1_Click
  ME.Test(10)
End Sub

Sub ME_OnSomeEvent
  Msgbox("SomeEvent!")
End Sub

But, if I clicked on Button1, event OnSomeEvent doesn't work.
How to correct?
:sign0085:
 

Discorez

Member
Licensed User
Longtime User
Erel, unfortunately, still it is not clear... In an example, event is anchored to already available event of ScrollBar, but in my case, event is required for the self class. I tried various variants, but I receive either an error, or event doesn't work in B4PPC...
I will be grateful, if somebody helps with example on a C #.
 

agraham

Expert
Licensed User
Longtime User
Like this I think. Compare it to the ScrollBar code.

B4X:
namespace EventHandlersTest
{
         // at runtime tells Basic4ppc what event has occurred
        private object[] SomeEventEventObject;

 
        // at compile times identifies the event to Basic4ppc
        // at runtime Basic4ppc sets this to its event handler
       public event EventHandler SomeEvent; 

      
        public class MyEvent
        {
            SomeEventEventObject = new object[] { this, "SomeEvent" };
        }

        public void OnSomeEvent()
        {
            if (SomeEvent != null)
                SomeEvent();
        }

        public void Test(int X)
        {
                if (X == 10)
                    OnSomeEvent();
        }
    }
}
 

Discorez

Member
Licensed User
Longtime User
agraham, Your example isn't compiled (~4 errors)
My project (VS 2008) in attach.
 

Attachments

  • EventHandlersTEst.zip
    16.1 KB · Views: 308

agraham

Expert
Licensed User
Longtime User
Sorry, I knocked it up from memory without testing it, but it's really not complicated.

B4X:
    public class MyEvent
    {
        private object[] SomeEventEventObject;
        public event EventHandler SomeEvent;

       public MyEvent()
        {
             SomeEventEventObject = new object[] { this, "SomeEvent" };
        }

        public void OnSomeEvent()
        {
            if (SomeEvent != null)
                SomeEvent(SomeEventEventObject, null); 
        }

        public void Test(int X)
        {
            if (X == 10)
                OnSomeEvent();
        } 

    }
 

Discorez

Member
Licensed User
Longtime User
agraham, many thanks!
All perfectly works! All problems was here -
B4X:
SomeEvent(SomeEventEventObject, null);
I did a call SomeEvent() without arguments...
 
Top