views:

334

answers:

3

In C++ I have a char[256] variable that is populated by a call to an external DLL which fills it with data. From there I would like to add the char[] as a ComboBox item.

char name[256];
name[0] = "76";
comboBox1->Items->Add(name);

This creates a build error because char[] is not a type of System::Object. Any ideas on converting the char[] to something I can add as an Item to a ComboBox control? Converting to a string would be just fine but I'm not sure quite how to do that. Plus if I try to create a variable such as:

string strName;

also creates an error for missing ';' before identifier 'strName'. I'm a beginner to C++ and am still getting my brain wrapped around it so thanks for any help provided!

EDIT Full code as requested:

#pragma once

namespace FMODMultipleSoundcardWindowed {

#include <string>
#include "inc/fmod.h"
#include "inc/fmod_errors.h"

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace std;


FMOD_SYSTEM     *systemA, *systemB;
FMOD_SOUND      *soundA, *soundB;
FMOD_CHANNEL    *channelA = 0, *channelB = 0;
FMOD_DSP        *dspNormalizerA, *dspNormalizerB;
FMOD_DSP        *dspCompressorA, *dspCompressorB;
FMOD_DSP        *dspEqualizerA[10], *dspEqualizerB[10];
FMOD_DSP        *dspVSTVUA, *dspVSTVUB;
FMOD_RESULT     result;
unsigned int    dspVSTVUHandleA, dspVSTVUHandleB;
unsigned int    version;
int             numdrivers, count;


public ref class Form1 : public System::Windows::Forms::Form
{
public:
    Form1(void)
    {
        InitializeComponent();

        result = FMOD_System_Create(&systemA);
        ERRCHECK(result);

        result = FMOD_System_GetVersion(systemA, &version);
        ERRCHECK(result);

        if (version < FMOD_VERSION)
        {
            MessageBox::Show("You are using an old version of FMOD!");
        }

        result = FMOD_System_GetNumDrivers(systemA, &numdrivers);
        ERRCHECK(result);

        for (count = 0; count < numdrivers; count++)
        {
            char name[256];

            result = FMOD_System_GetDriverInfo(systemA, count, name, 256, 0);
            ERRCHECK(result);

            m_objPrimaryAudioDeviceComboBox->Items->Add(name[0]);
        }
    }

    void ERRCHECK(FMOD_RESULT result)
    {
        if (result != FMOD_OK)
        {
            MessageBox::Show("FMOD Error!");
            this->Close();
        }
    }

protected:
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    ~Form1()
    {
        if (components)
        {
            delete components;
        }
    }
private: System::Windows::Forms::ComboBox^  m_objPrimaryAudioDeviceComboBox;
protected: 
private: System::Windows::Forms::ComboBox^  m_objSecondaryAudioDeviceComboBox;

private:
    System::ComponentModel::Container ^components;

pragma region Windows Form Designer generated code

    void InitializeComponent(void)
    {
        this->m_objPrimaryAudioDeviceComboBox = (gcnew System::Windows::Forms::ComboBox());
        this->m_objSecondaryAudioDeviceComboBox = (gcnew System::Windows::Forms::ComboBox());
        this->SuspendLayout();
        // 
        // m_objPrimaryAudioDeviceComboBox
        // 
        this->m_objPrimaryAudioDeviceComboBox->FormattingEnabled = true;
        this->m_objPrimaryAudioDeviceComboBox->Location = System::Drawing::Point(12, 12);
        this->m_objPrimaryAudioDeviceComboBox->Name = L"m_objPrimaryAudioDeviceComboBox";
        this->m_objPrimaryAudioDeviceComboBox->Size = System::Drawing::Size(254, 21);
        this->m_objPrimaryAudioDeviceComboBox->TabIndex = 0;
        // 
        // m_objSecondaryAudioDeviceComboBox
        // 
        this->m_objSecondaryAudioDeviceComboBox->FormattingEnabled = true;
        this->m_objSecondaryAudioDeviceComboBox->Location = System::Drawing::Point(12, 54);
        this->m_objSecondaryAudioDeviceComboBox->Name = L"m_objSecondaryAudioDeviceComboBox";
        this->m_objSecondaryAudioDeviceComboBox->Size = System::Drawing::Size(254, 21);
        this->m_objSecondaryAudioDeviceComboBox->TabIndex = 1;
        // 
        // Form1
        // 
        this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
        this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
        this->ClientSize = System::Drawing::Size(440, 426);
        this->Controls->Add(this->m_objSecondaryAudioDeviceComboBox);
        this->Controls->Add(this->m_objPrimaryAudioDeviceComboBox);
        this->Name = L"Form1";
        this->Text = L"FMOD Multiple Soundcard with VST";
        this->ResumeLayout(false);

    }

pragma endregion

};

}

A: 

Try string strName(name); (on the assumption you have your includes and namespaces right...)

UPDATE: It looks like it's System::String you need to use, not the STL string.

acron
Noted. But I think I'm missing some includes/namespaces. Again - I'm still learning and figuring this stuff out. C++ and strings blows my mind since it's always been so easy in VB and C# to work with anything I wanted.
Jeff
My apologies :) As the other answers indicate, you should be able to `#include <string>` and then use `using namespace std;` or change your call to use `std::string` instead of just `string`. Check this out: http://www.devarticles.com/c/a/Cplusplus/The-STL-String-Class/
acron
+1  A: 
string strName;

First, you need to #include <string> in order to use std::string. Secondly, you need to add using namespace std; to your unit in order to refer to a std::stringas string. Alternatively, use the plain std::string strName instead. It's a matter of taste. Using the fully qualified form avoids polluting your namespace with all the identifiers from std.

To the main question:

gcnew String( text.c_str() );

This should do the conversion. text is your std::string instance and the result of the expression is the string object you need to pass to Add.

Alexander Gessler
I'm working in VS2005 with my C++ project. Adding any reference to std is returning that 'std' : is not a class or namespace name. Something I'm missing here?
Jeff
It should be available if you `#include <string>` prior to `using namespace std;`.
Alexander Gessler
When I try this the build throws 100+ errors from file cstdio and cstdlib.
Jeff
Please show your source code - just the essential parts (your include's and using's plus the function where you're using `std::string`)
Alexander Gessler
Added. I don't have the function completed that uses std::string as I can't get the build to complete that far so I'm not writing anymore until I can figure out what Monday morning detail I'm missing this Wednesday.
Jeff
Commented on original post.
Alexander Gessler
Perfect. Took some work but we're there! Thanks for your help!!
Jeff
A: 

your second problem sounds like your missing the include for the string class, so just add #include <string> to the top of your file, you may also need to add using namespace std; or use 'std::string' depending on your system

Necrolis
When I try this the build throws 100+ errors from file cstdio and cstdlib. Am I missing something? BTW - working in VS2005 C++. Thanks for your help.
Jeff
might be because i forgot a semi-colon behind 'using namespace std' :|, fixed above
Necrolis