Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how could i keep the value of a specific pointer?

Status
Not open for further replies.

jayjay60

Programmer
Jun 19, 2001
97
FR
In my dialog app i have 2 functions which use ADO connection to catch value from a database, and which put them into a "C" pointer of pointer and a regular pointer. I want after in a third function use the 2 pointers, which represent some of the input data. As i do it with 3 different button which each of them call one and only one function.

For example the first, called OnLoadCorrel, put a whole matrix into a **pCorrel, the second one called On LoadVol, put a whole array into a *pVol. And, finally, the 3 function use the values needed coming from the matrix and the array. It's at this point of my app that there's a pb, because when i call the third function, the app doesn't recognize (or know) the value into both the pointers (perhaps the value have been lost?!?)

So you could see the following lost, and i hope that soeone could find if what is wron in my app.

/////////////////////////////////////////////////////////
So, in the CalcRiskPortDlg.cpp::

void CCalcRiskPortDlg::OnLoadVol()
{
UpdateData(TRUE);
::CoInitialize(NULL);
LoadTables Table(m_dlgSector);
int i;
// _variant_t IndexCol;
CString strValue;

try
{
_ConnectionPtr pConnection=NULL;
CString strTemp;
strTemp.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\CalcRiskPort\\vol_correl1.mdb;");
_bstr_t strCnn(strTemp);
TESTHR(pConnection.CreateInstance(__uuidof(Connection)));
pConnection->Open(strCnn,"","",adConnectUnspecified);

_RecordsetPtr pTp("ADODB.Recordset");
try
{

pTp->Open("Volatilité",strCnn,adOpenStatic,adLockOptimistic,adCmdTableDirect);

if(!pTp->EndOfFile)
{
for(i=1;i<=m_dlgSector;i++)
{
pTp->MoveFirst();
pTp->Move(i-1);
//strIndexCol=Table.ConvIntToVar((double)j);
//strIndexCol=strIndexCol+strT;
//_variant_t IndexCol(strIndexCol);
*(Table.pVol+i)=pTp->Fields->GetItem(&quot;Volatilité&quot;)->Value;
}
}

//dValue=pTp->Fields->GetItem(&quot;N° Titre&quot;)->Value;
//Parameter2=pTp->Fields->GetItem(IndexColumn)->Value;

pTp->Close();
}
catch(_com_error &e)
{
AfxMessageBox(&quot;a pas bon&quot;);
}
m_dlgValue=Table.pVol[3];
strValue.Format(&quot;%.9f&quot;,m_dlgValue);
CEdit* PEdit=(CEdit*)GetDlgItem(IDC_VALUE);
PEdit->SetWindowText(strValue);
//Table.free_dmatrix(Table.pCorrel,1,m_dlgPortWeight,1,m_dlgPortWeight);
pConnection->Close();


}
catch(_com_error &e)
{
AfxMessageBox(&quot;a pas bon&quot;);
}


::CoUninitialize();

// TODO: Add your control notification handler code here

}

