"Specified cast is not valid" converting from object A to object B

T

tlk

Hi, everyone. I'm having a problem that I hope some more experienced
developers can help me figure-out.

We have a project called Records that has been around for a year or so.
We're currently coding an add-on to this project called Lessons. In
creating Lessons we're obviously trying to re-use existing code from
Records. And, of course, for political reasons I'm not free to make
any changes to Records.

The project Records contains a class called UserControl and a class
called Manager. Manager is a class that keeps track of the Record's
status, the user's actions, the Record's summary, etc.

The new project Lessons contains analogous classes. It has its own
UserControl and Manager classes that function similarly to the
analogous classes in Records. The new Lessons project also re-uses
some of the Records code and data, and the Records assembly is
referenced in the Lessons project.

So when Lessons starts-up it creates its own Manager object and stores
it in the Session object, which we handle in our own WebSession object.
At one point in the project we want to access some data from the
Record's database. We do this by creating a UserControl object from
the Records. Unfortunately we hit this code in the Records:

Dim obj As Core.Manager
obj = CType(Me.WebSession("ManagerSession"), Core.Manager)


The CType is what is puking because the application has a Manager
object that was created in the Lessons world, and this line of code is
trying to covert the object into the Records flavored Manager object.
Obviously. So what can I do to make sure that this cast is valid? I
think that I need to make sure that the Manager object created in
Lessons has the same signature as the Manager object created in
Records. Right? They currently have the same public methods and
properties, but some of those properties have the type of other custom
objects. Would I have to then ensure that each of THOSE objects has
the same signature in each project?

I hope this is clear, and I hope that this is just a problem that will
be more easily fixed when I have more experience with VB.Net.
 
L

Larry Lard

tlk wrote:
[snip]
So when Lessons starts-up it creates its own Manager object and stores
it in the Session object, which we handle in our own WebSession object.
At one point in the project we want to access some data from the
Record's database. We do this by creating a UserControl object from
the Records. Unfortunately we hit this code in the Records:

Dim obj As Core.Manager
obj = CType(Me.WebSession("ManagerSession"), Core.Manager)


The CType is what is puking because the application has a Manager
object that was created in the Lessons world, and this line of code is
trying to covert the object into the Records flavored Manager object.

What's Core, an alias for Records? So you're saying
Me.WebSession("ManagerSession") is a Lessons.Manager, and the
subsequent code wants to deal with a Records.Manager?
Obviously. So what can I do to make sure that this cast is valid? I
think that I need to make sure that the Manager object created in
Lessons has the same signature as the Manager object created in
Records. Right? They currently have the same public methods and
properties, but some of those properties have the type of other custom
objects. Would I have to then ensure that each of THOSE objects has
the same signature in each project?

No, you are thinking up the wrong tree here. There isn't going to be
any way that a Lessons.Manager is going to be able to be
*automatically* converted to a Records.Manager - as far as the system
is concerned, they are completely different types. eg You might have
two Forms with the same controls on, but you wouldn't expect to be able
to convert from one to the other, would you?

The way to proceed here is:

- Identify the key functionality that Lessons.Manager and
Records.Manager share. It may be that it makes sense for the newer to
be a subclass of the older (the answer to this is the same as the
answer to 'Is a Lessons.Manager a particular kind of Records.Manager?')

- Or, if that doesn't make sense, you could look at the code subsequent
to this snippet, the code that currently expects a Records.Manager. If
this code is part of Lessons, maybe it should be working with a
Lessons.Manager, not a Records.Manager?

Your desire not to change the Records code is good, but it does
probably mean you are going to have to do more work.
 
T

tlk

Very helpful reply, Larry. Thanks very much!

Core is the namespace in which the class Manager is defined. Each
project, Records and Lessons, has its Manager class defined in its own
Core namespace. You're correct that I was trying to say that
Me.WebSession("ManagerSession") is a Lessons.Manager trying to be
converted to a Records.Manager.

I like the idea of making the newer Manager be a subclass of the
previous project's Manager class. I'll present these ideas to the
team. Hopefully when we all put our heads together we can straighten
this out.

Thanks, again.
 

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