473,396 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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.

Mar 8 '06 #1
2 1607

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.

--
Larry Lard
Replies to group please

Mar 9 '06 #2
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.

Mar 9 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tao | last post by:
I just upgraded .NET framework to 1.1 and VS.Net to 2003 version and tried to test it out. I created an ASP.NET project using the wizard and tried to run it by hitting "F5". I got an exception:...
0
by: Pankaj Jain | last post by:
Hi All, I have a class A which is derived from ServicesComponent to participate in automatic transaction with falg Transaction.Required. Class A is exposed to client through remoting on Http...
2
by: Fabian | last post by:
Hi, I work with asp.net 2.0 and I have a intermittent error, only happens a few times a day. In the page I evaluate a Query String and then I get data form a database. The code snipped: ...
0
by: Amil | last post by:
I've upgrade to VS 2005. I have several pages that dynamically load user controls; I get an exception like: "Unable to cast object of type 'ASP.xxxxx' to type" I've investigated and found...
4
by: JackBlack | last post by:
Hi, all! Need a little help tracking down a runtime error problem. I'm getting this error: "Unable to cast object of type 'myStruct' to type 'myStruct'... but the two types are identical! I...
10
by: R2d2Rabeau | last post by:
Hi, I'm new to C#, please be patient :-) I am trying to pass an Arraylist (myAL) to a jagged array (myAL2) . The problem I have is that when I write the values to a file i do not get the values...
4
by: archu007 | last post by:
hi i'm using the following statement in my application string strTArray = new string; strTArray = (string)(Session); when i run the application its giving the below error {"Unable to cast...
2
by: anj2k5 | last post by:
I uploaded music file in SQl DB in a image dataype .But when i try to reterive it,It doesn't play. It gives the following type casting error on runtime "Unable to cast object of type...
3
by: =?Utf-8?B?UGF1bCBQcmV3ZXR0?= | last post by:
I'm attempting to use LINQ to insert a record into a child table and I'm receiving a "Specified cast is not valid" error that has something to do w/ the keys involved. The stack trace is: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.