C# to VB.NET

S

Schizoid Man

Hi,

I am having trouble rewriting the following C# code in VB:

// delegate declaration
public delegate double FX(double x);

//delegate usage
FX f = delegate(double x)
{
.... code...
return answer;
};

The delegate declaration looks like:

Public Delegate Function FX(ByVal x As Double) As Double

However I'm uncertain on how to rewrite the code bit. I'd appreciate any
pointers.

Thanks,
Schiz
 
A

Arne Vajhøj

Schizoid said:
I am having trouble rewriting the following C# code in VB:

// delegate declaration
public delegate double FX(double x);

//delegate usage
FX f = delegate(double x)
{
... code...
return answer;
};

The delegate declaration looks like:

Public Delegate Function FX(ByVal x As Double) As Double

However I'm uncertain on how to rewrite the code bit. I'd appreciate any
pointers.

Example:

Dim f As FX = Function(x As Double) x*x

Arne
 
S

Schizoid Man

Peter Duniho said:
You are asking how to write VB.NET code. Your question would make a lot
more sense in a VB.NET newsgroup.

That said, as far as I know, VB.NET doesn't have anonymous methods the way
C# does. It looks like they did include support for lambda expressions,
but as near as I can tell, they can only be single-statement expressions.

So I think the short answer is: you can't do that in VB.NET.

But, if you want a really reliable answer, you need to post in a newsgroup
where questions about writing VB.NET code are on-topic. The experts are
there, not necessarily here.

Thanks a lot for the information, Pete. This post was a mistake, and I had
intended to post it only in the VB group.
 
S

Schizoid Man

Note, by the way, that even in C# an anonymous method is as much a
convenience as anything else. You can always implement the same behavior
with a named method, and optionally a class to hold data the method will
need. That's basically what an anonymous method is doing behind the
scenes anyway.

Now that I think about it, a named method might be more legible code anyway.
Thanks for the suggestion.
 
S

Schizoid Man

Peter Duniho said:
Note, by the way, that even in C# an anonymous method is as much a
convenience as anything else. You can always implement the same behavior
with a named method, and optionally a class to hold data the method will
need. That's basically what an anonymous method is doing behind the
scenes anyway.

A quick follow-up - I have run into a problem with using a separate named
method. I've added the details to the thread 'Delegate declaration' in
microsoft.public.dotnet.languages.vb.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top