VB.Net to C# Conversion Help

S

Steve Graddy

I am trying to convert the following VB.Net code to C# and I am getting the
compiler error:


VB.NET code:
'' Create a delegate that will be called asynchronously
Private Delegate Function GetTextData(ByVal DatabaseName As String, _ByVal
ProcName As String)

Dim async As New GetTextData(AddressOf TextProxy)
Dim asyncResult As IAsyncResult

My C# Conversion Code:
// Create a delegate that will be called asynchronously
private delegate void GetTextData(string DatabaseName, string ProcName);

GetTextData async = new GetTextData(TextProxy); <-- Causes a compiler error
IAsyncResult asyncResult;

I am receiveing a compiler error of:
A field initializer cannot reference the nonstatic field, method, or
property ''Orgbrat.DataUtility.DBSchema.TextProxy(string, string)''

Can any of you other guys help me with this conversion. You help is much
appreciated. Thanks...

Steve Graddy
(e-mail address removed)
 
K

Kevin Yu [MSFT]

Hi Steve,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that there is an compiler error when you are
trying to convert your VB.NET code to C#. If there is any misunderstanding,
please feel free to let me know.

Based on my research, you are getting this error because the following
statement cannot be used as field initializer.

GetTextData async = new GetTextData(TextProxy);

When initializing a field (such as async), we cannot use a non-static
method (just as TextProxy) to do this. For workaround, we have to put the
initializing code in the class constructor instead of putting them after
the field declaration. Here is an example:

GetTextData async;
public MyClass()
{
async = new GetTextData(TextProxy);
}

HTH. If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
T

TheNedMan

Steve,
I get something close to what you've got after running your vb code
through the vb.net to c# converter Instant C#:

private delegate object GetTextData(string DatabaseName, string
ProcName);

public GetTextData async = new GetTextData(TextProxy);
public IAsyncResult asyncResult;


The only difference I see is that the delegate must be defined as
returning an object to be consistent with your vb.net code.
 
S

Steve Graddy

Hi Kevin,

Thank you very much for your help. Your understanding of my
problem was right on target. Youe solution was exactly the problem. I
moved the initializing code into the constructor and all is well. Again
thanks for you assistance.
 

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