473,487 Members | 2,466 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Specified cast is not valid - copy object

Hello,

i have a problem to cast an object - hope somebody can help me. thx

namespace CopyTest
{

public class A
{
public int a;
}

public class B:A
{
public int b;
}

class TestClass
{

[STAThread]
static void Main(string[] args)
{
A a = new A();
B b = new B();

a.a = 1;
b = a; // <- error

// An unhandled exception of type 'System.InvalidCastException' occurred in
CopyTest.exe
// Additional information: Specified cast is not valid.
}
}
}

Nov 16 '05 #1
6 4725
Hi,

What if you do this:

b = a;
b.b = 4;

b contains a A instance and A does not contain a "b" member and you would
get an error.

Now , I would bet that the above line provoke a compilation error (unless a
cast be used), are you sure it;s the real code you are using?
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Mike Spass" <Mi*******@discussions.microsoft.com> wrote in message
news:D3**********************************@microsof t.com...
Hello,

i have a problem to cast an object - hope somebody can help me. thx

namespace CopyTest
{

public class A
{
public int a;
}

public class B:A
{
public int b;
}

class TestClass
{

[STAThread]
static void Main(string[] args)
{
A a = new A();
B b = new B();

a.a = 1;
b = a; // <- error

// An unhandled exception of type 'System.InvalidCastException' occurred in CopyTest.exe
// Additional information: Specified cast is not valid.
}
}
}

Nov 16 '05 #2
You are trying to assign the variable "b"( which you've said will point at
an object of type "B") to an object of type "A". Since "A" does not derive
from "B", this isn't allowed.

What you probably want is:
b.b = a.a

You may also want to review Object Oriented fundamentals and concepts
This is for java, but the concepts hold:
http://www.iam.ubc.ca/guides/javatut/java/objects/

--
-Philip Rieck
http://philiprieck.com/blog/

-
"Mike Spass" <Mi*******@discussions.microsoft.com> wrote in message
news:D3**********************************@microsof t.com...
Hello,

i have a problem to cast an object - hope somebody can help me. thx

namespace CopyTest
{

public class A
{
public int a;
}

public class B:A
{
public int b;
}

class TestClass
{

[STAThread]
static void Main(string[] args)
{
A a = new A();
B b = new B();

a.a = 1;
b = a; // <- error

// An unhandled exception of type 'System.InvalidCastException' occurred
in
CopyTest.exe
// Additional information: Specified cast is not valid.
}
}
}

Nov 16 '05 #3
this is the rigth code, the goal is to copy the same members from Class A to
Class B.
The result should be that only the base-class copied to the the Object B

namespace CopyTest

{
public class A

{

public int a;

}

public class B:A

{

public int b;

}

class TestClass

{
[STAThread]

static void Main(string[] args)

{

A a = new A();

B b = new B();

/* is working

b.a = 1;

b.b = 2;

a = b;

*/

a.a = 1;

b = a; // <- error
// An unhandled exception of type 'System.InvalidCastException' occurred in
CopyTest.exe

// Additional information: Specified cast is not valid.

}

}

}


Nov 16 '05 #4
Mike Spass wrote:
this is the rigth code, the goal is to copy the same members from Class A to
Class B.
The result should be that only the base-class copied to the the Object B


And the error is correct. The result that *you* want is that only the
base class members get copied. But the compiler has no idea what you
are trying to do, only that B could have a lot more in it than what it
inherits from A and therefore won't allow a straight assignment. B is A
but A is not B.

I might write a Copy method on B that takes A as input and copies data
appropriately.
--
Tom Porterfield
Nov 16 '05 #5
thx for the input - so my only change is a Memberwise Copy ?

ok i try this way and get the error... 'CopyTest.B.implicit operator
CopyTest.B(CopyTest.A)': user-defined conversion to/from base class

public class B:A
{
public static implicit operator B(A xx)
{
return new B();
}
public int b;
}

Nov 16 '05 #6
Mike Spass wrote:
thx for the input - so my only change is a Memberwise Copy ?

ok i try this way and get the error... 'CopyTest.B.implicit operator
CopyTest.B(CopyTest.A)': user-defined conversion to/from base class

public class B:A
{
public static implicit operator B(A xx)
{
return new B();
}
public int b;
}


Your operator, as coded here, would be to cast an object of type B to an
object of type B. That's not allowed because it's not necessary.
--
Tom Porterfield
Nov 16 '05 #7

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

Similar topics

0
3608
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:...
3
10491
by: PK9 | last post by:
I am looking for assistance in pinpointing the cause of the following exception. I am getting a "Specified Cast is not valid" exception on my page. I am trying to populate a datagrid. One of my...
2
3078
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: ...
3
2165
by: VB Programmer | last post by:
I am setting up forms authentication. In my code I keep getting this error. Any ideas? Error.... Server Error in '/LandOLots' Application....
0
1597
by: Alan Z. Scharf | last post by:
this question in datagrid group for several days with no repsonse. I'm hoping for an answer her because of greater activity in this group. No cross-posting intended. Thanks....
8
4245
by: Gamma | last post by:
I'm trying to inherit subclass from System.Diagnostics.Process, but whenever I cast a "Process" object to it's subclass, I encounter an exception "System.InvalidCastException" ("Specified cast is...
2
2481
by: thithi | last post by:
Server Error in '/Assignment' Application. -------------------------------------------------------------------------------- Specified cast is not valid. Description: An unhandled exception...
3
12409
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: ...
2
8730
by: vinrin | last post by:
Thank for your answer. :-) call CheckEmptyNode (treeview) public void CheckEmptyNode( Object N ) { Microsoft.Web.UI.WebControls.TreeNode menuNode = null; ...
0
7132
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,...
1
6846
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...
0
7341
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
5439
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,...
1
4870
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...
0
3076
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...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1381
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
600
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.