7 Replies - 1297 Views - Last Post: 10 December 2010 - 04:28 AM Rate Topic: -----

#1 bluebear1608  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 82
  • Joined: 03-June 08

public delegate bool WrongMediaEvent() & NullReferenceException

Posted 08 December 2010 - 03:21 AM

Im writing a program to burn some data on to a disk.
What im trying to do here is trigure the WrongMediaEvent when the disc inserted is not blank.
For some reason, the NullReferenceException is thrown when i trigure the WrongMediaEvent using this.OnWrongMedia().
Am i doing something wrong or missing something?
Heres a snippet of the code.

namespace FTSBurnLib
{
    public class FtsBurnEngine
    {
        public delegate bool WrongMediaEvent();

        public event WrongMediaEvent OnWrongMedia;

        public long DetectMediaSize(IMAPI_MEDIA_PHYSICAL_TYPE mediaType)
        {
            
            try
            {
                //Create a file system and select the media type
                MsftFileSystemImage fileSystemImage = new MsftFileSystemImage();
                fileSystemImage.ChooseImageDefaultsForMediaType((IMAPI_MEDIA_PHYSICAL_TYPE)mediaType);
                
                //See if there are other recorded sessions on the disc
                if (dataWriter.MediaHeuristicallyBlank)
                {
                    Int64 freeMediaBlocks = fileSystemImage.FreeMediaBlocks;
                    long size = 2048 * freeMediaBlocks;
                    return size;
                }
                else
                {
                    //Disc not blank
                    this.OnWrongMedia();
                    return -1;
                }
            }
            catch (COMException e)
            {
                //Media Detection error
                this.onerror(e.ErrorCode, e.Message);
                return -1;
            }
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: public delegate bool WrongMediaEvent() & NullReferenceException

#2 mavarazo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 36
  • View blog
  • Posts: 182
  • Joined: 25-October 10

Re: public delegate bool WrongMediaEvent() & NullReferenceException

Posted 08 December 2010 - 03:47 AM

I think you have to read some basics about delegate and events http://www.akadia.co...and_events.html

public delegate void WrongMediaEvent(bool isWrong);

public event WrongMediaEvent WrongMedia; // Rename to WrongMedia just it's nicer :)/>

public long DetectMediaSize(IMAPI_MEDIA_PHYSICAL_TYPE mediaType) {
   ...
   OnWrongMedia(true);
}

private void OnWrongMedia(bool isWrong) {
   if (WrongMedia!= null)
      WrongMedia(isWrong);
}


This post has been edited by mavarazo: 08 December 2010 - 03:48 AM

Was This Post Helpful? 0
  • +
  • -

#3 bluebear1608  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 82
  • Joined: 03-June 08

Re: public delegate bool WrongMediaEvent() & NullReferenceException

Posted 08 December 2010 - 06:11 AM

namespace FTSBurnLib
{
    public class FtsBurnEngine
    {
        public delegate bool WrongMediaEvent();

        public event WrongMediaEvent OnWrongMedia;

        public long DetectMediaSize(IMAPI_MEDIA_PHYSICAL_TYPE mediaType)
        {
           
        }
    }
}


this part of it was given to me so im not sure if i can change it. I've read the link but im struggling to understand the use of delegate. My understanding is its similar to a class in that you declare and instantiate it the same way you would with any other variable but then it kind of acts as an indirect function call. is that right? If so, what would be the benefits of doing this instead of just declaring a function and calling that?
So in my case, i need to add a function that should be encapsulated in the delegate?
so something like:
namespace FTSBurnLib
{
    public class FtsBurnEngine
    {
        public delegate bool WrongMediaEvent();

        public event WrongMediaEvent OnWrongMedia;

        public long DetectMediaSize(IMAPI_MEDIA_PHYSICAL_TYPE mediaType)
        {
            ...
            OnWrongMedia(Function1)
        }

        public bool Function1()
        {
            //Do something
            return true;
        }
    }
}


but then what use is declaring it as an event if all you can do is call a different function through it? I'm really confused about this.
I'm pretty sure with the startup code, i was only needed to trigger the event if a wrong media is inserted, and nothing else as its handled by a different part of the program. I think I'm completely lost
Was This Post Helpful? 0
  • +
  • -

#4 mavarazo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 36
  • View blog
  • Posts: 182
  • Joined: 25-October 10

Re: public delegate bool WrongMediaEvent() & NullReferenceException

Posted 08 December 2010 - 07:18 AM

Ok, sorry my fault :), didn't read carefully your post.

Try something like this:

public delegate bool WrongMediaEvent();

public class MyClass {
	public event WrongMediaEvent WrongMedia;

