473,802 Members | 2,341 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"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.WebSes sion("ManagerSe ssion"), 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 1624

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.WebSes sion("ManagerSe ssion"), 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
3641
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: "Specified cast is not valid." The only thing i put there was a "test this." inside the form. What might be the problem here? Thanks in advance. The Exception:
0
7389
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 channal hosting into IIS. There is a class B which is also available through remoting hosted on IIS on the same URI. B creates new of A inside a function. It succeed and able to create instance of A inside B first time. But it failes in 2nd attempt when...
2
3094
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: try {
0
1263
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 out that this is a known issue. The recommendations I've seen indicate I should add either the Page directive "Reference Control" or "Register" to the aspx file. I checked all my files and they all have the "Reference Control" directive (I think...
4
2465
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 have a class method that's building an array of user-defined structures (see below), and returning that array to the calling routine. I'm getting the error on that calling line. The structure in both the webform and class are defined like this:
10
7560
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 instead I get "System.String" ? How can I fix the problem? I wanted to use ArralyList because its length is flexible and I can add values to it. ...All help will be greatly appreciated. Here's my code:
4
11288
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 object of type 'System.String' to type 'System.String'."}
2
10815
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 'System.Byte' to type 'System.IO.Stream'. I used the following code to play musicfile
3
12438
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: ====================== Message: Specified cast is not valid. Type: System.InvalidCastException Source: System.Data.Linq TargetSite: Boolean TryCreateKeyFromValues(System.Object, V ByRef)
0
9699
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10535
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10282
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9111
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7598
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3792
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2966
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.