473,405 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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:
Expand|Select|Wrap|Line Numbers
  1. Textbox1.Text = ((Car) Listbox1.SelectedItem).param1.ToString();
  2. Textbox2.Text = ((Car) Listbox1.SelectedItem).param2.ToString();
  3. ...
  4. Textbox100.Text = ((Car) Listbox1.SelectedItem).param100.ToString();
option 2:

Expand|Select|Wrap|Line Numbers
  1. Car c = new Car();
  2. c = (Car) Listbox1.SelectedItem;
  3. Textbox1.Text = c.param1.ToString();
  4. Textbox2.Text = c.param2.ToString();
  5. ...
  6. Textbox100.Text = c.param100.ToString();
  7.  
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
Nov 20 '08 #1
6 3305
nukefusion
221 Expert 100+
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:

Expand|Select|Wrap|Line Numbers
  1. Car c = (Car) Listbox1.SelectedItem; 
  2. Textbox1.Text = c.param1.ToString(); 
  3. Textbox2.Text = c.param2.ToString(); 
  4. ... 
  5. Textbox100.Text = c.param100.ToString(); 
Nov 20 '08 #2
r035198x
13,262 8TB
Better use
Expand|Select|Wrap|Line Numbers
  1. Car c = (Car) Listbox1.SelectedItem;
instead of
Expand|Select|Wrap|Line Numbers
  1.  Car c = new Car();
  2. 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.
Nov 20 '08 #3
I'm sorry for my ignorance but what is the difference between these two snippets with regards to c.


Expand|Select|Wrap|Line Numbers
  1. car some_other_car = new car();
  2. car c = new car();
  3. c = some_other_car;
and

Expand|Select|Wrap|Line Numbers
  1. car some_other_car = new car();
  2. car c = some_other_car;
?
Dec 3 '08 #4
Shashi Sadasivan
1,435 Expert 1GB
@matthewaveryusa
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
Dec 3 '08 #5
balabaster
797 Expert 512MB
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:

Expand|Select|Wrap|Line Numbers
  1. with (car)ListBox1.SelectedItem{
  2.   textbox1.text = .param1;
  3.   textbox2.text = .param2;
  4. }
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.SelectedItem and treat it as the object type car.

Expand|Select|Wrap|Line Numbers
  1. car c = (car)listbox1.selectedItem;
  2. textbox1.text = c.param1;
  3. 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:
Expand|Select|Wrap|Line Numbers
  1. With Cast(ListBox1.SelectedItem, Car)
  2.   TextBox1.Text = .Param1
  3.   TextBox2.Text = .Param2
  4. End With
It would've been nice if you could do it that way in C# (as my first code block suggests).
Dec 3 '08 #6
Oh boy, good thing you sorted this out for me. This can become an issue when threading :)
Dec 4 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Vinodh Kumar | last post by:
I see that casting changes the value of a pointer in case of multiple inheritance.In single inheritance also it is the same know?Isn't it? Vinodh Kumar P
8
by: Trishia Rose | last post by:
this is something ive always wondered, does it take cpu time at run time to typecast or just at compile time? for example consider the two little bits of code: int a = 5; int b = a; and: ...
3
by: Kurt | last post by:
i just can't figure out why something im doing is not working correctly.... public interface IInterface { int someProperty { get; set; }
7
by: Andrea Williams | last post by:
I have two objects and one inherits the other and adds a few more properties. Say I populate the first object with the properties, then want to cast it to be the second object, then I would be...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
1
by: Remco | last post by:
Hi, Let me try to simply explain my questions. I've created a portal site with different types of users, e.g. Portal Administrators and Normal Users. One base class SessionUser (has a enum...
23
by: René Nordby | last post by:
Hi there, Is there anyone that knows how to do the following? I have a class A and a class B, that 100% inherits from class A (this means that I don't have other code in class B, than...
3
by: JimM | last post by:
I am trying to create a method in VS 2003 that validates an object argument is of the proper type and within a range of values. I am trying to use a Type to define the casting and object type for...
2
by: harvie wang | last post by:
Hi, I want to implement a common Form with special interface, such as MovePoint(double,double). I create a interface first: namespace ABC.Test { public Interface IMyWindowInterface { void...
19
by: =?Utf-8?B?WWFua2VlIEltcGVyaWFsaXN0IERvZw==?= | last post by:
I'm doing my c# more and more like i used to code c++, meaning i'm casting more often than creating an instance of objects. like : protected void gvOrderDetailsRowDataBound(object sender,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.