Solved

Cast an object to different types

Posted on 2014-03-07
14
210 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

809 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