Solved

Cast an object to different types

Posted on 2014-03-07
14
214 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

624 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