473,382 Members | 1,775 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,382 software developers and data experts.

Casting

Hello,

I have created a class that inherits MembershipUser:
public class UserHelper : MembershipUser {
....

Then I am creating a user as follows:
UserHelper user = (UserHelper)Membership.CreateUser("me", "pass",
"me@domain.com", null, null, true, null, out status);

Membership.CreateUser returns a MembershipUser

But I get an error:
Unable to cast object of type 'System.Web.Security.MembershipUser' to
type 'MyApp.Security.Membership.UserHelper'.

If I don't cast it, which means, using:
UserHelper user = Membership.CreateUser("me", "pass", "me@domain.com",
null, null, true, null, out status);

It does not compile and I get the error:
Cannot implicitly convert type 'System.Web.Security.MembershipUser' to
'MyApp.Security.Membership.UserHelper'. An explicit conversion exists
(are you missing a cast?)

What am I doing wrong?

Thanks,
Miguel
Nov 1 '08 #1
2 3417
On Sat, 01 Nov 2008 10:28:10 -0700, shapper <md*****@gmail.comwrote:
Hello,

I have created a class that inherits MembershipUser:
public class UserHelper : MembershipUser {
...

Then I am creating a user as follows:
UserHelper user = (UserHelper)Membership.CreateUser("me", "pass",
"me@domain.com", null, null, true, null, out status);

Membership.CreateUser returns a MembershipUser
If the object is not a UserHelper instance, you simply cannot cast it to
UserHelper. It's just not possible.

Note that in the second case, where you don't cast it and the compiler
says "an explicit conversion exists", it only means that in the
syntactical sense. It has no way to verify whether the conversion will
actually _succeed_...just that you can attempt it explicitly with a cast.

Pete
Nov 1 '08 #2
shapper <md*****@gmail.comwrote:
>
I have created a class that inherits MembershipUser:
public class UserHelper : MembershipUser {
...

Then I am creating a user as follows:
UserHelper user = (UserHelper)Membership.CreateUser("me", "pass",
"me@domain.com", null, null, true, null, out status);

Membership.CreateUser returns a MembershipUser

But I get an error:
Unable to cast object of type 'System.Web.Security.MembershipUser' to
type 'MyApp.Security.Membership.UserHelper'.

If I don't cast it, which means, using:
UserHelper user = Membership.CreateUser("me", "pass", "me@domain.com",
null, null, true, null, out status);

It does not compile and I get the error:
Cannot implicitly convert type 'System.Web.Security.MembershipUser' to
'MyApp.Security.Membership.UserHelper'. An explicit conversion exists
(are you missing a cast?)

What am I doing wrong?
You're casting in the wrong direction. If you have a UserHelper, you can
cast it to a MembershipUser, but you cannot safely start with a base object
and cast it to a derived class. What would happen to any additional
members you might have added? They wouldn't be present in the
MembershipUser instance.

In this case, you will have to use composition instead of inheritance. Add
a MembershipUser instance to UserHelper ad a member, call CreateUser, and
pass the MembershipUser object you get to the constructor for UserHelper.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Nov 11 '08 #3

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

Similar topics

4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
35
by: ytrama | last post by:
Hi, I have read in one of old posting that don't cast of pointer which is returned by the malloc. I would like to know the reason. Thanks in advance, YTR
7
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b;...
7
by: Jim Bancroft | last post by:
Hi everyone, A basic one here, I think. I haven't found the pattern yet, but sometimes when I cast a variable to another type using the "C" style cast operator the compiler refuses to play...
2
by: Enrique Bustamante | last post by:
Casting arrays that works on watch and command window but not in code. My application is casting arrays in a way it should work. To test if I was doing something invalid, I wrote a test code that...
7
by: S. Lorétan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
17
by: sophia.agnes | last post by:
Hi , I was going through peter van der linden's book Expert C programming, in this book there is a section named "How and why to cast" the author then says as follows (float) 3 - it's a...
9
by: Taras_96 | last post by:
Hi everyone, I was experimenting with static_cast and reinterpret cast #include <iostream> struct A1 { int a; }; struct A2 { double d; }; struct B : public A1, A2
101
by: Tinkertim | last post by:
Hi, I have often wondered if casting the return value of malloc() (or friends) actually helps anything, recent threads here suggest that it does not .. so I hope to find out. For instance : ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.