void CCalcRiskPortDlg::OnLoadCorrel()
{
UpdateData(TRUE);
::CoInitialize(NULL);
LoadTables Table(1,m_dlgSector,1,m_dlgSector);
int i,j;
// _variant_t IndexCol;
CString strValue,strT,strIndexCol;
strT=&quot;t&quot;;

//Table.pCorrel=Table.dmatrix(1,m_dlgPortWeight,1,m_dlgPortWeight);
try
{
_ConnectionPtr pConnection=NULL;
CString strTemp;
strTemp.Format(&quot;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\CalcRiskPort\\vol_correl1.mdb;&quot;);
_bstr_t strCnn(strTemp);
TESTHR(pConnection.CreateInstance(__uuidof(Connection)));
pConnection->Open(strCnn,&quot;&quot;,&quot;&quot;,adConnectUnspecified);

_RecordsetPtr pTp(&quot;ADODB.Recordset&quot;);
try
{

pTp->Open(&quot;Correlation&quot;,strCnn,adOpenStatic,adLockOptimistic,adCmdTableDirect);

if(!pTp->EndOfFile)
{
for(i=1;i<=m_dlgSector;i++)
{
for(j=1;j<=m_dlgSector;j++)
{
pTp->MoveFirst();
pTp->Move(i-1);
strIndexCol=Table.ConvIntToVar((double)j);
strIndexCol=strIndexCol+strT;
_variant_t IndexCol(strIndexCol);
*(*(Table.pCorrel+i)+j)=pTp->Fields->GetItem(IndexCol)->Value;
}
}
}

//dValue=pTp->Fields->GetItem(&quot;N° Titre&quot;)->Value;
//Parameter2=pTp->Fields->GetItem(IndexColumn)->Value;

pTp->Close();
}
catch(_com_error &e)
{
AfxMessageBox(&quot;a pas bon&quot;);
}
m_dlgValue=Table.pCorrel[3][7];
strValue.Format(&quot;%.9f&quot;,m_dlgValue);
CEdit* PEdit=(CEdit*)GetDlgItem(IDC_VALUE);
PEdit->SetWindowText(strValue);
//Table.free_dmatrix(Table.pCorrel,1,m_dlgPortWeight,1,m_dlgPortWeight);
pConnection->Close();


}
catch(_com_error &e)
{
AfxMessageBox(&quot;a pas bon&quot;);
}


::CoUninitialize();
// TODO: Add your control notification handler code here

}

void CCalcRiskPortDlg::OnCalcvar()
{
UpdateData(TRUE);
::CoInitialize(NULL);

LoadTables Table;
double Var;
double *pTabVar=new double[pow(2,m_dlgSector)];
CString strValue;

int *pTabBinPort=new int[m_dlgAsset];
int *pTabNumPort=new int[m_dlgAsset];

int k,i;
i=0;

for(k=0;k<=m_dlgAsset-1;k++)
{
*(pTabBinPort+k)=0;

Table.ConvertBinToNum(k,pTabBinPort,pTabNumPort);
}
*pTabVar=Table.CalcVar(pTabNumPort,m_dlgAsset);

for(i=1;i<=pow(2,m_dlgSector);i++)
{
k=m_dlgSector-1;

while(*(pTabBinPort+k)==1)
{
*(pTabBinPort+k)=0;
Table.ConvertBinToNum(k,pTabBinPort,pTabNumPort);
k--;
}
*(pTabBinPort+k)=1;
Table.ConvertBinToNum(k,pTabBinPort,pTabNumPort);

*(pTabVar+i)=Table.CalcVar(pTabNumPort,m_dlgAsset);
}
m_dlgValue=pTabVar[2];
strValue.Format(&quot;%.9f&quot;,m_dlgValue);
CEdit* PEdit=(CEdit*)GetDlgItem(IDC_VALUE);
PEdit->SetWindowText(strValue);
delete []pTabVar;
delete []pTabNumPort;
delete []pTabBinPort;
::CoUninitialize();

// TODO: Add your control notification handler code here

}
///////////////////////////////////////////////////////////
and for information the LoadTable.cpp code where you could find others function's definitions:

#include &quot;stdafx.h&quot;
#include &quot;CalcRiskPort.h&quot;
#include &quot;LoadTables.h&quot;

#define NR_END 1
#define FREE_ARG char*
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
LoadTables::LoadTables()
{

}

LoadTables::LoadTables(long a,long b,long c,long d)
{
//free_dmatrix(pCorrel,a,b,c,d);

pCorrel=dmatrix(a,b,c,d);

}

LoadTables::LoadTables(long a)
{
pVol=NULL;
Delete_pVol();
pVol=new double [a];
}

void LoadTables::Delete_pVol()
{
if(pVol!=NULL)
{
delete[] pVol;
pVol=NULL;
}
}

