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("Volatilité"->Value;
}
}
//dValue=pTp->Fields->GetItem("N° Titre"->Value;
//Parameter2=pTp->Fields->GetItem(IndexColumn)->Value;
pTp->Close();
}
catch(_com_error &e)
{
AfxMessageBox("a pas bon"
}
m_dlgValue=Table.pVol[3];
strValue.Format("%.9f",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("a pas bon"
}
::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="t";
//Table.pCorrel=Table.dmatrix(1,m_dlgPortWeight,1,m_dlgPortWeight);
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("Correlation",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("N° Titre"->Value;
//Parameter2=pTp->Fields->GetItem(IndexColumn)->Value;
pTp->Close();
}
catch(_com_error &e)
{
AfxMessageBox("a pas bon"
}
m_dlgValue=Table.pCorrel[3][7];
strValue.Format("%.9f",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("a pas bon"
}
::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("%.9f",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 "stdafx.h"
#include "CalcRiskPort.h"
#include "LoadTables.h"
#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:elete_pVol()
{
if(pVol!=NULL)
{
delete[] pVol;
pVol=NULL;
}
}
LoadTables::~LoadTables()
{
}
void LoadTables::nrerror(char error_text[])
{
AfxMessageBox("Ca marche moyen"
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("pb allocation1 in matrix()"
m +=NR_END;
m -=LowRow;
m[LowRow]=(double *)malloc((size_t)((RowSize*ColSize+NR_END)*sizeof(double)));
if(!m[LowRow])nrerror("pb allocation2 in matrix()"
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("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\CalcRiskPort\\vol_correl1.mdb;"
_bstr_t strcnn(strTemp);
_RecordsetPtr pTp("ADODB.Recordset"
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("N° Titre"->Value;
//Parameter2=pTp->Fields->GetItem(IndexColumn)->Value;
pTp->Close();
}
catch(_com_error &e)
{
AfxMessageBox("Table non atteinte"
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
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("Volatilité"->Value;
}
}
//dValue=pTp->Fields->GetItem("N° Titre"->Value;
//Parameter2=pTp->Fields->GetItem(IndexColumn)->Value;
pTp->Close();
}
catch(_com_error &e)
{
AfxMessageBox("a pas bon"
}
m_dlgValue=Table.pVol[3];
strValue.Format("%.9f",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("a pas bon"
}
::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="t";
//Table.pCorrel=Table.dmatrix(1,m_dlgPortWeight,1,m_dlgPortWeight);
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("Correlation",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("N° Titre"->Value;
//Parameter2=pTp->Fields->GetItem(IndexColumn)->Value;
pTp->Close();
}
catch(_com_error &e)
{
AfxMessageBox("a pas bon"
}
m_dlgValue=Table.pCorrel[3][7];
strValue.Format("%.9f",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("a pas bon"
}
::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("%.9f",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 "stdafx.h"
#include "CalcRiskPort.h"
#include "LoadTables.h"
#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:elete_pVol()
{
if(pVol!=NULL)
{
delete[] pVol;
pVol=NULL;
}
}
LoadTables::~LoadTables()
{
}
void LoadTables::nrerror(char error_text[])
{
AfxMessageBox("Ca marche moyen"
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("pb allocation1 in matrix()"
m +=NR_END;
m -=LowRow;
m[LowRow]=(double *)malloc((size_t)((RowSize*ColSize+NR_END)*sizeof(double)));
if(!m[LowRow])nrerror("pb allocation2 in matrix()"
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("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\CalcRiskPort\\vol_correl1.mdb;"
_bstr_t strcnn(strTemp);
_RecordsetPtr pTp("ADODB.Recordset"
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("N° Titre"->Value;
//Parameter2=pTp->Fields->GetItem(IndexColumn)->Value;
pTp->Close();
}
catch(_com_error &e)
{
AfxMessageBox("Table non atteinte"
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