简体   繁体   English

将VB.NET转换为C#时出错

[英]Errors converting VB.NET to C#

Im trying convert at vb.net console file into C#, but i got 3 errors when i tried to run the console application, here are the errors. 我试图将vb.net控制台文件转换为C#,但是当我尝试运行控制台应用程序时出现3个错误,这是错误。

Error   2   Single-line comment or end-of-line expected


private static MySettings defaultInstance = (MySettings)global::System.Configuration.ApplicationSettingsBase.Synchronized(new MySettings());
    #region "My.Settings Auto-Save Functionality"
    #if _MyType = "WindowsForms"    

Error   3   Single-line comment or end-of-line expected

        get {
            #if _MyType = "WindowsForms"
            if (!addedHandler) {
                lock (addedHandlerLockObject) {
                    if (!addedHandler) {
                        My.Application.Shutdown += AutoSaveSettings;
                        addedHandler = true;
                    }
                }
            }
            #endif
            return defaultInstance;


thanx

That's a build error, not a run-time error. 那是一个构建错误,而不是运行时错误。 In c#, the #if directive is much more limited than in other languages. 在c#中,#if指令比其他语言受限制得多。 From the language reference : 语言参考

When the C# compiler encounters an #if directive, followed eventually by an #endif directive, it will compile the code between the directives only if the specified symbol is defined. 当C#编译器遇到#if指令,最后遇到#endif指令时,只有在定义了指定符号的情况下,它才会编译指令之间的代码。 Unlike C and C++, you cannot assign a numeric value to a symbol; 与C和C ++不同,不能将数字值分配给符号。 the #if statement in C# is Boolean and only tests whether the symbol has been defined or not. C#中的#if语句为布尔型,仅测试符号是否已定义。

And note also the limitations of #define. 还要注意#define的局限性。 Again from the reference : 再次从参考

You use #define to define a symbol. 您使用#define定义符号。 When you use the symbol as the expression that's passed to the #if directive, the expression will evaluate to true, as the following example shows: 当您使用符号作为传递给#if指令的表达式时,该表达式的值将为true,如以下示例所示:

#define DEBUG #定义调试

The #define directive cannot be used to declare constant values as is typically done in C and C++. #define指令不能像在C和C ++中那样用于声明常量值。 Constants in C# are best defined as static members of a class or struct. C#中的常数最好定义为类或结构的静态成员。 If you have several such constants, consider creating a separate "Constants" class to hold them. 如果您有几个这样的常量,请考虑创建一个单独的“常量”类来保存它们。

So you need to do something like 所以你需要做类似的事情

#define WindowsForms
#if (!WindowsForms)
#define WPF
#endif

at the beginning of your file(s) rather than having a single preprocessor symbol with many possible values. 在文件的开头,而不是带有多个可能值的单个预处理器符号。 For project-wide preprocessor symbols, use a /define option when compiling the project. 对于项目范围的预处理器符号,在编译项目时使用/ define选项

More here . 这里更多。

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

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