Avatar of skillilea
skillilea

asked on 

Cast an object to different types

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

C#

Avatar of undefined
Last Comment
Fernando Soto
Avatar of kaufmed
kaufmed
Flag of United States of America image

What is the type of d, defined in line 5?
Avatar of skillilea
skillilea

ASKER

ps_HandleUserLoginBy_PasswordResult

the other type will be

ps_HandleUserLoginBy_GUIDResult
Avatar of kaufmed
kaufmed
Flag of United States of America image

OK. Do both of those classes inherit from a common base class, or doe they implement any particular interface?
Avatar of skillilea
skillilea

ASKER

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 ...
Avatar of skillilea
skillilea

ASKER

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.
Avatar of skillilea
skillilea

ASKER

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;
Avatar of skillilea
skillilea

ASKER

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

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

Avatar of skillilea
skillilea

ASKER

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;
        }
Which technology are you using Linq to SQL or Linq to Entity Framework or some other?
Avatar of skillilea
skillilea

ASKER

Linq to SQL

thanks
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of skillilea
skillilea

ASKER

It actually comes from a view that has some calculations in it.

Nice idea however!

tnx
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

You should be able to change the return type of the SP's and recreate the views.
C#
C#

C# is an object-oriented programming language created in conjunction with Microsoft’s .NET framework. Compilation is usually done into the Microsoft Intermediate Language (MSIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR).

98K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo