0

I want to add a property as a parameter.

 /// <summary> /// Permet de passer à la prochaine valeur de la <see cref="Dimension" />. /// </summary> public void DimensionSuivante() { if (Dimension == enuDimension.Petite) Dimension = enuDimension.Maximale; else Dimension += 1; } /// <summary> /// Permet de passer à la prochaine valeur de la <see cref="Qualite" />. /// </summary> public void QualiteSuivante() { if (Qualite == enuQualite.Faible) Qualite = enuQualite.Excellente; else Qualite += 1; } /// <summary> /// Permet de passer à la prochaine valeur de la <see cref="Sensibilite" />. /// </summary> public void SensibiliteSuivante() { if (Sensibilite == enuSensibilite.ISO_800) Sensibilite = enuSensibilite.ISO_64; else Sensibilite += 1; } 

These methods gets alot repetitive, so i wanted to create a new method that we pass a Property as a parameter. I have no idea what the syntax would be. I've tried adding the object before the param. Here's the method i have so far.

 private void GetPropertyNext(PropertyName) { if (PropertyName == FirstOfEnu) PropertyName = LastOfEnu; else PropertyName += 1; } 
1
  • In last code you need to define variable name after PropertyName. Commented Mar 17, 2017 at 1:12

1 Answer 1

1

You cannot pass properties by reference. See How to pass properties by reference in c#? and C# Pass a property by reference.

But usually, you don't need to. And that's the case here. I think your method should look more like this:

static T IncrementEnum<T>(T value) { int[] values = Enum.GetValues(typeof(T)).Cast<int>().ToArray(); int i = (int)(object)value, min = values.Min(), max = values.Max(); return (T)(object)(i == max ? min : i + 1); } 

Then you can call it like:

Dimension = IncrementEnum(Dimension); 

The above method is somewhat costly, because each time you call it, it has to determine the min and max values. You can encapsulate it in a generic type, and initialize those values in the static constructor, if this is something where that performance overhead would be an issue.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.