Strange Compile Problem With Generics

G

Guest

I've tried to debug this every way I can think of, but I can't figure out
this strange compile problem I am having. Any help is greatly appreciated.

I have a generic collection class that inherits from
System.Collections.ObjectModel.Collection(Of T) called
MatrixGenericCollectionBase (Matrix is the name of our project). Despite
it's name it is actually an instantiable class that is used widely in our
application to hold objects of many types. There is some additional
functionality in the class but for the most part it is just a normal
implementation of a generic collection, not a ton of additional code.

In one instance I need a type specific collection because I need to add a
function at the collection level that is specific for that type. In this
case I have created a new class that inherits from
MatrixGenericCollectionBase(Of TypeX) and named the new collection class
TypeXCollection. The only code this class contains is the new function,
which basically just loops through the collection and looks for specific
members based on some parameters passed in.

This compiles and runs fine when I compile through Visual Studio on my local
machine. It also compiles fine when I run my nAnt build scripts on my local
machine. It compiles fine when compiled in the VS IDE on the build server.
BUT, it fails when the nAnt build script is run on the build server. The
compile error I receive is "error BC30311: Value of type 'T' cannot be
converted to 'Matrix.TypeX'". This error is received on a line of code that
is iterating through the collection in a for each statement.

I have mapped a drive to the code on the build server and run the nAnt
scripts locally against the build server code and it compiles fine. I have
copied the command line call to vbc that is shown in the output window of
Visual Studio when I compile in release mode and run it from a command line
and it fails with the same error. That's right, the same thing that is
succeeding for Visual Studio on the same box is failing for me. I've now
even gone as far as uninstalling and reinstalling the .Net 2.0 framework.

I'm frustrated and ready to go back to the old non-generic base collection
just to be able to compile on my build server. If you have any ideas, please
help.
 
G

Guest

Just to share some extra weirdness...

I tried running msbuild on the build server against the project file
expecting to get a clean compile the same as I do through VS 2005. Wrong!
It gave me the same error I described below. Why in the world would the
MSBuild results be different than the results I get through the IDE? Doesn't
the IDE just call MSBuild?

Can someone at least confirm for me that I am not crazy and that the code
situation I described *should* compile?
 
P

Peter Duniho

Brett said:
Just to share some extra weirdness...

I tried running msbuild on the build server against the project file
expecting to get a clean compile the same as I do through VS 2005. Wrong!
It gave me the same error I described below. Why in the world would the
MSBuild results be different than the results I get through the IDE? Doesn't
the IDE just call MSBuild?

I don't have any specific knowledge regarding that question, but given
the behavior I'd say perhaps it doesn't.

Alternatively, maybe there are some compiler switches that are
different. That's the first thing I'd look at.
Can someone at least confirm for me that I am not crazy and that the code
situation I described *should* compile?

I'm not sure anyone can do that until you actually post a
complete-but-concise sample of code that reliably reproduces the problem.

Pete
 
P

Patrice

'Matrix.TypeX' inherits from ?

It could be quite easy to forget that your collection inherits from its
parent but it doesn't mean that the type you are using to store your
elements inherits from the type you used for elements when inheriting from
your base collection - hopefully you'll better understand than me what I'm
trying to say ;-)
 
G

Guest

Comments below:

Peter Duniho said:
I don't have any specific knowledge regarding that question, but given
the behavior I'd say perhaps it doesn't.

Alternatively, maybe there are some compiler switches that are
different. That's the first thing I'd look at.

It was the first thing I looked at too, that is why I copied the vbc call
and all parameters from the output window of VS 2005 and pasted it into a
command prompt window. Either VS 2005 isn't showing me all of the parameters
it is setting or something else is causing the difference. Either way if
someone can tell me how to figure out how to run the vb compiler exactly the
same way VS 2005 is I'd be mighty grateful.
I'm not sure anyone can do that until you actually post a
complete-but-concise sample of code that reliably reproduces the problem.

I'm afraid to spend too much time paring the code down to a useable sample
because I'm not sure if anyone will be able to reproduce the problem. Right
now I can only recreate the problem on one box, it just happens to be our
build server. But maybe if I can reproduce it on the build server someone
else will be able to reproduce it too; I'll see what I can put together
quickly.
 
G

Guest

Thanks for the suggestion Patrice but the generic type parameters for the
collection I'm inheriting from and the type of the members I am putting into
the collection match.

I even tried swithching it to declare the type specific collection as a
generic collection with a constraint for the specific type I want to hold and
then passing the type parameter through to the class I'm inheriting from and
I still got the same compile error in the same situations.
 
G

Guest

Well I'm no closer to knowing what causes it to compile sometimes and
sometimes not, but I've narrowed down what it is choking on. It appears to
be specifically related to the enumerator. If I change the code that is
throwing an error to use a for loop with an index instead of a for each loop
it works fine.
 
G

Guest

The MatrixGenericCollectionBase object was inheriting
System.Collections.ObjectModel.Collection(Of T) and was also implementing
IDictionary since it is sort of hybrid dictionary/collection [dumb I know,
cleaning up code one step at a time]. Apparently since both of those
implement IEnumerable it seems like there was some sort of clash. I still
can't explain why it worked in some environments and not in others but at
least I know how to start to fix it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top