C# How to set Property with two arguments - c#

I need to setup a Property with two arguments, for example, to append text in a log file.
Example:
public string LogText(string text, bool Overwrite)
{
get
{
return ProgramLogText;
}
set
{
ProgramLogText = value;
}
}
How do I do this?
(in the example above, I need to pass the text I want to be written in the file and 1 to overwrite (0 as default value for appending the text), else append to a text file, but when I get, I just need the text.)

You can extract class - implement your own class (struct) with Text and Overwrite properties and add some syntax sugar:
public struct MyLogText {
public MyLogText(string text, bool overwrite) {
//TODO: you may want to validate the text
Text = text;
Overwrite = overwrite;
}
public string Text {get;}
public bool Overwrite {get;}
// Let's add some syntax sugar: tuples
public MyLogText((string, bool) tuple)
: this(tuple.Item1, tuple.Item2) { }
public void Deconstruct(out string text, out bool overwrite) {
text = Text;
overwrite = Overwrite;
}
public static implicit operator MyLogText((string, bool) tuple) => new MyLogText(tuple);
//TODO: You may want to add ToString(), Equals, GetHashcode etc. methods
}
And now you can put an easy syntax
public class MyClass {
...
public MyLogText LogText {
get;
set;
}
...
}
And easy assignment (as if we have a property with 2 values):
MyClass demo = new MyClass();
// Set two values in one go
demo.LogText = ("some text", true);
// Get two values in one go
(string text, bool overWrite) = demo.LogText;

You can't.
However, you have a few possible alternative approaches: create a method, or use a Tuple instead or create a class/struct and pass as a parameter (which has been answered by someone else).
Below are some alternative methods that can also be used instead.
Alternative Method 1
Create a Tuple, but then you'd have to return a tuple string, bool.
public Tuple<string, bool> LogText { get; set; }
I wouldn't do this method because then your getter would also return two values.
Alternative Method 2
Create getter and setter methods instead.
public string GetLogText() => ProgramLogText;
public void SetLogText(string text, bool overwrite) => ProgramLogText = text; // and implement in this method your use of overwrite.

Related

Is it possible to make a variable that can be an array or a non-array?

