CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2004
    Location
    Philippines
    Posts
    58

    Setting size of Multi-dimensional Array dynamically

    good day to all! i would like to use a multi-dimensional array which size and range are setup dynamically. the array will be placed in the public location of my class because this array will be the one to catch data from another class (having a multi-dimensional array too)..

    i try CStringArray but i cant set the additional range for it
    Normal Use:
    CStringArray cArrRowCol[5];

    i want was:
    CStringArray cArrRowCol[i]; which " i " varies from what will i give to its value

    any idea of it? thanks

  2. #2
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443
    This FAQ might help...
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  3. #3
    Join Date
    Aug 2002
    Posts
    18
    Hi

    try this


    typedef CStringArray* PSTRARRAY;

    PSTRARRAY *cArrRowCol;

    int x = 5;
    cArrRowCol = new PSTRARRAY[x];

    for(int i=0;i<5;i++)
    {
    for(int j=0;j<3;j++)
    {
    CString str;
    str.Format("%d %d",i,j);
    cArrRowCol[i]->Add(str);
    }
    }

    hope this could solve your problem


    VladimirV

  4. #4
    Join Date
    Feb 2004
    Location
    Philippines
    Posts
    58
    To VladimirV:
    i already try your approach the multi-dimensional array unable to catch the data that i have thrown. here is my code snipet:

    //header file
    typedef CStringArray *PSTRARRAY;

    class CReportUI : public CDialog
    {
    // Construction

    public:
    PSTRARRAY *cArrRowCol;
    CManagerialReport cManage;
    CStringArray mv_sArrType;
    CString m_sDialogTitle;
    CReportUI(CWnd* pParent = NULL); // standard constructor
    }

    //ccp file
    BOOL CReportUI::OnInitDialog()
    {
    CDialog::OnInitDialog();
    SetWindowText(m_sDialogTitle);
    SetTypeSelection();
    cArrRowCol = new PSTRARRAY[5];
    cArrRowCol[0]->Add("hello"); // unable to catch the value
    // TODO: Add extra initialization here

    return TRUE; // return TRUE unless you set the focus to a control
    // EXCEPTION: OCX Property Pages should return FALSE
    }


    To Gabriel Fleseriu: How can i adopt the STL code to my MFC project? i dont have any idea of it...

  5. #5
    Join Date
    Aug 2002
    Posts
    18
    sorry, there was a mistake, this is working well

    typedef CStringArray* PSTRARRAY;

    void CCg1Dlg::OnButton1()
    {
    PSTRARRAY cArrRowCol;
    int x = 5;
    cArrRowCol = new CStringArray[x];

    for(int i=0;i<5;i++)
    {
    for(int j=0;j<3;j++)
    {
    CString str;
    str.Format("%d %d",i,j);
    cArrRowCol[i].Add(str);
    }
    }

    TRACE("%s",cArrRowCol[0].GetAt(0));
    TRACE("%s",cArrRowCol[1].GetAt(0));
    TRACE("%s",cArrRowCol[2].GetAt(0));
    }

    VladimirV

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured