15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


Radio Buttons

J G Clark -- J.G.Clark@ecs.soton.ac.uk
Thursday, January 25, 1996

I'm working on a database project using VC 4.0 and the DAO classes.

In the ACCESS database I have an integer field m_VRU, which is used to select a group radio button .

The derived CDaoRecordset has this field defined as a 'short m_VRU'

the definition for the DDX function is as follows

void AFXAPI DDX_FieldRadio( CDataExchange* pDX, int nIDC, int& value, CDaoRecordset* pRecordset );

in my program Class wizzard creates the line DDX_FieldRadio(pDX, IDC_VRU, m_pSet->m_VRU, m_pSet);

the compiler objects to the int&, as the value parameter passed (m_pSet->m_VRU) is a short!
ie
error C2607: 'initializing' : cannot implicitly convert a 'short' to a 'int &' that is not const

I've tried casting to an int&, this will screw up the classwizzard, plus the program does'nt work properly









Jim Beveridge -- 73542.1421@compuserve.com
Sunday, January 28, 1996

J G Clark wrote:
> 
> I'm working on a database project using VC 4.0 and the DAO classes.
> 
> In the ACCESS database I have an integer field m_VRU, which is used to select a group radio button .
> 
> The derived CDaoRecordset has this field defined as a 'short m_VRU'
> 
> the definition for the DDX function is as follows
> 
> void AFXAPI DDX_FieldRadio( CDataExchange* pDX, int nIDC, int& value, CDaoRecordset* pRecordset );
> 
> in my program Class wizzard creates the line DDX_FieldRadio(pDX, IDC_VRU, m_pSet->m_VRU, m_pSet);
> 
> the compiler objects to the int&, as the value parameter passed (m_pSet->m_VRU) is a short!
> ie
> error C2607: 'initializing' : cannot implicitly convert a 'short' to a 'int &' that is not const
> 
> I've tried casting to an int&, this will screw up the classwizzard, plus the program does'nt work properly


The cause of the error message is a C++ question, not an MFC question, but
here is the answer:

Remember that references are really pointers.  What you are effectively
trying to do it this:

	(int*)&m_VMU;	// where m_VMU is short

Also, because DDX functions go both ways (load and save), the DDX_FieldRadio
function is guaranteed to write m_VMU at least sometimes.

An int is 4 bytes, a short is 2 bytes, therefore when DDX tries to write your value
it would overflow the two byte short and overwrite some value later in the structure.
Thus the error message.

The compiler can implicitly convert a short to a const int& because the short can
be cast to a temporary variable.  Since the variable is const the called function
will not be able to change the temporary.  Obviously, this scenario will not
work for DDX_FieldRadio

Since you are stuck with a short, you will have to do something like this in DoDataExchange:
(This is off the top of my head, so beware typos, etc.)

	int nRadioValue = m_pSet->m_VRU;
	DDX_FieldRadio(pDX, IDC_VRU, m_pSet->m_VRU, m_pSet);
	m_pSet->m_VRU = (short)nRadioValue;

You will have to move this code outside of the braces that the class wizard controls
in DoDataExchange.


-- Jim Beveridge





Niels Ull Jacobsen -- nuj@kruger.dk
Monday, January 29, 1996

> 
> I'm working on a database project using VC 4.0 and the DAO classes.
> 

> In the ACCESS database I have an integer field m_VRU, which is used
> to select a group radio button .

> 
> The derived CDaoRecordset has this field defined as a 'short m_VRU'
> 
> the definition for the DDX function is as follows
> 
> void AFXAPI DDX_FieldRadio( CDataExchange* pDX, int nIDC, int& value, CDaoRecordset* pRecordset );
> 

> in my program Class wizzard creates the line DDX_FieldRadio(pDX,
> IDC_VRU, m_pSet->m_VRU, m_pSet); the compiler objects to the int&,
> as the value parameter passed (m_pSet->m_VRU) is a short!  ie

> error C2607: 'initializing' : cannot implicitly convert a 'short' to a 'int &' that is not const
 
> I've tried casting to an int&, this will screw up the classwizzard,
> plus the program does'nt work properly

This looks like a flaw in the Class Wizard. However, the
fix/workaround should be simple:


Just add the following function somewhere appropriate:

void AFXAPI DDX_FieldRadio( CDataExchange* pDX, int nIDC, short& value, CDaoRecordset* pRecordset )
{
	int intval = value;
	DDX_FieldRadio(pDX, nIDC, value, pRecordset );
        ASSERT(intval >= SHRT_MIN && intval <= SHRT_MAX);
        value = (short) intval;
};


--
Niels Ull Jacobsen, Kruger A/S

Everything stated herein is THE OFFICIAL POLICY of the entire Kruger
group and should be taken as legally binding in every respect. Pigs
will grow wings and fly.









| Вернуться в корень Архива |