dros
dros

Reputation: 1557

How do I convert the contents of one class to a new one?

I have 2 classes:

public class BookingSummary
{
    public int BookingId { get; set; }
    public string BookingReference { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public string HotelCode { get; set; }
    public string HotelName { get; set; }
    public bool PreCheckinCompleted { get; set; }
    public string BookingPeriod {get; set; } 
    public string UserType { get; set; }
    public string UserRole { get; set; }
}

and

public class PersonalisationState
{
    public UserType UserType { get; set; }
    public UserRole UserRole { get; set; }
    public BookingPeriod BookingPeriod { get; set; }
}

How do I convert the contents of BookingSummary into a new PersonalisationState class when passing in a BookingSummary as a parameter:

private PersonalisationState ConvertBookingSummaryToPersonalisationState(BookingSummary booking)
{
   return(no clue)
}

Is this possible?

Upvotes: 0

Views: 82

Answers (3)

Rufus L
Rufus L

Reputation: 37020

"how do i convert the contents of bookingsummary into a new PersonalisationState class when passing in booking summary as a parameter"

You need to return an instance of PersonalisationState initialized with values taken from the booking argument. In order to to this, we need is a way to create instances of the UserType, UserRole, and BookingPeriod classes from a string type. In general this can be done by adding to these classes either a static method that takes in a string and returns an instance of the class (normally this method is named Parse), or a constructor that takes in a string, or finally just set a known string property of the class.

Here are examples of each:

public class UserRole
{
    public string RoleName { get; private set; }

    // Static method returns an instance based on a string
    public static UserRole Parse(string name)
    {
        return new UserRole {RoleName = name};
    }
}

public class UserType
{
    public string TypeName { get; private set; }

    // Constructor creates an instance based on a string
    public UserType (string name)
    {
        TypeName = name;
    }
}

public class BookingPeriod
{
    public string Name { get; set; }
}

Next, we can do something similar with the PersonalizationState class: either add a constructor that takes in an instance of a BookingSummary, or provide a static method that returns an instance of the class from an instance of a BookingSummary. Here's an example of the constructor:

public class PersonalisationState
{
    public UserType UserType { get; set; }
    public UserRole UserRole { get; set; }
    public BookingPeriod BookingPeriod { get; set; }

    public PersonalisationState(BookingSummary booking)
    {
        if (booking == null) return;
        UserRole = UserRole.Parse(booking.UserRole);
        UserType = new UserType(booking.UserType);
        BookingPeriod = new BookingPeriod {BookingPeriodType = booking.BookingPeriod};
    }
}

Now our Convert method becomes extremely simple (and, infact, unnecessary: since it's only one line of code, you can replace any call to this method with the line inside the method):

private PersonalisationState Convert(BookingSummary booking)
{
    return new PersonalisationState(booking);
}

Note that I renamed your method to simply Convert because the "from" and "to" types are implied by the argument type and the return type.

Upvotes: 1

Jawad
Jawad

Reputation: 11364

Create a new constructor that takes in old class and gives you a new class.

Object types (UserType, UserRole and BookingPeriod) will need to be initialized and set accordingly...

    public class PersonalisationState
        {

            public UserType UserType { get; set; }
            public UserRole UserRole { get; set; }
            public BookingPeriod BookingPeriod { get; set; }

            public PersonalisationState(BookingSummary booking)
            {
                // Add Error checking to ensure booking.UserType, UserRole or others are not null
                this.UserType = new UserType() { userType = booking.UserType };
                this.UserRole = new UserRole() { roleName = booking.UserRole };
                this.BookingPeriod = new BookingPeriod() {periodName = booking.BookingPeriod};
            }
        }

You can use it in your main or anywhere like

BookingSummary book = new BookingSummary() { /* with Values */ };
PersonalisationState pers = new PersonalisationState(book);

Upvotes: 1

LLL
LLL

Reputation: 3771

You do it by returning a new object with the properties set from the input object

private PersonalisationState ConvertBookingSummaryToPersonalisationState(BookingSummary booking)
{

   return new PersonalisationState()
   {
           UserType = new UserType()
           {    
                Name = booking.UserType,
                <...>
           } 
   };
}

Or you can use a mapping library like https://automapper.org/ or similar

Upvotes: 3

Related Questions