c# Does casting create a new object?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • matthewaveryusa
    New Member
    • Jul 2008
    • 20

    c# Does casting create a new object?

    I have a little dilemma I would like to straighten out.

    I have a class Car with 100's of parameters.
    I add a bunch of Car instances to a listbox. They are now plain old objects.
    Then when I selected an item from the listbox, I would like to cast the object back to Car and print the parameters on screen.
    so I have 2 options:

    option 1:
    Code:
    Textbox1.Text = ((Car) Listbox1.SelectedItem).param1.ToString();
    Textbox2.Text = ((Car) Listbox1.SelectedItem).param2.ToString();
    ...
    Textbox100.Text = ((Car) Listbox1.SelectedItem).param100.ToString();
    option 2:

    Code:
    Car c = new Car();
    c = (Car) Listbox1.SelectedItem;
    Textbox1.Text = c.param1.ToString();
    Textbox2.Text = c.param2.ToString();
    ...
    Textbox100.Text = c.param100.ToString();
    Option 1 Could be more efficient if the cast is pointing to the object, while option 2 creates a new object, which could be more efficient if a cast creates a new object each and every time. Which one is right, which one is wrong? A reference to the MSDN website would be nice

    thanks a lot!
    Matt
  • nukefusion
    Recognized Expert New Member
    • Mar 2008
    • 221

    #2
    I would say that option 2 is almost certainly the better way to do this.
    A cast obviously requires some sort of performance overhead and in code sample 2 you only have to perform the cast once.
    In code sample 1 you have to cast each and everytime.

    I can't say whether it would cost you more in terms of performance to declare the new object in sample 2 than it would to cast X amount of times in sample one, however I do know declaring a new object in sample 2 is unnecessary. Here's how I would do it:

    Code:
    Car c = (Car) Listbox1.SelectedItem; 
    Textbox1.Text = c.param1.ToString(); 
    Textbox2.Text = c.param2.ToString(); 
    ... 
    Textbox100.Text = c.param100.ToString();

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Better use
      Code:
      Car c = (Car) Listbox1.SelectedItem;
      instead of
      Code:
       Car c = new Car();
      c = (Car) Listbox1.SelectedItem;
      There is no need for creating that new Car object which you simply discard afterwards.

      P.S Your code looks like it could have been done cleaner using arrays or ArrayLists.

      Comment

      • matthewaveryusa
        New Member
        • Jul 2008
        • 20

        #4
        I'm sorry for my ignorance but what is the difference between these two snippets with regards to c.


        Code:
        car some_other_car = new car();
        car c = new car();
        c = some_other_car;
        and

        Code:
        car some_other_car = new car();
        car c = some_other_car;
        ?

        Comment

        • Shashi Sadasivan
          Recognized Expert Top Contributor
          • Aug 2007
          • 1435

          #5
          Originally posted by matthewaveryusa
          I'm sorry for my ignorance but what is the difference between these two snippets with regards to c.


          Code:
          car some_other_car = new car();
          car c = new car();
          c = some_other_car;
          and

          Code:
          car some_other_car = new car();
          car c = some_other_car;
          ?
          You mean to c#...
          objects in c# get referenced when you do that....
          so c will point to the same meory location as some_other_car

          Comment

          • balabaster
            Recognized Expert Contributor
            • Mar 2007
            • 798

            #6
            Alas it was a design decision by the language team at MS that the WITH statement not be included in C# or you could do:

            Code:
            with (car)ListBox1.SelectedItem{
              textbox1.text = .param1;
              textbox2.text = .param2;
            }
            of course, when you point a variable to an object instance, it points to the reference, so you're really not creating a new object, what you're really doing here is saying: Create a reference c to ListBox1.Select edItem and treat it as the object type car.

            Code:
            car c = (car)listbox1.selectedItem;
            textbox1.text = c.param1;
            textbox2.text = c.param2;
            Alas, this is one of those instances where C# isn't quite as syntactically efficient as if it had adopted some of VBs keywords - those instances are few and far between though.

            In VB you can do:
            Code:
            With Cast(ListBox1.SelectedItem, Car)
              TextBox1.Text = .Param1
              TextBox2.Text = .Param2
            End With
            It would've been nice if you could do it that way in C# (as my first code block suggests).

            Comment

            • matthewaveryusa
              New Member
              • Jul 2008
              • 20

              #7
              Oh boy, good thing you sorted this out for me. This can become an issue when threading :)

              Comment

              Working...