How do I convert a long variable to const Int ?

I get back numOfContext as a long (req. by this call). I then want to create 2 arrays based on this size but I need const int for array declaration. How can I do this Thanks.

pSYContextContainer->get_Count(&numOfContext);




Answer this question

How do I convert a long variable to const Int ?

  • _ag

    Depending on the compiler settings, int and long are 4 bytes.

    sizeof()

    Kuphryn


  • Dennis Cheng _ MSFT

    So how can I get the following to work Thanks.

    pSYContextContainer->get_Count(&numOfContext);

    int contextId[numOfContext];



  • fizban2

    Thank you very much and that seems to be working just great for the int array. I also need to delare an arry that is the size of numOfConext and holds strings. But, I'm not getting return back as string. I'm getting BSTR and I can cast it to _bstr_t. I tried declaring an array of _bstr_t but it doesn't seem to work when I compiled. It does like it when I delare the CString array but I don't know how to get my BSTR string to CString. Appreciate it if you can suggest some way to work around this. Thank you.

    pDisp = V_DISPATCH(&var);

    pDisp->QueryInterface(IID_IADs, (void**)&pADs);

    pDisp->Release();

    hr = pADs->get_Name(&bstr);

    if(SUCCEEDED(hr))

    {

    _bstr_t contextName(bstr);//This includes the CN=



  • Ahmed.H.Hassan

    The conversions between numbers are almost always automatic so you usually don't have to worry about things like that. Long -> const int is also automatic since it will automatically add the constant to it. Of course, you do run into problems sometimes if you are converting down and don't check your bounds, ie int -> short could result in some weird results because you are converting from 4 bytes to 2 bytes and the compiler just discards the upper word in these cases.

    What you want to do here is to get a value at runtime and create an array out of it. The compiler can't do that because it needs to know the size of the array while compiling to allocate memory for it. What you want to do though needs to be done a different way.

    For this to work you need to define it like this.

    int* contextid;

    contextid = new int[numOfContext];

    This will create the array the way you want it and you will be able to access it as an array by using contextid[index] since array syntax works with pointers too.



  • elect_son

    AlphaDavis wrote:

    I get back numOfContext as a long (req. by this call). I then want to create 2 arrays based on this size but I need const int for array declaration. How can I do this Thanks.

    pSYContextContainer->get_Count(&numOfContext);

    Is this managed code or native code

    Here's some code showing you how to do this for a native as well as for a managed array :

    int count = 10;
    char* native = new char[count];//native array
    delete[] native;
    array<char>^ managed = gcnew array<char>(count);//managed array



  • msreeni

    AlphaDavis wrote:

    So how can I get the following to work Thanks.

    pSYContextContainer->get_Count(&numOfContext);

    int contextId[numOfContext];

    You cannot do that. The value must be known at compile time - which it won't be in this case.



  • Zou Kok Man

    Thank you for the reply. The code is unmanaged. The number returned is long in numOfContext. I don't see how your sample code will convert a long variable, or assign it, to a "const int" which is requred for declaring an int array and a char array. Thank you.

  • How do I convert a long variable to const Int ?