Geeks With Blogs


Google My Blog

Catch me at: The List!

My InstallScript Utility Belt My Amazon Wishlist
My Standard Disclaimer

Chris G. Williams Beware: I mix tech and personal interests here.

I've run into a bit of a snag in some of the conversions I've been doing.  C# has this thing called Anonymous Delegates that VB.NET doesn't. I don't really understand what they do, and as I said, VB.NET doesn't have them, so I don't have a clue how to convert them.

I've done some googling, but haven't found anything that really jumps out as the right answer. So any suggestions or help would be appreciated.

The code in BOLD ITALICS is what I'm having trouble with.


        /// <summary>
        /// The constructor reads model data from our custom XNB format.
        /// This is called by the CustomModelReader class, which is invoked
        /// whenever you ask the ContentManager to read a CustomModel object.
        /// </summary>
        internal CustomModel(ContentReader input)
            int partCount = input.ReadInt32();

            for (int i = 0; i < partCount; i++)
                ModelPart modelPart = new ModelPart();

                // Simple data types like integers can be read directly.
                modelPart.TriangleCount = input.ReadInt32();
                modelPart.VertexCount = input.ReadInt32();
                modelPart.VertexStride = input.ReadInt32();

                // These XNA Framework types can be read using the ReadObject method,
                // which calls into the appropriate ContentTypeReader for each type.
                // The framework provides built-in ContentTypeReader implementations
                // for important types such as vertex declarations, vertex buffers,
                // index buffers, effects, and textures.
                modelPart.VertexDeclaration = input.ReadObject<VertexDeclaration>();
                modelPart.VertexBuffer = input.ReadObject<VertexBuffer>();
                modelPart.IndexBuffer = input.ReadObject<IndexBuffer>();

                // Shared resources have to be read in a special way. Because the same
                // object can be referenced from many different parts of the file, the
                // actual object data is stored at the end of the XNB binary. When we
                // call ReadSharedResource we are just reading an ID that will later be
                // used to locate the actual data, so ReadSharedResource is unable to
                // directly return the shared instance. Instead, it takes in a delegate
                // parameter, and will call us back as soon as the shared value becomes
                // available. We use C# anonymous delegate syntax to store the value
                // into its final location.
                input.ReadSharedResource<Effect>(delegate(Effect value)
                    modelPart.Effect = value;

Posted on Tuesday, January 1, 2008 3:59 PM Game Development , General Interest | Back to top

Comments on this post: Anonymous Delegates

# re: Anonymous Delegates
Requesting Gravatar...

Anonymous delegates are just a convenient way to declare a method without naming it. A delegate is something like a function pointer, you can pass it to another method (as a parameter) and execute it remotely. Usually they are just a pointer to a method defined somewhere else in the class, but in the case of anonymous ones, they are defined in place.This makes it easier, because like this you don't need to look for a name :-)

The red code can be rewritten to:

void MyMethod(Effect value)
modelPart.Effect = value;
Left by Laurent on Jan 01, 2008 4:18 PM

# re: Anonymous Delegates
Requesting Gravatar...
Anonymous delegates just allow you to declare a delegate on the fly rather than creating a separate method to handle the functionality. I'm not a VB expert, in fact I haven't written any VB code in a few years but here is the equivalent in C# without the use of the anonymous delegate.

public delegate void ReadSharedResourceDelegate(Effect value);

public void HandleReadSharedResource(Effect value)
modelPart.Effect = value;

Your code would then call it like so:
input.ReadSharedResource<Effect>(new ReadSharedResourceDelegate(HandleReadSharedResource));

This might make it easier to translate to VB.
Left by Cameron on Jan 01, 2008 4:25 PM

# re: Anonymous Delegates
Requesting Gravatar...
hey that helped!!
Left by Blogus Maximus on Jan 01, 2008 6:55 PM

# re: Anonymous Delegates
Requesting Gravatar...
Remember... C#, like C, is for "lazy" programmers. That is, for programmers that don't like typing a lot.

Or is it for efficient programmers??
Left by Augusto Ruiz on Jan 02, 2008 6:22 AM

# re: Anonymous Delegates
Requesting Gravatar...
Anonymous delegate - passing a method to a method by typing in the method (including the curly braces), rather than the name of a method declared somewhere else.

That is the basic idea. Used for "single use" cases. In C# 3.0, Lambdas will be an option for cases like this as well.
Left by Aaron Erickson on Jan 02, 2008 8:41 AM

# re: Anonymous Delegates
Requesting Gravatar...
Augusto Ruiz,
You said, "Remember... C#, like C, is for "lazy" programmers. That is, for programmers that don't like typing a lot."

Give me a break man. I think you have it backwards.
Left by John Grove on Dec 23, 2008 8:21 AM

# re: Anonymous Delegates
Requesting Gravatar...
Wow - I've been searching up and down the net for this info and finally found it here. Thanks for the simple explanation guys.
Left by Delgado Business Software on Jan 01, 2009 5:37 PM

# re: Anonymous Delegates
Requesting Gravatar...
Remember... C#, like C, is for "lazy" programmers. That is, for programmers that don't like typing a lot.

I have to disagree. It is a better tool, not necessary lazy. It is also why we use C/C++ instead of assembly language. And what is other way if you are not using this anonymouse func? Because you doesn't know how many partCount is, so you can't create a function for each part. I wrote a struct to do that instead of creating too many class at run-time especially I have tons of models to load, but I didn't profile it to see if it has less garbage collection. Lambda just like recursive, they are a better tool to solve the problem.
Left by Zhi Wen on Jun 26, 2010 4:54 AM

Your comment:
 (will show your gravatar)

Copyright © Chris G. Williams | Powered by: