Click here to Skip to main content
15,878,959 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
What does this code line means ? Name= (string)item.Element("Name") ?

is it means declaring variable of type string ?
Then what is the difference between below code?
string Name = item.Element("Name");

what does below means ?

i = (int)cellFormat.FontId.Value; ??? is this same as
int i =cellFormat.FontId.Value ?
Posted

Name= (string)item.Element("Name")Doesn't declare any variables - it use the already declared Name and puts a value into it.

Exactly what the item.Element("Name") part does is open: without knowing what type item is it could be anything, but the most likely is that it's a generic method that returns an object from a store of some form, using "Name" as the index to locate exactly what item you want to fetch. The (string) part casts that object to a string (if it can, it will fail and throw and exception if it can't) so that the compiler will be happy stroign the value into a string variable called Name

C#
string Name = item.Element("Name");
Does declare the Name variable, and will probably give a compilation error if the Element method does not return a string type.

Your other two lines do very similar things, but this time casting the value to a integer instead of a string.

This is basic stuff: you really need to go back in your notes and review the language from the beginning if you don't understand this too well.
 
Share this answer
 
Comments
Am Gayathri 16-Dec-14 6:53am    
Then instead of this can i do like Name= item.Element("Name").ToString(); ?? What is the difference ?
OriginalGriff 16-Dec-14 7:09am    
Yes - but it may not always give you what you want.
ToString will always return a string (unless the value is null, in which case you will get an exception and your app will crash) - but for some types it returns the type name instead of a human readable content: for example byte[] is one that catches a lot of people when they try to save images to databases. Using ToString on a byte[] does not return the array content, but "System.Byte[]" which is really useless later!

Casting is generally better because it tells you when there is a problem by throwing a cast exception so you know there has been a problem.
Am Gayathri 16-Dec-14 7:36am    
Thanks
OriginalGriff 16-Dec-14 7:45am    
You're welcome!
Quote:
What does this code line means ? Name= (string)item.Element("Name") ?
Assign to the (string) variable Name the value of item.Element("Name"), casted (that is coherced) to string.



Quote:
is it means declaring variable of type string ?
No.



Quote:
Then what is the difference between below code?
string Name = item.Element("Name");
In the former piece of code there is an explicit cast (required when item.Element("Name") type is not string, e.g. is object) which is missing in the above line.



Quote:
i = (int)cellFormat.FontId.Value; ??? is this same as
int i =cellFormat.FontId.Value ?
You may apply the same considerations to these lines.
 
Share this answer
 
Comments
Am Gayathri 16-Dec-14 6:57am    
Then can i write i = (int)cellFormat.FontId.Value this as
i = Convert.ToInt(cellFormat.FontId.Value) ? what is the difference ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900