在类中调用类函数?

3

我正在将C ++代码翻译为C#,现在我遇到了这个问题,所以我无法调用我需要调用的函数。

public struct TargetList_t
    {
        public float Distance;
        public float[] AimbotAngle = new float[3];
        public float[] aimbotAngle = new float[3];
        public float[] myCoords = new float[3];
        public float[] enemyCoords = new float[3];

        public TargetList_t(float[] aimbotAngle, float[] myCoords, float[] enemyCoords)
        {
            Distance = Get3dDistance(myCoords[0], myCoords[1], myCoords[3], enemyCoords[0], enemyCoords[1], enemyCoords[2]);

            AimbotAngle[0] = aimbotAngle[0];
            AimbotAngle[1] = aimbotAngle[1];
            AimbotAngle[2] = aimbotAngle[2];
        }
    };

这是我的课程。

TargetList_t TargList;

这就是我尝试达成它的方式。

1
你需要调用构造函数吗?就像这样 TargetList_t TargList = new TargetList_t(aimbotAngle, myCoors, ... 其他参数); - Ilya Ivanov
1
顺便提一下:你想要的很可能是一个“类”,而不是一个“结构体”。C++的结构体与C#的不同。 - dcastro
1个回答

1
在C++中,在哪里可以调用默认构造函数?
TargetList_t TargList;

在C#中,您总是希望使用new关键字:
// init your constructor parameters
float[] aimbotAngle = new float[1]; 
float[] myCoords = new float[1]; 
float[] enemyCoords = new float[1]; 

// call constructor
var targList = new TargetList_t(aimbotAngle , myCoords , enemyCoords);

注:

  • 你应该将struct更改为class,因为这是(有争议的)它看起来像什么

  • 在C++中,您需要添加后缀或前缀来说明它是typeint或其他类型,在C#中,通常不这样做,因此您的类名应该是TargetList。此外,List是.Net框架中一个众所周知的类,因此我希望这个类要么从它派生,要么从名称中删除List单词,如TargetList_t => Targets

  • 使用公共属性而不是公共字段

    public float Distance; => public float Distance {get;set;}


在这种情况下不会起作用。这个类没有默认构造函数。 - Selman Genç

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接