Copied to clipboard

Flag this post as spam?

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


  • PierreD Savard 183 posts 290 karma points
    Jul 30, 2015 @ 22:31
    PierreD Savard
    0

    How to test if value is null

    When I do this:

    foreach (var item in Model.Items)
    {    
        var test = item.GetValue<string>("url");
    }
    

    I got en null exception: exception of type 'System.NullReferenceException' occurred in Lecoati.LeBlender.Extension.dll but was not handled in user code

    How I can test if URL setting is defined then do something? I try HasValue('url,) but the fonction is not avalaible. Any suggestion? Thanks

  • Swathi 87 posts 281 karma points
    Jul 31, 2015 @ 11:34
    Swathi
    0

    Try this,

    foreach (var item in Model.Items) { if(!item .IsNullOrEmpty(url)) { var test = item.GetValue

  • PierreD Savard 183 posts 290 karma points
    Jul 31, 2015 @ 12:18
    PierreD Savard
    0

    No, that do not work because:

    'Lecoati.LeBlender.Extension.Models.LeBlenderValue' does not contain a definition for 'IsNullOrEmpty'

    Item = LeBlenderValue that do not implement any IsNull or HasValue, etc... I think I will have to put an "" like default value to be able to test if(item != "") of for the item to be null and not undefined....

  • Bjarne Fyrstenborg 1280 posts 3990 karma points MVP 7x c-trib
    Jul 31, 2015 @ 12:41
    Bjarne Fyrstenborg
    0

    Hi Pierre

    I haven't tested this, but maybe this?

    if(Model.Items.Any()) 
    {
        foreach (var item in Model.Items)
        {    
            var test = item.GetValue<string>("url");
        }
    }
    

    or

    foreach (var item in Model.Items.Where(x => x != null))
    {    
        var test = item.GetValue<string>("url");
    }
    

    /Bjarne

Please Sign in or register to post replies

Write your reply to:

Draft