[英]Please help me to convert vb.NEt to C# … Addressof part is creating error for me
Imports System.Threading
Public Class clsThread
'This variable will hold the Thread Index that are passed from the main window, when we created it
Private m_ThreadIndex As Integer
'This is local thread variable. We will send the value of this variable to the parent form
Private m_Counter As Integer = 0
'We will need this variable to pass argument to the method of the main window
Private m_Args(1) As Object
'This will hold the ref to the main window,
Private m_MainWindow As Form
'Here we are going to call the method ReceiveThreadMessage() of the main form.
'So the declaration of the delete should be same as ReceiveThreadMessage()
Private Delegate Sub NotifyMainWindow(ByVal ThreadIndex As Integer, ByVal Counter As Integer)
'We need an object of this deletegate
Private m_NotifyMainWindow As NotifyMainWindow
Public Sub New(ByVal ThreadIndex As Integer, ByRef MainWindow As frmtest)
m_ThreadIndex = ThreadIndex
m_MainWindow = MainWindow
'We need to point our delegate to the Method, which we want to call from this thread
m_NotifyMainWindow = AddressOf MainWindow.ReceiveThreadMessage
End Sub
Public Sub StartThread()
While True
m_Counter = m_Counter + 1
'we need to create this array such a way, that it should contains the no of elements, that we need
'to pass as the arguments.
'Here we will pass two arguments ThreadIndex and Counter, so we took the array with 2 elements.
'Now we need to place the variable to the appropriate position of this array.
'Like : Our First Argument is ThreadIndex, so we will put ThreadIndex into the first element and
'm_counter into the second element.
'Basically you have to put the variables into the array with the same sequence that is there in the
'argument list
ReDim m_Args(1)
m_Args(0) = m_ThreadIndex
m_Args(1) = m_Counter
'Call the notificaiton method on the main window by the delegate
m_MainWindow.Invoke(m_NotifyMainWindow, m_Args)
'wait for some time before continuing loop
Thread.Sleep(1000)
End While
End Sub
End Class
看一下
要在C#中創建委托實例,只需指定委托類型,方法以及(如果要創建一個目標,使其與當前實例不同的實例或類型)目標即可。 例如,以下每個創建一個ThreadStart委托:
ThreadStart x1 = new ThreadStart(SomeInstanceMethod); ThreadStart x2 = new ThreadStart(AnotherType.SomeStaticMethod); ThreadStart x3 = new ThreadStart(someVariable.SomeInstanceMethod);
還要將此作為參考
此轉換后的代碼應為您提供幫助
using System.Threading;
using System.Windows.Forms;
public class clsThread
{
//This variable will hold the Thread Index that are passed from the main window, when we created it
private int m_ThreadIndex;
//This is local thread variable. We will send the value of this variable to the parent form
private int m_Counter = 0;
//We will need this variable to pass argument to the method of the main window
private object[] m_Args = new object[2];
//This will hold the ref to the main window,
private Form m_MainWindow;
//Here we are going to call the method ReceiveThreadMessage() of the main form.
//So the declaration of the delete should be same as ReceiveThreadMessage()
private delegate void NotifyMainWindow(int ThreadIndex, int Counter);
//We need an object of this deletegate
private NotifyMainWindow m_NotifyMainWindow;
public clsThread(int ThreadIndex, ref frmtest MainWindow)
{
m_ThreadIndex = ThreadIndex;
m_MainWindow = MainWindow;
//We need to point our delegate to the Method, which we want to call from this thread
m_NotifyMainWindow = MainWindow.ReceiveThreadMessage;
}
public void StartThread()
{
while (true)
{
m_Counter = m_Counter + 1;
//we need to create this array such a way, that it should contains the no of elements, that we need
//to pass as the arguments.
//Here we will pass two arguments ThreadIndex and Counter, so we took the array with 2 elements.
//Now we need to place the variable to the appropriate position of this array.
//Like : Our First Argument is ThreadIndex, so we will put ThreadIndex into the first element and
//m_counter into the second element.
//Basically you have to put the variables into the array with the same sequence that is there in the
//argument list
int[] m_Args = new int[2];
m_Args[0] = m_ThreadIndex;
m_Args[1] = m_Counter;
//Call the notificaiton method on the main window by the delegate
m_MainWindow.Invoke(m_NotifyMainWindow, m_Args);
//wait for some time before continuing loop
Thread.Sleep(1000);
}
}
}
在C#中,方法調用需要括號,並且通過省略括號,您基本上是在傳遞方法本身。
如果您使用的是C#3.5, 並且方法_signature_與分配給它的委托匹配 ,則將自動為您創建委托(在inferrence類型的幫助下)
因此,此行:
m_NotifyMainWindow = AddressOf MainWindow.ReceiveThreadMessage
當轉換為C#時,將簡單地變為:
m_NotifyMainWindow = MainWindow.ReceiveThreadMessage;
假設MainWindow
是一個實例,而ReceiveThreadMessage
是一個實例方法, 或者它是一個靜態類,而ReceiveThreadMessage
是一個靜態方法。
基本上,只需刪除 AddressOf運算符,在行的末尾添加分號,您將獲得等效的C#版本。
如果僅是解決問題的地址,那您應該在幾秒鍾內完成。 隨便吧。 c#允許您直接將方法分配給委托,因此
myDelegate = AddressOf obj.SomeMethod
在VB.NET中成為
myDelegate = obj.SomeMethod;
要獲得更多見解,可以使用Reflector(RedGate軟件)等工具,在其中可以將其反編譯為c#和VB.NET。
看一下
如何使用代碼轉換器 1-通過按2粘貼或在左側鍵入代碼,將轉換方向從C#轉換為VB,從VB轉換為C#。 要轉換整個文檔,請選擇上載文件。 3-點擊橙色按鈕。 轉換后的代碼將顯示在右側,或者將有指向轉換后的文件的鏈接供下載。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.