C # - What is the datatype of "value" in "set" accessor?
I'm just wondering what is the datatype of a variable value
in a C # set
accessor?
Because I want to implement type hint in C # 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 to implement this in C # access 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, I can call it like this:
User newuser = new User() {
Username = new SingleWord("sam023")
};
Thanks in advance!
EDIT: Here's the source code 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 have used this class to provide internal validation by adding SingleWord
as the datatype required by the installer.
source to share
Type value
- the type of the property, no matter what.
So in your example
public string Username
{
...
set
{
value.GetType() // -> string
...
}
}
A simple solution for what you are looking for is to just call .getValue()
on your instance SingleWord
,
User newuser = new User()
{
Username = new SingleWord("sam023").getValue()
};
Or better yet, but I'm guessing it won't work because of the code you didn't show us,
User newuser = new User()
{
Username = "sam023"
};
But if it is an absolute "no-go" then what you look like is an implicit operator on SingleWord
. If you have the ability to change the class, you can add a statement that looks like this and it will automatically convert to string
so that you can use the syntax you specified.
public static implicit operator string(SingleWord d)
{
return d.getValue();
}
source to share