	public MyClass() {
		WrongMedia += new WrongMediaEvent(MyCalss_WrongMedia);
	}

	public bool MyClass_WrongMedia() {
		return true;
	}
	
	public void SomeMethod() {
		WrongMedia(); // Fire Event
	}
}

Was This Post Helpful? 1
  • +
  • -

#5 Zunera  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 74
  • Joined: 07-December 10

Re: public delegate bool WrongMediaEvent() & NullReferenceException

Posted 08 December 2010 - 07:18 AM

Hi,

what should happen when an error occurs: An event should be fired so that other parts of the program may react. You can also take a look here (and focus on the last example on this page). I prefer a shorter and more common way to define an event (than defining an additional delegate):

public event EventHandler WrongMedia;
protected virtual void OnWrongMedia(EventArgs args)
{
  if(WrongMedia != null) WrongMedia(this, args);
}
public long DetectMediaSize(IMAPI_MEDIA_PHYSICAL_TYPE mediaType)
{
  ...
    OnWrongMedia(EventArgs.Empty);
  ...
}



Of course you can use your own EventArgs (derive from EventArgs and use generic EventHandler<MyEventArgs>).

The event is optional - you can just remove all the lines about WrongMedia if you don't need/want to fire an event - but you cannot replace an invocation of an event by a function just returning true! Hf
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,516
  • Joined: 23-August 08

Re: public delegate bool WrongMediaEvent() & NullReferenceException

Posted 08 December 2010 - 09:28 AM

Another nice tutorial on events, and one on delegates, and another on delegates.

Actually, if you search our C# Tutorials there is lots of relevant information on delegates.
Was This Post Helpful? 0
  • +
  • -

#7 bluebear1608  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 82
  • Joined: 03-June 08

Re: public delegate bool WrongMediaEvent() & NullReferenceException

Posted 08 December 2010 - 11:35 PM

i tried mavarazo's solution and got it working. I've read all the tutorials but I've still got a couple of questions.
1. Why would you use delegate events? is it just quicker than creating an event class or are there other advantages?
2.
namespace FTSBurnLib
{
    public class FtsBurnEngine
    {
        public delegate bool ErrorEvent(int errc, string message);

        public event WrongMediaEvent onerror;

        public FtsBurnEngine()
        {
            onerror += new ErrorEvent(Error);
        }

        public long DetectMediaSize(IMAPI_MEDIA_PHYSICAL_TYPE mediaType)
        {
            ...
            onerror()
        }

        public bool Error(int errc, string message)
        {
            return true;
        }
    }
}

If the FtsBurnEngine object is created by another part of the program, and the ErrorEvent is fired, would my program have access to the parameters of ErrorEvent?
Sorry if the questions seem stupid, I'm new to this and am trying to figure it out.

This post has been edited by bluebear1608: 08 December 2010 - 11:35 PM

Was This Post Helpful? 0
  • +
  • -

#8 Zunera  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 74
  • Joined: 07-December 10

Re: public delegate bool WrongMediaEvent() & NullReferenceException

Posted 10 December 2010 - 04:28 AM

Thats the way events are used:

    public class SomeClass
    {
        public void SomeMethod()
        {
            FtsBurnEngine engine = new FtsBurnEngine();
            engine.WrongMedia += new EventHandler<MyErrorEventArgs>(engine_WrongMedia);
            long mediaSize = engine.DetectMediaSize(...);
        }

        void engine_WrongMedia(object sender, MyErrorEventArgs e)
        {
            Console.WriteLine(e.Message);
        }
    }

    public class MyErrorEventArgs : EventArgs
    {
        public String Message {get; private set;}
        public MyErrorEventArgs(String message) { Message = message; }
    }

    public class FtsBurnEngine
    {
        public event EventHandler<MyErrorEventArgs> WrongMedia;
        protected virtual void OnWrongMedia(MyErrorEventArgs args)
        {
            if (WrongMedia != null) WrongMedia(this, args);
        }
        public long DetectMediaSize(IMAPI_MEDIA_PHYSICAL_TYPE mediaType)
        {
          //...
            OnWrongMedia(new MyErrorEventArgs("Wrong media!"));
          //...
        }
    }



You really should read some tutorials on this topic to get a basic understanding of events.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1