Click here to Skip to main content
15,904,822 members
Home / Discussions / C#
   

C#

 
AnswerRe: C# Convertion PinPopular
parths26-Mar-13 21:08
parths26-Mar-13 21:08 
GeneralRe: C# Convertion Pin
OriginalGriff26-Mar-13 21:21
mveOriginalGriff26-Mar-13 21:21 
GeneralRe: C# Convertion Pin
parths26-Mar-13 21:28
parths26-Mar-13 21:28 
GeneralRe: C# Convertion Pin
OriginalGriff26-Mar-13 21:34
mveOriginalGriff26-Mar-13 21:34 
GeneralRe: C# Convertion Pin
parths26-Mar-13 21:45
parths26-Mar-13 21:45 
AnswerRe: C# Convertion Pin
Midnight Ahri26-Mar-13 22:08
Midnight Ahri26-Mar-13 22:08 
GeneralRe: C# Convertion Pin
harold aptroot27-Mar-13 0:28
harold aptroot27-Mar-13 0:28 
AnswerRe: C# Convertion Pin
OriginalGriff26-Mar-13 21:33
mveOriginalGriff26-Mar-13 21:33 
To add to (and correct) what parths said:

parths wrote:
a.ToString would throw a null reference exception if a is null

No, it doesn't! It will throw a NullReferenceException, since it has no instance to call a method from.

There is also:
C#
txtValue = a as string;
Which will try to do a cast to a string, and return null if the cast cannot be performed instead of throwing an exception. This is frequently used when dealing with a variety of objects that derive from a single base class. For example:
C#
foreach (Control c in Controls)
   {
   Button b = c as Button;
   if ( b != null)
      {
      ...
      }
   }


There is also an implicit call to ToString when you concatenate a strign with a non-string:
C#
int i = 999;
string s = "The value is: " + i;
Produces
The value is: 999
because the system does an implicit call to ToString to convert the data.

You should also note that unless a class specifically implements ToString as an override, the default Object version will be called. This returns a string which contains the fully qualified name of the instance type, which catches a lot of people out, who assume that
C#
Image i = Image.FromFile(@"D:\Temp\MyPic.jpg");
string s = i.ToString();
string sql = "INSERT INTO MyTable (userPic) VALUES ('" + s + "')";
...
will insert the image data into their database for later. What it actually inserts is the text
System.Drawing.Bitmap
which confuses them when it fails to work as a picture on retrieval.

[edit]Spurious code block removed Blush | :O - Thanks Richard! - OriginalGriff[/edit]
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)


modified 27-Mar-13 4:57am.

GeneralRe: C# Convertion Pin
Richard MacCutchan26-Mar-13 22:54
mveRichard MacCutchan26-Mar-13 22:54 
GeneralRe: C# Convertion Pin
OriginalGriff26-Mar-13 22:57
mveOriginalGriff26-Mar-13 22:57 
GeneralRe: C# Convertion Pin
Keith Barrow26-Mar-13 23:51
professionalKeith Barrow26-Mar-13 23:51 
AnswerRe: C# Convertion Pin
V.26-Mar-13 22:29
professionalV.26-Mar-13 22:29 
QuestionLinq To SQL Left Join Pin
Kevin Marois26-Mar-13 17:01
professionalKevin Marois26-Mar-13 17:01 
AnswerRe: Linq To SQL Left Join Pin
Simon_Whale26-Mar-13 23:19
Simon_Whale26-Mar-13 23:19 
Questionserialization / type cast differs between service and console execution Pin
JudyL_MD26-Mar-13 14:20
JudyL_MD26-Mar-13 14:20 
GeneralRe: serialization / type cast differs between service and console execution Pin
JudyL_MD26-Mar-13 14:22
JudyL_MD26-Mar-13 14:22 
AnswerRe: serialization / type cast differs between service and console execution Pin
Dave Kreskowiak26-Mar-13 14:39
mveDave Kreskowiak26-Mar-13 14:39 
QuestionRe: serialization / type cast differs between service and console execution Pin
JudyL_MD26-Mar-13 15:13
JudyL_MD26-Mar-13 15:13 
AnswerRe: serialization / type cast differs between service and console execution Pin
Mycroft Holmes26-Mar-13 15:34
professionalMycroft Holmes26-Mar-13 15:34 
GeneralRe: serialization / type cast differs between service and console execution Pin
JudyL_MD26-Mar-13 16:21
JudyL_MD26-Mar-13 16:21 
GeneralRe: serialization / type cast differs between service and console execution Pin
Mycroft Holmes26-Mar-13 16:49
professionalMycroft Holmes26-Mar-13 16:49 
GeneralRe: serialization / type cast differs between service and console execution Pin
Dave Kreskowiak26-Mar-13 17:02
mveDave Kreskowiak26-Mar-13 17:02 
GeneralRe: serialization / type cast differs between service and console execution Pin
JudyL_MD27-Mar-13 1:42
JudyL_MD27-Mar-13 1:42 
GeneralRe: serialization / type cast differs between service and console execution Pin
Dave Kreskowiak27-Mar-13 1:48
mveDave Kreskowiak27-Mar-13 1:48 
GeneralRe: serialization / type cast differs between service and console execution Pin
JudyL_MD27-Mar-13 1:52
JudyL_MD27-Mar-13 1:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.