Solved

Cast an object to different types

Posted on 2014-03-07
14
212 Views
Last Modified: 2014-03-19
I have an object that I am trying to populate  the return will be the same from 3 stored procedures and I don't want to duplicate the code.

Here is what I am trying to do
        public static CompassUser HandleUserLoginByPassword(string UGUID, string UserName, string PassWord)
        {
            CompassUser cU = new CompassUser();

            var d = _dB.ps_HandleUserLoginBy_Password(UGUID, UserName, PassWord).SingleOrDefault();
            if (d != null)
            {
                cU = MakeCompassUser(d);
            }
            return cU;
        }

        public static CompassUser HandleUserLoginByGUID(string UGUID)
        {
            ...
                cU = MakeCompassUser(d);
        }



I want to convert/cast the object coming into MakeCompassUser and populate the object.

        private static CompassUser MakeCompassUser(Object d)
        {
            CompassUser cU = new CompassUser();

            switch (d.GetType().Name)
            {
                case "ps_HandleUserLoginBy_PasswordResult" :
                    d = (ps_HandleUserLoginBy_PasswordResult)d;
                    cU.UserID = d.UserID;
....here is where it fails

                    break;
            }

            //cU.UserID = d.UserID;

            return cU;
        }

// object is this


    public class CompassUser
    {
        public Int64 UserID { get; set; }
        public string UserGUID { get; set; }
        public string UserDisplayName { get; set; }
        public string UserName { get; set; }
        public string UserEmail { get; set; }
        public string UserPictureURL { get; set; }
        public string UserPhone { get; set; }
        public string UserFax { get; set; }
        public string UserBio { get; set; }
        public string UserRole { get; set; }
        public string LastLoginDate { get; set; }
        public string LoginAttempt { get; set; }
        public Int16 IsSystem { get; set; }
    }


thanks in advance for the help

Open in new window

0
Comment
Question by:skillilea
  • 8
  • 4
  • 2
14 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39913807
What is the type of d, defined in line 5?
0
 

Author Comment

by:skillilea
ID: 39913852
ps_HandleUserLoginBy_PasswordResult

the other type will be

ps_HandleUserLoginBy_GUIDResult
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39913872
OK. Do both of those classes inherit from a common base class, or doe they implement any particular interface?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:skillilea
ID: 39913882
Nope they are stored procedures that I call from Linq...The SQL is this.


SELECT
      u.UserID
      , u.UserGUID
      , u.UserDisplayName
      , u.UserName
      , u.UserEmail
      , u.UserPictureURL
      , u.UserPhone
      , u.UserFax
      , u.UserBio
      , u.UserPassword
      , u.UserRole
      , u.CreatedDate
      , u.LastLoginDate
      , u.LoginAttempt
      , u.IsActive
      , u.IsSystem
FROM ...
0
 

Author Comment

by:skillilea
ID: 39913887
I can pass it this way and it works just fine.

        private static CompassUser MakeCompassUser(ps_HandleUserLoginBy_PasswordResult  d)
        {

but then I would have to create 3 separate functions that do the same thing.  

Each procedure is passing the same result set.
0
 

Author Comment

by:skillilea
ID: 39913892
Here is the linq generated <partial> class
      public partial class ps_HandleUserLoginBy_PasswordResult
      {
            
            private long _UserID;
            
            private string _UserGUID;
            
            private string _UserDisplayName;
            
            private string _UserName;
            
            private string _UserEmail;
            
            private string _UserPictureURL;
            
            private string _UserPhone;
            
            private string _UserFax;
            
            private string _UserBio;
            
            private string _UserPassword;
            
            private string _UserRole;
            
            private System.DateTime _CreatedDate;
            
            private System.DateTime _LastLoginDate;
            
            private System.Nullable<int> _LoginAttempt;
            
            private bool _IsActive;
            
            private bool _IsSystem;
0
 

Author Comment

by:skillilea
ID: 39913946
I can overload the method but...I thought converting would be easier.
        private static CompassUser MakeCompassUser(ps_HandleUserLoginBy_PasswordResult d)
        {
            CompassUser cU = new CompassUser();
            cU.UserID = d.UserID;
            return cU;
        }

        private static CompassUser MakeCompassUser(ps_HandleUserLoginBy_GUIDResult d)
        {
            CompassUser cU = new CompassUser();
            cU.UserID = d.UserID;
            return cU;
        }

Open in new window

0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39914818
Hi skillilea;

What happens when you change the code like this?

switch (d.GetType().Name)
{
    case "ps_HandleUserLoginBy_PasswordResult" :
        ps_HandleUserLoginBy_PasswordResult dU = 
            (ps_HandleUserLoginBy_PasswordResult)d;
        cU.UserID = dU.UserID;
        break;
}

return cU;

Open in new window

0
 

Author Comment

by:skillilea
ID: 39914873
Same amount of code as 2 overloaded methods...



        private static CompassUser MakeCompassUser(Object d)
        {
            CompassUser cU = new CompassUser();

            switch (d.GetType().Name)
            {
                case "ps_HandleUserLoginByPasswordResult":
                    ps_HandleUserLoginByPasswordResult dU = (ps_HandleUserLoginByPasswordResult)d;
                    cU.UserID = dU.UserID;
                    cU.UserGUID = dU.UserGUID;
                    cU.UserDisplayName = dU.UserDisplayName;
                    cU.UserName = dU.UserName;
                    cU.UserEmail = dU.UserEmail;
                    cU.UserPictureURL = dU.UserPictureURL;
                    cU.UserPhone = dU.UserPhone;
                    cU.UserPhone2 = dU.UserPhone2;
                    cU.UserFax = dU.UserFax;
                    cU.UserBio = dU.UserBio;
                    cU.UserRole = dU.UserRole;
                    cU.UserDefaultPage = dU.UserDefaultPage;
                    cU.LastLoginDate = dU.LastLoginDate.ToString();
                    cU.LoginAttempt = dU.LoginAttempt.ToString();
                    cU.IsSystem = dU.IsSystem;
                    cU.UserEval = dU.UserEval;
                    cU.IsLoggedIn = true;
                    break;
                case "ps_HandleUserLoginByGUIDResult":
                    ps_HandleUserLoginByGUIDResult dU = (ps_HandleUserLoginByGUIDResult)d;
                    cU.UserID = dU.UserID;
                    cU.UserGUID = dU.UserGUID;
                    cU.UserDisplayName = dU.UserDisplayName;
                    cU.UserName = dU.UserName;
                    cU.UserEmail = dU.UserEmail;
                    cU.UserPictureURL = dU.UserPictureURL;
                    cU.UserPhone = dU.UserPhone;
                    cU.UserPhone2 = dU.UserPhone2;
                    cU.UserFax = dU.UserFax;
                    cU.UserBio = dU.UserBio;
                    cU.UserRole = dU.UserRole;
                    cU.UserDefaultPage = dU.UserDefaultPage;
                    cU.LastLoginDate = dU.LastLoginDate.ToString();
                    cU.LoginAttempt = dU.LoginAttempt.ToString();
                    cU.IsSystem = dU.IsSystem;
                    cU.UserEval = dU.UserEval;
                    cU.IsLoggedIn = true;
                    break;
            }

            return cU;
        }
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39915155
Which technology are you using Linq to SQL or Linq to Entity Framework or some other?
0
 

Author Comment

by:skillilea
ID: 39917277
Linq to SQL

thanks
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 39917662
Hi skillilea;

By the looks of the two stored procedure calls that is being return are all the same fields from the database. If what is being returned from the SP's are actually all the columns from a single table in the model, DBML, then you could change the return type of the two SP's to that table name changing the return type of the two SP's to be the same. This can be done by right clicking the SP in the designer and selecting properties and in the Properties window click on the Auto Generated for the Return Type and selecting the table from the drop down list. Now because the two SP's return the same type you will not need to duplicate the code.
0
 

Author Comment

by:skillilea
ID: 39919008
It actually comes from a view that has some calculations in it.

Nice idea however!

tnx
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39919024
You should be able to change the return type of the SP's and recreate the views.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question