Barcode
Barcode

Reputation: 970

How to keep track or trace the elements of array in MFC c++ visual studio

I'm fairly new to visual studios/window aplications, so im not used to not having a command line.

Basically I have for example a CString m_storeEx = "12 + 2 - 4 " and so I used tokenize method to put each number and operator into an array by doing this:

CStringArray arr;
CString resToken= m_StoreEx.Tokenize(_T(" "),curPos);    

while (resToken != _T(""))
{
    arr.Add(resToken);
    resToken = m_StoreEx.Tokenize(_T(" "), curPos);
}

I want the end result to be an array like this arr = ['12', '+', '2', '-', '4']

I just want to check if im adding the elements in right. Usually if I code with something that has a commandline, I'd just make a for loop, and print the array out to check. But I'd know to do that in MFC visual basics since there is no command line. I tried using the debugging tool to check the value of the array, but it just gave me weird numbers and letters.

Upvotes: 1

Views: 545

Answers (3)

IInspectable
IInspectable

Reputation: 51489

As pointed out by l33t in his/her answer, Visual Studio has provisions to extend and customize the visualization of objects in the debugger. Details are available under Create custom views of native objects. By default, Visual Studio doesn't ship a CStringArray visualizer, so you are left with writing your own.

The following is a simple visualizer, that displays both the collection size as well as its contents:

<?xml version="1.0" encoding="utf-8"?>
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
  <Type Name="CStringArray">
    <DisplayString>{{size={m_nSize}}}</DisplayString>
    <Expand>
      <ArrayItems>
        <Size>m_nSize</Size>
        <ValuePointer>m_pData</ValuePointer>
      </ArrayItems>
    </Expand>
  </Type>
</AutoVisualizer>

There are several ways to enable this visualizer. The easiest deployment is to copy the XML code above to a file (e.g. CStringArray.natvis) and copy that file to the directory %USERPROFILE%\My Documents\Visual Studio 2012\Visualizers\. Other alternatives are documented in the link above.

Once deployed, stepping through the following code

CStringArray str;
str.Add( _T( "12" ) );
str.Add( _T( "+" ) );
str.Add( _T( "2" ) );
str.Add( _T( "-" ) );
str.Add( _T( "4" ) );

produces this visualization in the debugger: enter image description here

Upvotes: 3

l33t
l33t

Reputation: 19966

If you desperately need this feature, you could always create a custom view in Visual Studio.

  1. Close Visual Studio.
  2. Open the file autoexp.dat from [Visual Studio Installation Folder]\Common7\Packages\Debugger in Notepad.
  3. Copy and paste the following code snippet into autoexp.dat. Specifically, paste it on the first line of the file.

    CStringArray{
        children
        (
            #array
            (
                expr : ($e.m_pData[$i]),
                size : ($e.m_nSize)
            )
        )
    }
    

Upvotes: 1

Jabberwocky
Jabberwocky

Reputation: 50831

Unfortunately the Visual Studio debugger cannot show the content of a CStringArray, but ther are some helper trace function that you can call:

...
while (resToken != _T(""))
{
    arr.Add(resToken);
    resToken = m_StoreEx.Tokenize(_T(" "), curPos);
}

afxDump.SetDepth(1);
arr.Dump(afxDump);

This will show the content of the arr array in the Visual Studio output window like this:

a CStringArray at $0032FB60
with 5 elements
    [0] = 12
    [1] = +
    [2] = 2
    [3] = -
    [4] = 4

Upvotes: 0

Related Questions