Solved

Cast an object to different types

Posted on 2014-03-07
14
205 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 74

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 74

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
 

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 62

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 62

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 62

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 62

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now