Link to home
Start Free TrialLog in
Avatar of louisiana_blues
louisiana_blues

asked on

How do I convert this csharp code to vb.net? (anonymous methos)

I have a task of converting a class to vb.net (mine is not to ask why, just to ask how high!)

The following snippet below has me stumped.

What I need to do is come up w/an acceptable conversion.  I need an experts expert!

500 points to whomever can convert the below code to vb.net...

_oQueue.Send(delegate(object dummy)
            {
                InitializeStateMachine(oinitialstate);
            }, null);

Open in new window

Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

Here is an example of delegates in VB.NET:
http://www.developerfusion.com/article/5251/delegates-in-vbnet/
Basically:

Delegate Sub MyDelegate(ByVal dummy As Object)

Then in your original code

Dim del As New MyDelegate(AddressOf InitializeStateMachine)
_oQueue.Send(del, null)
Avatar of ITHelper80
ITHelper80

_oQueue.Send(Function(ByVal dummy As Object) Do
    InitializeStateMachine(oinitialstate)
End Function, Nothing)

However VB does not support anonymous methods/lambda expressions with a statement body

Here is a sweet convertor I use all the time.
http://www.developerfusion.com/tools/convert/csharp-to-vb/ 
Very nice.  I miss typed null --> should be Nothing.  Like ItHelper80's converter.  Have to add that to my favorites.
Avatar of louisiana_blues

ASKER

I understand that vb does not support anonymous functions.  What I am looking for is a *close as possible* vb.net conversion...whether by naming a function and calling it or whatever.

I belive mwvisa is very close, but he doesn't state specifically what the solution is.  Could you do that?  Then I can award you the points!

And yes, I am well familiar with that csharp conversion link--I'd bee screwd if I had to convert the code line by line! ;-)
What I mean mvisa is that something is missing.  When I copy and paste your sollution into the class I get errors.  Could you go the extra dollar and convert the two lines of code completely to vb?
I was suggesting using Delegates.  The tricky part is you can use the Invoke method to pass the variable to the delegate, but in your code you have to pass the delegate itself to the send method.

http://www.codeproject.com/KB/vb/Delegate.aspx

Is InitializeStateMachine a method you have defined in this class.  I have not used the StateMachine class before, so this might be part of that but just curious as that is the missing part.  The AddressOf piece of the delegate instantiation calls to the actual implementation method that has same signature as delegate declaration.

Therefore, an InitializeStateMachine sub with parameter of Object must exist already.
Hey Visa:

It's two lines of code.  I have no clue what you are spaking of.  If you could just convert those two lines of code into acceptable vb, then I will give you the points!  I can't seem to get that last important part....
I am also a visual woman...so it helps me understand something when I *see* code that is working...
Please provide the remainder of the C# code...

Still have question on:
"Is InitializeStateMachine a method you have defined in this class?"
Hey Visa:

Yes, InitalizeStateMachin is a function iin the same class as the code you see.  There's really not any other code that is relevant to the above anon function---just a definition of the _oQueue object (which is a Queue).

Hope this helps!
From what I've read one should be able to replace the anonymous function with a real function, then mimic the rest of it.  Of course, the 'mimic the rest of it' leaves me stumped!

If you do name a function, just call it MyAnonymousFunction so we are both on the same page.  And Thank you so much!
Well, haven't heard from anyone so I'll go ahead and post more code--though once you see it you'll understand it has nothing to do with the question, and thus is just filling up another question w/confusing posts for an expert who would know the answer except for wading through all of the non-answer answers.

You can see all the relevant code below.  I need to replace the anon function w/a named one...The IntializeStateMachine is just a function in the class.
        // Used for queuing events.
        private DelegateQueue queue = new DelegateQueue();
 
 
        protected override void Initialize(State initialState)
        {
            queue.Send(delegate(object dummy)
            {
                InitializeStateMachine(initialState);
            }, null);
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That is about the extent of my understanding of the delegate situation you are dealing with here.  If you need further assistance you can use the request attention button to draw in higher ranked experts in this arena.
Well visa, I'm going to give you the points, since you are the ONLY one who even responded, and (I'm assuming your code works--I just read you last message, and it seems as if you don't have confidence in this?  Either way you get the points, but could you tell me if you know this works or if it doesn't?  And why it wouldn't work?  Now I'm worried...
The only reason I don't have confidence in it is that I don't have the DelegateQueue class on my system; therefore, I could not compile and test that this is indeed what you are needing.  From what you showed me and that I know of the DelegateQueue, it takes a delegate method as its argument.  The send method takes delegate and then an object array for the parameters to pass to that function/sub.

I put the code above in the correct places...just makes me feel better when I actual can say I compiled the code and this is what you will get.  Since I didn't do that, I am just being honest with you as to not waste any time getting more experts involved who may have this class already setup and working...