Search code examples
c#umbracoumbraco8

When using .Value I'm not sure why I receive 'is a method, which is not valid in the given context'


In converting a string to a number I can't seem to be able to use .Value.

I'm using this to build responsive images using Umbraco.

public static IHtmlString GetSrcSet(this IPublishedContent item, int width = 0, int height = 0, string widths = "300,600,768")
{
       StringBuilder sb = new StringBuilder(string.Empty);

       if (width == 0)
       {
           width = int.Parse(item.GetProperty("UmbracoWidth").ToString());
       }

       if (height == 0)
       {
           height = int.Parse(item.GetProperty("UmbracoHeight").ToString());
       }

       string[] splitWidths = widths.Split(',');

       foreach (string newWidth in splitWidths)
       {
           . . . 
       }

       return (new HtmlString(sb.ToString()));
}

I'm confident I need to use .Value on these lines

 if (width == 0)
 {
   width = int.Parse(item.GetProperty("UmbracoWidth").Value.ToString());
 }

 if (height == 0)
 {
   height = int.Parse(item.GetProperty("UmbracoHeight").Value.ToString());
 }

But then I get

Error CS0119 'PublishedElementExtensions.Value(IPublishedElement, string, string, string, Fallback, object)' is a method, which is not valid in the given context

Omitting .Value:

Input string was not in a correct format.


Solution

  • You need to add brackets after the Value. You are currently calling Value like it's a property when you need to call it like a method.

     if (width == 0)
     {
       width = int.Parse(item.GetProperty("UmbracoWidth").Value().ToString());
     }
    
     if (height == 0)
     {
       height = int.Parse(item.GetProperty("UmbracoHeight").Value().ToString());
     }