[英]C# accessing a class'es member from private static void
我需要同時使用“ public int”(不能100%保證)和“ private static void”(100%可以保證!)
但是,我無法從私有靜態void內訪問類的成員數據。
有人可以告訴我如何解決此問題或解決方法嗎?
非常感謝你的幫助!
namespace MyDLL
{
public class clsDLL
{
ThirdPartyAPI _api = new ThirdPartyAPI();
double _X = 0;
//My C# project is a COM DLL that will be called by other applications, so I have chosen "public int" here
public int open(string uKey)
{
int iRet = _api.Open(uKey);
return iRet;
}
//This is a callback that will be called by "_api"
private static void CallBack_MoveDetected(ref MoveData data, IntPtr userData)
{
_X=data.positionX; //this does not work. I can not access "_X" from here.
}
}
}
不知道這是否有意義,但是您可以將課程更改為
namespace MyDLL
{
public class clsDLL
{
ThirdPartyAPI _api = new ThirdPartyAPI();
double _X = 0;
public double X
{
get{ return _X;}
set{ _X = value;}
}
public int open(string uKey)
{
int iRet = _api.Open(uKey);
return iRet;
}
private static void CallBack_MoveDetected(ref MoveData data,
IntPtr userData, clsDLL instance)
{
instance.X=data.positionX;
}
}
}
回到您最初的問題。 沒有靜態方法無法在沒有類實例的情況下直接使用實例變量。 想一想。 靜態方法不需要聲明任何實例,因此,如果您嘗試執行的任何方式都是可行的,那么應該從哪個實例中選出一個靜態方法,以該靜態方法讀取變量_X的值?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.