E
Edward Diener
This has occurred in MC++, but since there is very little response on that
NG, I am also reporting it here in the hope that someone can find me a
workaround, and report it to MS.
If a __value class with an event is put into an assembly and a __gc class in
another assembly, which has an instance of the __value class as a data
member, attempts to attach its own event handler to the __value class's
event, everything compiles correctly but the Linker puts out error "LINK :
error LNK2020: unresolved token (0A000003) AValueClassEvent" followed by
"LINK : fatal
error LNK1120: 1 unresolved externals". If the __value class is in the same
assembly as the __gc class, everything is fine and there is no linker error.
What follows is code illustrating this bug.
------------------------------------------------------------------------
In one assembly:
// ValueClass.h
#pragma once
namespace ValueClass
{
public __value class AValueClass
{
public:
AValueClass();
__event System::EventHandler * AValueClassEvent;
int AnInt;
};
}
// ValueClass.cpp
#include "stdafx.h"
#include "ValueClass.h"
ValueClass::AValueClass::AValueClass() : AnInt(1) {}
In a second assembly which references the first assembly:
// HandleValueClassEvent.h
#pragma once
namespace HandleValueClassEvent
{
public __value class AnotherValueClass
{
public:
AnotherValueClass();
__event System::EventHandler * AnotherValueClassEvent;
int AnotherInt;
};
public __gc class HandleEvent
{
public:
HandleEvent();
void AMemberFunction();
void AnotherMemberFunction();
private:
ValueClass::AValueClass avc;
HandleValueClassEvent::AnotherValueClass anvc;
void AnEventHandler(System::Object *,System::EventArgs *);
};
}
// HandleValueClassEvent.cpp
#include "stdafx.h"
#include "HandleValueClassEvent.h"
HandleValueClassEvent::HandleEvent::HandleEvent() {}
void HandleValueClassEvent::HandleEvent::AMemberFunction()
{
avc.AValueClassEvent += new System::EventHandler(this,AnEventHandler);
}
void HandleValueClassEvent::HandleEvent::AnotherMemberFunction()
{
anvc.AnotherValueClassEvent += new
System::EventHandler(this,AnEventHandler);
}
void HandleValueClassEvent::HandleEvent::AnEventHandler(System::Object *
sender,System::EventArgs * e) {}
HandleValueClassEvent::AnotherValueClass::AnotherValueClass() :
AnotherInt(1) {}
----------------------------------------------------------------------------
--
The attempt to add an event handler to avc.AValueClassEvent, which is in
another assembly, causes the linker error. If you comment it out there is no
error. The attempt to add an event handler to anvc.AnotherValueClassEvent,
which is the same assembly, causes no error.
Since I have designed a number of __value classes in an assembly which have
events which must be caught by __gc classes in other assemblies, I badly
need a workaround to this problem else I have to scrapped my own logical
design or duplicate value classes under slightly different names in each
assembly in which I want to use them, which is a real PITA.
NG, I am also reporting it here in the hope that someone can find me a
workaround, and report it to MS.
If a __value class with an event is put into an assembly and a __gc class in
another assembly, which has an instance of the __value class as a data
member, attempts to attach its own event handler to the __value class's
event, everything compiles correctly but the Linker puts out error "LINK :
error LNK2020: unresolved token (0A000003) AValueClassEvent" followed by
"LINK : fatal
error LNK1120: 1 unresolved externals". If the __value class is in the same
assembly as the __gc class, everything is fine and there is no linker error.
What follows is code illustrating this bug.
------------------------------------------------------------------------
In one assembly:
// ValueClass.h
#pragma once
namespace ValueClass
{
public __value class AValueClass
{
public:
AValueClass();
__event System::EventHandler * AValueClassEvent;
int AnInt;
};
}
// ValueClass.cpp
#include "stdafx.h"
#include "ValueClass.h"
ValueClass::AValueClass::AValueClass() : AnInt(1) {}
In a second assembly which references the first assembly:
// HandleValueClassEvent.h
#pragma once
namespace HandleValueClassEvent
{
public __value class AnotherValueClass
{
public:
AnotherValueClass();
__event System::EventHandler * AnotherValueClassEvent;
int AnotherInt;
};
public __gc class HandleEvent
{
public:
HandleEvent();
void AMemberFunction();
void AnotherMemberFunction();
private:
ValueClass::AValueClass avc;
HandleValueClassEvent::AnotherValueClass anvc;
void AnEventHandler(System::Object *,System::EventArgs *);
};
}
// HandleValueClassEvent.cpp
#include "stdafx.h"
#include "HandleValueClassEvent.h"
HandleValueClassEvent::HandleEvent::HandleEvent() {}
void HandleValueClassEvent::HandleEvent::AMemberFunction()
{
avc.AValueClassEvent += new System::EventHandler(this,AnEventHandler);
}
void HandleValueClassEvent::HandleEvent::AnotherMemberFunction()
{
anvc.AnotherValueClassEvent += new
System::EventHandler(this,AnEventHandler);
}
void HandleValueClassEvent::HandleEvent::AnEventHandler(System::Object *
sender,System::EventArgs * e) {}
HandleValueClassEvent::AnotherValueClass::AnotherValueClass() :
AnotherInt(1) {}
----------------------------------------------------------------------------
--
The attempt to add an event handler to avc.AValueClassEvent, which is in
another assembly, causes the linker error. If you comment it out there is no
error. The attempt to add an event handler to anvc.AnotherValueClassEvent,
which is the same assembly, causes no error.
Since I have designed a number of __value classes in an assembly which have
events which must be caught by __gc classes in other assemblies, I badly
need a workaround to this problem else I have to scrapped my own logical
design or duplicate value classes under slightly different names in each
assembly in which I want to use them, which is a real PITA.