I want to be able to do something like
public string[]|string stringsOrSingleString;
I want to create a variable that can be an array or a non-array of a specific type (a string in the example).
Example usage
I want to be able to do stringsOrSingleString = "bla" or stringsOrSingleString = new string[] { "bla" };
Do I need a custom class to do this? Preferably, I don't want to use a custom class, but if necessary then ok.
I should be able to tell later on if the value assigned was an array or non-array, using typeof or is, or something.
The whole reason for this ordeal is that I have a javascript API(that I didn't create), and I am trying to make a C# api that follows the JS api/syntax as close as possible.
Is this possible?
May be you want something like this?
public class Item<T>
{
public T Value => this.Values.Length > 0 ? this.Values[0] : default(T);
public T[] Values { get; set; }
}
The class has an array of values and a single value. There are some implementations like this, for example, when you select files with OpenFileDialog: you have a list of files (for MultiSelect case) and also a single SelectedFile. My answer is focused with this in mind. If you need another thing, give more information.
UPDATE
You can update previous class in this way:
public class Item<T>
{
public T Value => this.Values.Length > 0 ? this.Values[0] : default(T);
public T[] Values { get; set; }
public T this[int index] => this.Values[index];
public static implicit operator Item<T>(T value)
{
return new Item<T> { Values = new[] { value } };
}
public static implicit operator Item<T>(List<T> values)
{
return new Item<T> { Values = values.ToArray() };
}
public static implicit operator Item<T>(T[] values)
{
return new Item<T> { Values = values };
}
}
Example usage:
Item<string> item = "Item1";
string text = item.Value;
string sameText = item[0];
Item<string> items = new[] { "Item1", "Item2" };
string[] texts = item.Values;
string item1 = item[0];
string item2 = item[1];
You can create an instance using a simple object or an array. You can access to the first value using Value property and to all items using Values. Or use the indexer property to access to any item.
In C# you need to know the type of the variable. It's difficult work in the same form of JavaScript. They are very different languages.

How to restrict a variable to a fixed set of strings?

If I want to restrict the values of the spicelevel column in the database to 1, 2 and 3, I could do something like
private enum SpiceLevel
{
Low=1,
Medium=2,
Hot=3
}
Then in the code I could do (int)SpiceLevel.Low to pick 1 as the spice level.
Now what if I have a need where I can only accept "Red Rose","White Rose" and "Black Rose" for the values of a column in the database? What is a graceful way to handle this?
I am thinking of storing them in a config file or constants, but neither is as graceful as enums. Any ideas?
Update:
The answer here worked for me
You can use a property for this
public string[] AllowedRoses = new string[] { "Red Rose", "White Rose" ,"Black Rose" };
string _Rose = "Red Rose";
public string Rose
{
get
{
return _Rose;
}
set
{
if (!AllowedRoses.Any(x => x == value))
throw new ArgumentException("Not valid rose");
_Rose = value;
}
}
I can see the following options:
verify the value in the setter (see for example l4V's answer)
conceptually, you're thinking about an enum. So you could do the following:
enum RoseType { RedRose, WhiteRose, BlackRose };
and then provide appropriate conversion from this enum to string. Two convenient options how to do it are described here: Enum ToString with user friendly strings. One is to use a custom Description attribute, and the second (I'd prefer this one) to provide an extension method:
public static class RoseTypeExtensions
{
public static string GetString(this RoseType #this)
{
switch (#this)
{
case RoseType.RedRose:
return "Red Rose";
case RoseType.WhiteRose:
return "White Rose";
case RoseType.BlackRose:
return "Black Rose";
default:
throw new InvalidOperationException();
}
}
}
create a set of constants:
public class RoseType
{
public readonly RoseType RedRose = new RoseType("Red Rose");
public readonly RoseType WhiteRose = new RoseType("White Rose");
public readonly RoseType BlackRose = new RoseType("Black Rose");
public string Content { get; private set; }
private RoseType(string content)
{
this.Content = content;
}
public override string ToString()
{
return this.Content;
}
}
As Oskar Berggren correctly pointed out in the comment, RoseType should also provide other standard overrides beside ToString: Equals, GetHashCode, operator== and operator!=.
There is no really good solution. All of them require the database to be "synchronized" with the enum in C#.
Simplest solution:
What you said store the enum values as integers in the database.
Almost as simple but less efficient:
Store the values as strings in the database and convert between string and enum with anEnumVar.ToString() and Enum.Parse (or any of the other parse methods in Enum).
Complex but flexible:
Have a sort of enum in the database: a table with string values and ids and then use foreign keys to that table where you want to save the enums. This allows you to either select/update/insert using the numeric value or the string value (via a join).
It also maintains integrity as it is not possible to store an integer which has no corresponding enum value.
The downside is the complexity.
Create a mapping of string to enum with Dictionary<string, SpiceLevel> to associate the string to the Enum. Wrap them in a class.
You could also use a Decorator attribute [Name("Red Rose"] Low=1, and get that from the enum itself, but that involves reflection, which has some performance issues, especially when iterating through enum values to find the one with the matching attribute.
public static class Spice
{
public enum Level
{
Low = 1,
Medium = 2,
Hot = 3
}
private static readonly Dictionary<string, Level> spices = new Dictionary<string, Level>{
{ "Red Rose", Level.Low },
{ "White Rose", Level.Medium },
{ "Black Rose", Level.Hot },
};
public static bool TryGet(string spiceName, out Level spiceLevel) => spices.TryGetValue(spiceName, out spiceLevel);
public static string SpiceName(Level target) => Enum.GetName(typeof(Spice.Level), target);
}
/// <summary>
/// Some tests to validate it works. This could be a unit test or just in a console app
/// </summary>
public class SpiceTest
{
public void VerifyBlackRoseIsHot()
{
string subject = "Black Rose";
Spice.Level expectedSpice;
// Here's the ease of use. Pass a string, get an enum and whether it's a valid string
var result = Spice.TryGet(subject, out expectedSpice);
//Some Assertion from a unit test library
Assert.True(result, $"Unable to find spice '{subject}', when it should exist");
Assert.True(Spice.Level.Hot.Equals(expectedSpice), $"The returned spice '{ Spice.SpiceName(expectedSpice) }' was not the value 'Hot' as expected");
}
}

java enums vs C# enums - missing features

in java I could easily describe an enum with aditional data.
I could describe it something like this
public enum OperatorType
{
GreaterOrEqual (">=", "GreaterOrEqual"),
Greater (">" ,"Greater"),
Less ("<", "Less"),
LessOrEqual ("<=", "LessOrEqual"),
Equal ("==", "Equal"),
Between ("Between", "Between"),
Around ("Around","Around");
private final String symbol;
private final String name;
private OperatorType(final String symbol, final String name) {
this.symbol = symbol;
this.name = name;
}
}
And then add a static method that iterates over values(), adds all data to a hashmap and allow to retrieve from the map full enum data by one of its attriburtes as a key.
In brief, enum is a very developed type in java.
Now,
moving to c#, what are my options?
I want to hold an enum with its attributes, load it to a map, and retreive by key when I need. Do I have anything to assist (like, a singletone for each enum - which is not a good idea).
Thanks.
I would just create a class with public static readonly instances of each type and ditch enums altogether. You can use them as dictionary keys or do whatever you like. If you still intend to map them to an underlying data type (int) then you can create implicit operators for that too.
public class OperatorType
{
private static readonly Dictionary<int, OperatorType> OperatorMapping = new Dictionary<int, OperatorType>();
public static readonly OperatorType GreaterOrEqual = new OperatorType(0, ">=", "GreaterOrEqual");
public static readonly OperatorType Greater = new OperatorType(1, ">", "Greater");
public readonly String symbol;
public readonly String name;
private readonly int underlyingValue;
private OperatorType(int underlyingValue, string symbol, string name) {
this.underlyingValue = underlyingValue;
OperatorMapping[underlyingValue] = this;
this.symbol = symbol;
this.name = name;
}
public static implicit operator int(OperatorType operatorType)
{
return operatorType.underlyingValue;
}
public static implicit operator OperatorType(int value)
{
return OperatorMapping[value];
}
}
Sample usage:
Dictionary<OperatorType, string> operators = new Dictionary<OperatorType, string>();
operators.Add(OperatorType.GreaterOrEqual, "Greater or equal");
Console.WriteLine(operators[OperatorType.GreaterOrEqual]); //"Greater or equal"
OperatorType operatorType = 1;
Console.WriteLine(operatorType.name); //"Greater"
If you don't care about an underlying value, don't include it. Also consider whether or not the Dictionary mapping should be threadsafe for your usage. You can also expose a static IEnumerable<OperatorType> (or other collection) to get all operators defined if you want.
EDIT: On second thought, explicit operators are possibly preferable instead of implicit, both to conform with typical .NET best practices and to better match typical enum conversions.
The most convinient workaround might be to create an extension method to your enum type, and return the associated symbols.
Something like this:
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
tester t = tester.x;
t.testenums();
Console.ReadKey();
}
}
public static class ext
{
public static void testenums(this tester x)
{
Console.WriteLine(x.ToString());
}
}
public enum tester
{
x,
y
}
}
Of course you can write a more complex extension method, with return value, etc, this is just an example how to do it.
You can create an attribute:
public class EnumKeyAttribute : Attribute
{
public string Key { get; set; }
public string Description { get; set; }
public EnumKeyAttribute(string key, string description)
{
this.Key = key;
this.Description = description;
}
}
Then apply it to your enum
public enum OperatorType
{
[EnumKey(">=", "GreaterOrEqual")]
GreaterOrEqual,
[EnumKey(">", "Greater")]
Greater,
[EnumKey("<", "Less")]
Less,
[EnumKey("<=", "LessOrEqual")]
LessOrEqual,
[EnumKey("==", "Equal")]
Equal,
[EnumKey("Between", "Between")]
Between,
[EnumKey("Around", "Around")]
Around
}
To get the attribute data you can use reflection. Below is an example of getting the attribute for "Less"
MemberInfo memberInfo = typeof(OperatorType).GetMember(OperatorType.Less.ToString()).FirstOrDefault();
if(memberInfo != null)
{
EnumKeyAttribute attribute = (EnumKeyAttribute)memberInfo.GetCustomAttributes(typeof(EnumKeyAttribute), false).FirstOrDefault();
Console.WriteLine(attribute.Key);
Console.WriteLine(attribute.Description);
}
But because these enums are not created at runtime you can increase your efficiency by creating a static method that looks up the value in a dictionary. Do this as an extension method for ease of use
public static class KeyFinder
{
private static Dictionary<OperatorType, EnumKeyAttribute> lookupTable =
new Dictionary<OperatorType, EnumKeyAttribute>();
public static EnumKeyAttribute GetKey(this OperatorType type)
{
if (lookupTable.ContainsKey(type))
{
return lookupTable[type];
}
MemberInfo memberInfo = typeof(OperatorType).GetMember(type.ToString()).FirstOrDefault();
if (memberInfo != null)
{
EnumKeyAttribute attribute = (EnumKeyAttribute)memberInfo.GetCustomAttributes(typeof(EnumKeyAttribute), false).FirstOrDefault();
if (attribute != null)
{
lookupTable.Add(type, attribute);
return attribute;
}
}
// add a null value so next time it doesn't use reflection only to find nothing
lookupTable.Add(type, null);
return null;
}
}
So now to get the values you simply do the following:
OperatorType.Less.GetKey().Key
OperatorType.Less.GetKey().Description
Just be careful of null reference exceptions (since it will return null if it can't find an attribute). If you want to find by key you can simply create other extension methods that use the string value as the key.
C# doesn't really have the same feature. However there are several possibilities to get really close (and potentially more flexible as well).
Sticking to regular enums, you could use attributes to enrich with extra information. Of course, this requires reflection to work with that
public enum OperatorType
{
[DisplayName(">=")]
GreaterOrEqual,
// ...
}
There are several patterns to work with this, e.g. http://www.codeproject.com/Articles/28087/DisplayNameAttribute-for-Enumerations, google for more.
Another approach can be to enhance your enumeration types using regular classes:
public class OperatorType
{
public static OperatorType GreaterOrEqual = new OperatorType(">=", "GreaterOrEqual");
// ...
string symbol;
string name;
private OperatorType(string symbol, string name)
{
this.symbol = symbol;
this.name = name;
}
}
This article describes some other ways to work with enum-like types in C#
If you really need the functionality of Java-style enums in C#, I see three reasonable ways to implement it:
Use a C# enum and a static class of helper methods. You lose type safety, but this is an otherwise very workable solution.
Use a C# enum and a set of extension methods. Probably the most idiomatic C# solution, but you still have to deal with the loss of type safety (your extension methods should be able to cope with out-of-range values, even if only by throwing an exception).
Use the type-safe enum pattern that was common in Java before the language gained the enum keyword in Java 5. If you have non-trivial logic for each enum value, this would be my preference.

Convert custom array object to string array

I'm trying to convert an object array of my class to a string array.
In this example I have two classes. In one of my classes I want to creat two variables, objects of the other class. On variable will be a array and the other a "regular" variable.
Two classes name. ShowResult and Game.
In the Game class I write:
private ShowResult[] _showResults;
private ShowResult _showResult;
In my properties in my Game class I try to convert this two variables to string array and string:
public string[] ShowResults
{
get { return _showResults.ToString().ToArray(); }
set { _showResults.ToString().ToArray() = value; }
}
public string ShowResult
{
get { return _showResult.ToString(); }
set { _showResult = value; }
}
This doesn't work. I really want my custom object to be converted to string array and to string. But I get an error.. I know I can, but I don't know just how..
If anyone has any suggestion I would be greatful. OBS, this is just an example. But still I don't get why it wont work..?
By the way, sorry for my bad english. ;)
Best regards
Try this:
_showResults.Select(a => a.ToString()).ToArray()
Given the nature of your code (i.e. strings appear to map directly to ShowResults instances), you might want to consider implementing implicit operators:
ShowResults s = "hello";
string ss = s;
Console.WriteLine(ss); // "hello"
class ShowResults
{
public string SomeProp
{
get; private set;
}
public static implicit operator ShowResults(string s)
{
//this is where you'd parse your string
//to form a valid ShowResults
return new ShowResults{SomeProp = s};
}
public static implicit operator string(ShowResults s)
{
return s.SomeProp;
}
}

How can I implement custom attributes in .NET 2.0?

Unfortunately I am still working on .NET 2.0. I have not created a custom attribute before.
I want to create a CustomStringFormatAttribute:.
If a class, say Customer.Name, has:
MaxLength=30
ActualLength=10
I need to pad it with empty spaces till it reached 30.
I also need an attribute for date that I can format like DisplayDataFormat
I have created the following but How do I get access to the actual value of the property within the attribute?
public class Customer
{
[CustomStringFormatAttribute(30)]
public string Name { get; set; }
//todo:customDateAttribute
public DateTime StartDate { get; set; }
}
[AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
public sealed class CustomStringFormatAttribute : Attribute
{
private readonly int maxLength;
public CustomStringFormatAttribute(int maxLength)
{
MaxLength = maxLength;
}
public int MaxLength { get; private set; }
//?Should I override ToString
public override string ToString()
{
return Format();
}
private string Format()
{
//simplified version of my formatting for brevity
string source = "value from the property of the class.";//How do I get access to the actual value of the property within the attribute?
const char paddingChar = ' ';
return source.PadLeft(maxLength, paddingChar);
}
}
Any suggestions?
Note: I' used automatic property for brevity. I don't have that luxury in .NET 2.0.
Sorry, you cannot access the class instance or the property info inside your attribute.
You should write an additional method, for example a static method in some "static" class, that allow you to do what you want to do.
Example....
public static string FormatProperty(object instance, PropertyInfo property)
{
CustomStringFormatAttribute attrib = Attribute.GetCustomAttribute(property, typeof(CustomStringFormatAttribute)) as CustomStringFormatAttribute;
return property.GetValue(instance, null).ToString().PadLeft(attrib.MaxLength, ' ');
}
public static string FormatProperty(object instance, string propertyName)
{
return FormatProperty(instance, instance.GetType().GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance));
}
But this is very uncomfortable and insanely slow since uses reflection to get property value through property info.
To access property attributes you need a PropertyInfo.
public static int GetPropertyMaxLength(PropertyInfo property)
{
CustomStringFormatAttribute attrib = Attribute.GetCustomAttribute(property, typeof(CustomStringFormatAttribute)) as CustomStringFormatAttribute;
return attrib != null ? attrib.MaxLength : int.MaxValue;
}
public static int GetPropertyMaxLength(Type type, string propertyName)
{
return GetPropertyMaxLength(type.GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance));
}
Let's suppose we put these functions inside the attribute.
Then we want to override, for example, our ToString method in our class Customer.
public override string ToString()
{
return CustomStringFormatAttribute.FormatProperty(this, "Name");
}
The problem with this is of course Speed, it uses reflection by name, very slow, and Refactoring, you will not have a compile time warning or error if property "Name" doesn't exists, you will get only an exception at runtime. I would suggest you to use another mechanism.
With newer version of the language you could use lambda expressions to obtain your property info directly by the property itself, but since you are in C# 2.0 this is not possible.
Another solution can be: add instead another property called FormattedXXX, for example FormattedName that returns the lenght as you want it and you can use that property instead of the Name property.
This will allow you to keep your formatted version of the property near your property.
You need to do it the other way around. You shouldn't have any logic in your attribute, it should simply expose properties with the information it contains (e.g. a MaxLength property). Then your Customer class should access the information provided by CustomStringFormatAttribute and format it accordingly:
private string m_Name;
public string Name
{
get
{
var formatAttribute = typeof(Customer).GetCustomAttributes(false)
.OfType<CustomStringFormatAttribute>
.SingleOrDefault();
if (formatAttribute != null)
return m_Name.PadLeft(formatAttribute.MaxLength);
return m_Name;
}
set
{
m_Name = value;
}
}

Categories

Resources