none
Object casting RRS feed

  • Question

  • The code below the stars work according to the Rent check if the Rent is true it will work with RentingDB object else it will work with SellingDB object.
    Is it possible to change the newRealty reference according to Rent check in C#?
     I attempted use the common referance with Object type,but it did not work and I do not know how to cast and complete this.
                                Object newRealty;
                                if(Rent)
                                 newRealty= new RentingDB();
                                  else
                                 newRealty= new SellingDB();
    //**********************************************************************************
                                newRealty.OwnerId = "";
                                newRealty.Type = (string)RealtyType.SelectedItem;
                                newRealty.City = City.SelectedItem.ToString();
                                newRealty.SubCity = SubCity.SelectedItem.ToString();
                                newRealty.Street = Street.Text;
    Friday, December 13, 2013 11:33 PM

Answers

  • In most cases selected item is a row that contain multiple fields which is an array.  The fields in the array can be index by index number of name.  You didn't specify the object (control) you are using and the format is a little different depending on which object you are using.  Below are some of the common syntax that is used and which one you need to use depends on the object.

    (string)RealtyType.SelectedItem[0]
    (string)RealtyType.SelectedItem["abc"]
    (string)RealtyType.SelectedItem["abc"].value
    (string)RealtyType.SelectedItem["abc"].text
    
    


    jdweng

    • Marked as answer by GhadaNaim Sunday, December 15, 2013 2:46 PM
    Saturday, December 14, 2013 1:14 AM
  • I donth think this works, you can use a design pattern for creating objects....creational design pattern- factory pattern

    check out this simple example here

    Illustrating Factory pattern with a very basic example



    Mark ANSWER if this reply resolves your query, If helpful then VOTE HELPFUL
    INSQLSERVER.COM Mohammad Nizamuddin


    Saturday, December 14, 2013 7:05 AM
  • Hi Mohamad 

    you can simply solve this issue by using  inheritance

        public class DB
        {
            public string OwnerId { get; set;}
            public string Type { get; set;}
            public string City { get; set;}
            public string SubCity { get; set;}
            public string Street { get; set;}
        }
    
        public class RentingDB:DB 
        {
        }
        public class SellingDB:DB 
        {
        }
    
                DB newRealty;
                if (Rent)
                    newRealty = new RentingDB();
                else
                    newRealty = new SellingDB();
                //**********************************************************************************
                newRealty.OwnerId = "";
                newRealty.Type = (string)RealtyType.SelectedItem;
                newRealty.City = City.SelectedItem.ToString();
                newRealty.SubCity = SubCity.SelectedItem.ToString();
                newRealty.Street = Street.Text;


    Happy Coding, RDRaja

    • Marked as answer by GhadaNaim Sunday, December 15, 2013 2:46 PM
    Saturday, December 14, 2013 9:33 AM

All replies

  • In most cases selected item is a row that contain multiple fields which is an array.  The fields in the array can be index by index number of name.  You didn't specify the object (control) you are using and the format is a little different depending on which object you are using.  Below are some of the common syntax that is used and which one you need to use depends on the object.

    (string)RealtyType.SelectedItem[0]
    (string)RealtyType.SelectedItem["abc"]
    (string)RealtyType.SelectedItem["abc"].value
    (string)RealtyType.SelectedItem["abc"].text
    
    


    jdweng

    • Marked as answer by GhadaNaim Sunday, December 15, 2013 2:46 PM
    Saturday, December 14, 2013 1:14 AM
  • I donth think this works, you can use a design pattern for creating objects....creational design pattern- factory pattern

    check out this simple example here

    Illustrating Factory pattern with a very basic example



    Mark ANSWER if this reply resolves your query, If helpful then VOTE HELPFUL
    INSQLSERVER.COM Mohammad Nizamuddin


    Saturday, December 14, 2013 7:05 AM
  • Hi Mohamad 

    you can simply solve this issue by using  inheritance

        public class DB
        {
            public string OwnerId { get; set;}
            public string Type { get; set;}
            public string City { get; set;}
            public string SubCity { get; set;}
            public string Street { get; set;}
        }
    
        public class RentingDB:DB 
        {
        }
        public class SellingDB:DB 
        {
        }
    
                DB newRealty;
                if (Rent)
                    newRealty = new RentingDB();
                else
                    newRealty = new SellingDB();
                //**********************************************************************************
                newRealty.OwnerId = "";
                newRealty.Type = (string)RealtyType.SelectedItem;
                newRealty.City = City.SelectedItem.ToString();
                newRealty.SubCity = SubCity.SelectedItem.ToString();
                newRealty.Street = Street.Text;


    Happy Coding, RDRaja

    • Marked as answer by GhadaNaim Sunday, December 15, 2013 2:46 PM
    Saturday, December 14, 2013 9:33 AM
  • Thank you :)
    Saturday, December 14, 2013 11:20 AM
  • Inheritance will definitely solve the problem, even if you go for a creation design pattern it will talk about the same inheritance..you have base class and then concrete classes


    Mark ANSWER if this reply resolves your query, If helpful then VOTE HELPFUL
    INSQLSERVER.COM Mohammad Nizamuddin

    Saturday, December 14, 2013 7:56 PM