decipher
decipher

Reputation: 148

Cannot implicitly convert from type 'void' to 'bool'

I hope that I am not asking a rather stupid question now. I have searched quite some time on Google, but I can't seem to find the answer -- or maybe I just lack the understanding of how it works.

I hope that someone can explain to me why this error occurs? I keep getting the error: "Cannot implicitly convert from type 'void' to 'bool'".

This is the code:

class User
{
    public string FirstName
    {
        get { return _FirstName; }
        set
        {
                 // the error occurs on the line below
            if((ArgChecker.ThrowOnStringNullOrWhiteSpace(_FirstName) ) )
            {
                Console.WriteLine("something1");
            }
            else { _FirstName = value; }
        }
    }
    private string _FirstName;
}


class ArgChecker
{
    public static void ThrowOnStringNullOrWhiteSpace(string arg)
    {
        if (string.IsNullOrWhiteSpace(arg))
        {
            throw new ArgumentNullException("something2");
        }
    }
}

Upvotes: 2

Views: 9064

Answers (2)

Eser
Eser

Reputation: 12546

You can not convert void to bool, Instead your property should be something like

public string FirstName
{
    get { return _FirstName; }
    set
    {
        ArgChecker.ThrowOnStringNullOrWhiteSpace(value);
        _FirstName = value; 
    }
}

Upvotes: 1

Ken Ross
Ken Ross

Reputation: 17

Shouldn't you be passing value into your ThrowOnStringNullOrWhiteSpace function (instead of _FirstName)?

Upvotes: 0

Related Questions