简体   繁体   English

在派生类的构造函数的主体中初始化基类成员变量

[英]Initializing base class member variables in the body of derived class's constructor

I have a C++ code which I need to rewrite to C# and looks like this: 我有一个C ++代码,需要将其重写为C#,如下所示:

class dppServerError: public dppBaseError
{
  public :
    dppServerError(DWORD ActionCode, const TCHAR* Desciption)
#ifdef POSTER_VER
        : dppBaseError(Desciption)
#else
        : dppBaseError(TEXT("Server text response: \"%s\""), Desciption)
#endif
        , m_AC(ActionCode), m_ErrorCode(dppERR_SERVER)
    {
    };

Problem is I am not using #defines in my C# code and instead using public const Enums . 问题是我没有在我的C#代码中使用#defines,而是使用了public const Enums Now, how can I duplicate above code in C#? 现在,如何在C#中复制以上代码? the #ifdefs part ? #ifdefs部分 Can't I normally initialize member variables of base class in the body of the constructor of derived class? 通常不能在派生类的构造函数的主体中初始化基类的成员变量吗? (without : syntax). (不带:语法)。 Then I could do (in C#): 然后我可以做(在C#中):

   dppServerError(uint ActionCode, string Desciption)
    {
       // Initialize base class member
       if(Globals.ConfigEnum == POSTER_VER)
          dppBaseError  = Desciption; // Can I initialize this base class ivar like this? without : syntax?
       else
         dppBaseError = "Smth else" + Desciption;

        // These are just ivars from This class
        m_AC = ActionCode;
        m_ErrorCode = dppERR_SERVER;

    };

PS. PS。 Someone told me this about #defines in C# 有人告诉我有关C#中的#defines的信息

"Be aware though: there is no guarantee that the conditional compilation symbol is the same for all projects in your solution. This will hinder reuse of your DLLs by other solutions that want different conditional compilation symbols." “但是请注意:不能保证条件编译符号对于解决方案中的所有项目都是相同的。这将阻止其他希望使用不同条件编译符号的解决方案重用DLL。”

And I decided to move to enums because I didn't really get what this meant. 我决定使用枚举,因为我并没有真正理解这意味着什么。 I am a bit new to .NET. 我对.NET有点陌生。

If dppBaseError is a field, you can initialize it as you have shown in your code. 如果dppBaseError是一个字段,则可以按照代码中所示的方式对其进行初始化。

If it's a base class constructor, you could do this: 如果它是基类构造函数,则可以执行以下操作:

dppServerError(uint ActionCode, string Desciption)
: base( (Globals.ConfigEnum == POSTER_VER) ? Desciption : "Smth else" + Desciption)
{
    ...

To get the same c++ behaviour in c#, use this: 要在c#中获得相同的c ++行为,请使用以下命令:

#if POSTER_VER
     dppBaseError  = Desciption;    
#else
    dppBaseError = "Smth else" + Desciption;
#endif

or also: 或者:

dppServerError(uint ActionCode, string Desciption)
#if POSTER_VER
    :base(Desciption)
#else
    :base("Smth else" + Desciption)
#endif 

Use a #define POSTER_VER directive, or better, define the symbol in project properties -> build -> Conditional compilation symbols. 使用#define POSTER_VER指令,或者更好,在项目属性-> #define POSTER_VER >条件编译符号中定义符号。

Usually a source file is included only in one project (unless you use "add as link " in visual studio to add same file to two or more projects), so the remarks "be aware" does not apply. 通常,源文件仅包含在一个项目中(除非您在Visual Studio中使用“添加为链接”将相同的文件添加到两个或多个项目中),所以“注意”的注释不适用。 if it does, use the same care you would use for c++ code. 如果是这样,请使用与c ++代码相同的方式。

In you c# code , the variable Global.ConfigEnum is evaulated at runtime , in my c# code, as in your c++, the symbol POSTER_VER is checked at complile time , resulting in different compiled binary files. 在您的c#代码中, 在运行时评估变量Global.ConfigEnum,在我的c#代码中,就像在您的c ++中一样, 在编译时检查符号POSTER_VER,从而生成了不同的已编译二进制文件。

see #if , #define and ProjectProperties on MSDN 请参阅MSDN上的#if# define和ProjectProperties

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM