What is the datatype of "value" in "set" accessor?

565 views Asked by At

I'm just wondering what is the datatype of value variable in C#'s set accessor?

Because I want to implement type-hinting in C#'s set accessor.

For example, I have a setter method:

public User
{

    private string username;

    public void setUsername(SingleWord username)
    {
        this.username = username.getValue(); // getValue() method from "SingleWord" class returns "string"
    }

}

Now how do I implement this in C#'s accessor syntax?

public User
{
    public string Username
    {
        get ;
        set {
            // How do I implement type-hinting here for class "SingleWord"?
            // Is it supposed to be:
            // this.Username = ((SingleWord)value).getValue();    ???
        }
    }

}

So that I can call it this way:

User newuser = new User() {
    Username = new SingleWord("sam023")
};

Thanks in advance!

EDIT: Here's the source code of SingleWord:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Guitar32.Exceptions;
using Guitar32.Common;

namespace Guitar32.Validations
{
    public class SingleWord : Validator, IStringDatatype
    {
        public static String expression = "^[\\w\\S]+$";
        public static String message = "Spaces are not allowed";
        private String value;

        public SingleWord(String value, bool throwException = false) {
            this.value = value;
            if (throwException && value != null) {
                if (!this.isValid()) {
                    throw new InvalidSingleWordException();
                }
                //if (this.getValue().Length > 0) {
                //    if (!this.isWithinRange()) {
                //        throw new Guitar32.Exceptions.OutOfRangeLengthException();
                //    }
                //}
            }
        }

        public int getMaxLength() {
            return 99999;
        }

        public int getMinLength() {
            return 1;
        }

        public String getValue() {
            return this.value;
        }

        public bool isWithinRange() {
            return this.getValue().Length >= this.getMinLength() && this.getValue().Length <= this.getMaxLength();
        }

        public override bool isValid() {
            return this.getValue().Length > 0 ? Regex.IsMatch(this.getValue(), expression) : true;
        }
    }

    public class InvalidSingleWordException : Exception {
        public InvalidSingleWordException() : base("Value didn't comply to Single Word format")
        { }
    }
}

I used this class to provide back-end validation by adding SingleWord as the datatype required from the setter.

1

There are 1 answers

1
Matthew Haugen On BEST ANSWER

The type of value is the type of the property, no matter what.

So in your example,

public string Username
{
    ...
    set
    {
        value.GetType() // -> string
        ...
    }
}

The simple solution for what you're looking for is to just call .getValue() on your SingleWord instance,

User newuser = new User()
{
    Username = new SingleWord("sam023").getValue()
};

Or better yet, but I assume this won't work because of code you haven't shown us,

User newuser = new User()
{
    Username = "sam023"
};

But if that's an absolute no-go, what it sounds like you're looking for is an implicit operator on SingleWord. If you have the ability to modify the class, you can add an operator that looks like this, and it'll automatically perform the conversion to a string such that you should be able to use the syntax you've listed.

public static implicit operator string(SingleWord d)
{
    return d.getValue();
}