null

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

I have the following:
<%= Html.TextBox("ThumbnailPath",
Path.GetFileName((string)ViewData["ThumbnailPath"] ??
ViewData.Model.SlidePaper.Slide.ThumbnailPath.ToString()) )%>

I get an error when ThumbnailPath is null ... How can I avoid that?

Can I still keep my code without if block?

Thanks,
Miguel
 
have you tried using .ToString() instead of (String) ?

usually .ToString() iwll return blank when it's null instead of actual null.

<%= Html.TextBox("ThumbnailPath",
Path.GetFileName(ViewData["ThumbnailPath"].ToString() ??
ViewData.Model.SlidePaper.Slide.ThumbnailPath.ToString()) )%>

if that doesnt work, a simple inline if statement might be your solution:

(condition ? if its true do this : otherwise do this)

Path.GetFileName(ViewData["ThumbnailPath"] == null ? "" :
(string)ViewData["ThumbnailPath"] )

watchout on the parenthesis I think I'm off somewhere in my sample solutions
I have you. but I'm sure you get the idea.
 
have you tried using .ToString() instead of (String) ?

usually .ToString() iwll return blank when it's null instead of actual null.

<%= Html.TextBox("ThumbnailPath",
Path.GetFileName(ViewData["ThumbnailPath"].ToString() ??
ViewData.Model.SlidePaper.Slide.ThumbnailPath.ToString()) )%>

if that doesnt work, a simple inline if statement might be your solution:

(condition ? if its true do this : otherwise do this)

Path.GetFileName(ViewData["ThumbnailPath"] == null ? "" :
(string)ViewData["ThumbnailPath"] )

watchout on the parenthesis I think I'm off somewhere in my sample solutions
I have you. but I'm sure you get the idea.

shapper said:
I have the following:
<%= Html.TextBox("ThumbnailPath",
Path.GetFileName((string)ViewData["ThumbnailPath"] ??
ViewData.Model.SlidePaper.Slide.ThumbnailPath.ToString()) )%>
I get an error when ThumbnailPath is null ... How can I avoid that?
Can I still keep my code without if block?
Thanks,
Miguel

Thanks
 
Rogelio said:
have you tried using .ToString() instead of (String) ?

usually .ToString() iwll return blank when it's null instead of actual null.

No, it does not usually return blank if the reference is null. Actually
it never does. You can't call the ToString method on a null reference,
that will cause a null reference exception.
 
Back
Top