LoadTables::~LoadTables()
{

}

void LoadTables::nrerror(char error_text[])
{
AfxMessageBox(&quot;Ca marche moyen&quot;);
exit(1);
}

double **LoadTables::dmatrix(long LowRow,long HighRow,long LowCol,long HighCol)
{
long i, RowSize=HighRow-LowRow+1,ColSize=HighCol-LowCol+1;

double **m;

m=(double **)malloc((size_t)((RowSize+NR_END)*sizeof(double*)));
if(!m)nrerror(&quot;pb allocation1 in matrix()&quot;);

m +=NR_END;
m -=LowRow;

m[LowRow]=(double *)malloc((size_t)((RowSize*ColSize+NR_END)*sizeof(double)));
if(!m[LowRow])nrerror(&quot;pb allocation2 in matrix()&quot;);

m[LowRow] +=NR_END;
m[LowRow] -=LowCol;

for(i=LowRow+1;i<=HighRow;i++)
m=m[i-1]+ColSize;

return m;
}


void LoadTables::free_dmatrix(double **m,long LowRow,long HighRow,long LowCol,long HighCol)
{

free((FREE_ARG)(m[LowRow]+LowCol-NR_END));
free((FREE_ARG)(m+LowRow-NR_END));

}

CString LoadTables::ConvIntToVar(double j)
{
CString strIndexCol;
int decimal,sign;

strIndexCol=_fcvt(j,0,&decimal,&sign);

//_variant_t IndexCol(strIndexCol);

return strIndexCol;

}

double LoadTables::ReturnValue(long Row,_variant_t IndexCol, _variant_t ParamTable)
{

double dValue;
CString strTemp;
strTemp.Format(&quot;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\CalcRiskPort\\vol_correl1.mdb;&quot;);
_bstr_t strcnn(strTemp);
_RecordsetPtr pTp(&quot;ADODB.Recordset&quot;);

try
{
pTp->Open(ParamTable,strcnn,adOpenStatic,adLockOptimistic,adCmdTableDirect);
pTp->MoveFirst();
pTp->Move(Row-1);
if(!pTp->EndOfFile)
dValue=pTp->Fields->GetItem(IndexCol)->Value;
//dValue=pTp->Fields->GetItem(&quot;N° Titre&quot;)->Value;
//Parameter2=pTp->Fields->GetItem(IndexColumn)->Value;

pTp->Close();

}

catch(_com_error &e)
{

AfxMessageBox(&quot;Table non atteinte&quot;);
pTp->Close();

}

return dValue;

}

double LoadTables::CalcVar(int* pTabNumPort,int NbOfAsset)
{
int i,j;
double sum1,sum2,sum;

sum=sum1=sum2=0;

for(i=1,j=0;i<=NbOfAsset,j {
sum1 +=(*(pVol+(*(pTabNumPort+i-1))))*(*(pVol+(*(pTabNumPort+i-1))));
sum2 +=*(*(pCorrel+(*(pTabNumPort+i-1)))+ *(pTabNumPort+j))+(*(pVol+(*(pTabNumPort+i-1))))*(*(pVol+(*(pTabNumPort+j))));
}

sum=sum1-2*sum2;

return sum;

}

void LoadTables::ConvertBinToNum(int Rang,int *pTabBinPort,int *pTabNumPort)
{

if(*(pTabBinPort+Rang)==0)
*(pTabNumPort+Rang)=2*Rang+1;
else
*(pTabNumPort+Rang)=2*(Rang+1);

}


thanks in advance

jayjay
 
I havn't looked at all this code, but if you declare
LoadTables Table(...) within a specific function, Table
with it's data will &quot;die&quot; if you exit that function.
What you can do is create your to pointers as member variables of class CCalcRiskPortDlg and move pVol and pCorrel to these pointers, or rewrite your functions, will
say change 'em from void to type pointer and use their result.
hope it helps
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top