简体   繁体   中英

How can I define variables that are not used like types?

I have a code written in Visual Studio 2017.
Now I'm trying to use Visual Studio 2015, but Tuple installation failed.
So I'm coding without using Tuple.

This code

namespace TEST00
{
    public class Class1<T> : IValueConverter
    {
        public T ValueForTrue { get; set; }

        public T ValueForFalse { get; set; }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            bool v = new bool();
            bool v1 = (value is v) && v;
            return v1 ? ValueForTrue : ValueForFalse;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

causes

CS0118 : 'v' is a variable but is used like a type

Original code in Visual Studio 2017 is

namespace TEST00
{
    public class Class1<T> : IValueConverter
    {
        public T ValueForTrue { get; set; }

        public T ValueForFalse { get; set; }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return ((value is bool v) && v) ? ValueForTrue : ValueForFalse;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

and it works.

I don't know how to define 'v'.
How can I define variables that are not used like types?

你正在寻找这个:

    bool v1 = (value is bool) && (bool)value;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM