Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Claushingebjerg 886 posts 2415 karma points
    Jan 16, 2015 @ 09:18
    Claushingebjerg
    0

    Syntax difference when content is "TypedContent"

    I have a blob of code, that works fine and looks to be standar razor code... But when using this within a typedContext context it fails

    The working code outside of the TypedContent is this :

    if (@fieldset.HasValue("youtube")) {
        string myString = @fieldset.GetValue("youtube");
        int charPositionStart = myString.IndexOf("=");
        int lengthOfText = 11;
        myString = myString.Substring(charPositionStart+1, lengthOfText);
        var myBg = "http://img.youtube.com/vi/" + @myString + "/maxresdefault.jpg";
    
        ---- HTML stuff ----
    }
    
    I then correct this to match the TypedContent variable like this:
    if (@boxItem.HasValue("youtube")) {
        string myString = @boxItem.GetPropertyValue("youtube");
        int charPositionStart = myString.IndexOf("=");
        int lengthOfText = 11;
        myString = myString.Substring(charPositionStart+1, lengthOfText);
        var myBg = "http://img.youtube.com/vi/" + @myString + "/maxresdefault.jpg";
    
        ---- HTML stuff -----
    
    }
    
    The first error i get, and this seems to be the same through the entir code block is this:
    Compiler Error Message: CS0266: Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?)


    on this line:

    string myString = @boxItem.GetPropertyValue("youtube");
  • Dennis Aaen 4457 posts 17970 karma points admin hq c-trib
    Jan 16, 2015 @ 09:23
    Dennis Aaen
    0

    Hi Claus,

    What if you do something like this

    var myString = @boxItem.GetPropertyValue<string>("youtube");

    Does this help, or did you get same error.

    /Dennis

  • Jeavon Leopold 3008 posts 13221 karma points MVP 7x admin c-trib
    Jan 16, 2015 @ 09:25
    Jeavon Leopold
    100

    Hi Claus,

    Either make the variable implicit

    var myString = boxItem.GetPropertyValue("youtube");
    

    Or specify the return type

    string myString = boxItem.GetPropertyValue<string>("youtube");
    

    Jeavon

  • Jeavon Leopold 3008 posts 13221 karma points MVP 7x admin c-trib
    Jan 16, 2015 @ 09:26
    Jeavon Leopold
    0

    Or both as Dennis has suggested :)

    Also you don't need/want those @ in there

  • Claushingebjerg 886 posts 2415 karma points
    Jan 16, 2015 @ 09:31
    Claushingebjerg
    0
    string myString = boxItem.GetPropertyValue<string>("youtube");

    did the trick, thanks guys

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies