I
I
Ilya2017-03-03 09:03:02
.NET
Ilya, 2017-03-03 09:03:02

How to create an array of structures with n length?

Good day. I don't really know C++, but I really need to write one library for use in C#. I can't create an array with a length dependent on another array.
Error: expression must be a constant value

/////Класс  для get_param_value_bynames ///////////////
public ref class P_BYNAME_DATA
{
public:
    String^ PntName;         //имя точки
    String^ PrmName;         //имя параметра
    short   nPrmOffset;      //смещение параметра
    PARvalue*   pupvValue;   //(входной/выходной) значение пара-метра
    short nType;             //(входной / выходной) тип значения
    long    fStatus;         //состояние каждого до-ступа к значению
    P_BYNAME_DATA() {}
    P_BYNAME_DATA(String^ name) { PntName = name; }
    virtual ~P_BYNAME_DATA() {}
};

///Класс для обращения 
public ref class Hscnapi
{
private:
    static int status;

public:
    static int get_param_value_bynames(String ^Server, array<P_BYNAME_DATA^>^ prmdata)
    {           
        int len = prmdata->Length;
        PARAM_BYNAME_DATA rgprmbd[i]; //Ошибка: выражение должно иметь константное значение 
    }

}

In my opinion, you need to create a pointer to determine the size of the array or something else. But I dont know how.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Fat Lorrie, 2017-03-03
@Free_ze

Moreover, you have an undeclared variable i =)
Let's say you made a mistake by cutting out extra code, let this variable be calculated in your runtime (i.e. it is not a constant). Inside the method, you are trying to create an array on the stack . To do this, it is important for the compiler to know in advance (at compile-time) which buffer to reserve there, i.e. a constant or constant expression is required, which is what the error message says.
In your case, you need a structure like:

array< PARAM_BYNAME_DATA^ >^ rgprmbd 
                   = gcnew array< PARAM_BYNAME_DATA^ >(i);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question