简体   繁体   English

#define-将C ++迁移到C#或VB.Net

[英]#define - Migrating C++ to C# or VB.Net

I need some advice on how to do the following in either C# and VB.net. 我需要一些有关如何在C#和VB.net中执行以下操作的建议。

In C++, in my header file I do the following: 在C ++中,在头文件中执行以下操作:

#define  StartButtonPressed  Input[0]==1 // Input is an array declared in .cpp file

In my .cpp file, i have a code something like this: 在我的.cpp文件中,我有类似以下的代码:

if(StartButtonPressed)
    // do something

The reason of me doing so is so that my code is easier to read. 我这样做的原因是为了使我的代码更易于阅读。 I tried the same thing in C# but it got error. 我在C#中尝试了相同的操作,但出现错误。 How could I do the same thing in C# and VB.Net? 如何在C#和VB.Net中做同样的事情? Please advice. 请指教。 Thanks. 谢谢。

There is no good reason to use a macro for this in C++; 没有充分的理由在C ++中为此使用宏。 you could just as easily make it a function and the code would be far cleaner: 您可以轻松地使其成为函数,并且代码将更加简洁:

bool IsStartButtonPressed()
{
    return Input[0] == 1;
}

Input should also probably be passed as an argument to the function, but it's hard to tell exactly where that is coming from. Input也可能应该作为参数传递给函数,但是很难确切说明输入的来源。

You're best off creating a property in your class 最好在课堂上创建媒体资源

protected bool StartButtonPressed {
   get { return Input[0] == 1; }
}

then your code can be as before 那么你的代码可以像以前一样

.
.
.
if(StartButtonPressed) {

.
.
.
}

However for consistency with the .net framework I'd suggest calling the property IsStartButtonPressed 但是为了与.net框架保持一致,我建议调用属性IsStartButtonPressed

If you need to to be evaluated at the point of the if statement then you really need a function or a property. 如果您需要在if语句的位置进行求值,那么您确实需要一个函数或属性。 However is this is one time evaluation you can use a field 但是,这是一次评估,您可以使用一个字段

bool isStartButtonPressed = Input[0] ==1;

If you want may classes to have this functionality then I'd recommend a static function from another class, something like 如果您希望类具有此功能,那么我建议您从另一个类中推荐一个静态函数,例如

public static class ButtonChecker {
        public static bool IsPressed(int[] input) {
            return input[0] == 1;
        }
    }

Then you call it anywhere with 然后用

if(ButtonChecker.IsPressed(Input)) {
  .
  .
}

But ultimately you cannot use macro's like you're used in C/C++. 但是最终您不能像在C / C ++中那样使用宏。 You shouldn't be worried about performance of properties and functions like this as the CLR jit compiler implementation is very very good for them 您不必担心这样的属性和函数的性能,因为CLR jit编译器实现对它们非常有用

Here is an example program: 这是一个示例程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace ConsoleApplication1 {

    public static class ButtonChecker {
        public static bool IsPressed(int[] input) {
            return input[0] == 1;
        }
    }

    static class Program {

        public static void Main(){
            int[] Input = new int[6] { 1, 0, 2, 3,4 , 1 };

            for(int i = 0; i < Input.Length; ++i){
                Console.WriteLine("{0} Is Pressed = {1}", i, ButtonChecker.IsPressed(Input));
            }
            Console.ReadKey();
        }
    }
}

You could use an enum 您可以使用一个枚举

public enum buttonCode
{
   startButton = 0,
   stopButton = 1
   // more button definitions
}  

Then maybe one function 然后也许一个功能

public bool IsButtonPressed(b as buttoncode)
{
return Input[b] == 1;
}

Then your calls look like: 然后您的呼叫如下所示:

if IsButtonPressed(buttonCode.StartButton) {   }

The only changes needed to switch button codes are then in the enum, not spread across multiple functions. 这样,切换按钮代码所需的唯一更改就在枚举中,而不是分散在多个功能上。

Edited to Add: 编辑添加:

If you want individually named functions, you could do this: 如果要使用单独命名的函数,可以执行以下操作:

public bool IsStartButtonPressed()
{
return Input[buttonCode.StartButton] == 1;
}

Still, all of the edits would be in the enum, not the functions. 尽管如此,所有编辑都将在枚举中,而不是函数中。

Bjarne Stroustrup wrote: Bjarne Stroustrup写道:

The first rule about macros is: Do not use them if you do not have to. 关于宏的第一条规则是:不必使用它们。 Almost every macro demonstrates a flaw in the programming language, in the program, or in the programmer. 几乎每个宏都表明编程语言,程序或程序员中的缺陷。

It's worth noting two things here before saying anything else. 在说其他任何事情之前,这里需要注意两件事。 The first is that "macro" can mean a very different thing in some other languages; 首先,在某些其他语言中,“宏”的含义可能完全不同。 one would not make the same statement about Lisp. 人们不会对Lisp发表相同的说法。 the second is that Stroustrup is willing to take his share of the blame in saying that one reason for using macros is "a flaw in the programming language", so it's not like he's just being superior in condemning their use. 第二个原因是Stroustrup愿意承担责任,他说使用宏的一个原因是“编程语言中的缺陷”,所以这并不意味着他只是在谴责使用宏方面表现出色。

This case though isn't a flaw in the programming language, except that the language lets you do it in the first place (but has to, to allow other macros). 这种情况虽然不是编程语言中的缺陷,但它首先允许您使用它(但必须允许其他宏)。 The only purpose of this macro is to make the code harder to read. 该宏的唯一目的是使代码更难阅读。 Just get rid of it. 摆脱它。 Replace it with some actual C# code like: 将其替换为一些实际的C#代码,例如:

private bool StartButtonPressed
{
  get
  {
    return Input[0]==1
  }
}

Edit: 编辑:

Seeing the comment above about wanting to be faster to code, I would do something like: 看到上面有关希望更快地编写代码的评论,我将执行以下操作:

private enum Buttons
{
  Start = 0,
  Stop = 1,
  Pause = 2,
  /* ... */
}

private bool IsPressed(Buttons button)
{
  return Input[(int)button] == 1;
}

And then call eg IsPressed(Buttons.Start) . 然后调用例如IsPressed(Buttons.Start) Then I'd fix the C++ to use the same approach too (in C++ I would even be able to leave out the Buttons. where I wanting particularly great concision). 然后,我也将C ++修复为也使用相同的方法(在C ++中,我什至Buttons.在我想要特别简洁的地方)。

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

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