C#
C#

help

Root Question Message

Natro
Natro12/7/2022
❔ string.Join returns System.Int32[]

In my constructor I am getting T desiredValue.
In this case T is int[].
When debugging the below code:
string desiredValue;
if (typeof(T) == typeof(int[]))
{
    format = "{0} = [{1}]\n";
    desiredValue = string.Join(", ", this.desiredValue);
}

I end up getting System.Int32[] any idea why?
Angius
Angius12/7/2022
Maybe has something to do with desiredValue being a string..?
Natro
Natro12/7/2022
I have another member variable called desiredValue that is of type T. It's weird convention I have to follow :/
Angius
Angius12/7/2022
Ah, yeah, desiredValue is probably that System.Int32[] string
Angius
Angius12/7/2022
And joining a single string... results in a single string
Natro
Natro12/7/2022
Natro
Natro12/7/2022
Natro
Natro12/7/2022
renamed the string desiredValue to newValue to avoid confusion
Angius
Angius12/7/2022
Huh
Angius
Angius12/7/2022
Somewhere, desiredValue gets, somehow, turned into a string
Natro
Natro12/7/2022
Let me post the whole class.
public class DirectionalitySetCalibrationData<T> : TestActionBase<HearingInstrumentContext>
{
    public DirectionalitySetCalibrationData(string propertyName, T desiredValue)
    {
        this.propertyName = propertyName;
        this.desiredValue = desiredValue;
    }

    public override bool Execute(HearingInstrumentContext hiContext, TestCaseContext testCaseContext)
    {
        const string assemblyName = "ReSound.Algorithm.Dooku3";
        string pattern = propertyName + " = .*?\\n";

        Type compress = Assembly.LoadFrom(assemblyName + ".Platform.dll").GetType(assemblyName + ".Compress");
        object comCal = Activator.CreateInstance(compress, null);

        var compressCalibrationData = (string)hiContext.SoundProgram.Blackboard.Properties[Directionality.CalibrationData.PropertyName].Value;
        var calibrationData = (string)compress.GetMethod("DecompressString").Invoke(comCal, new object[] { compressCalibrationData });

        string format = "{0} = {1}\n";
        string newValue = string.Empty;
        if (typeof(T) == typeof(int[]))
        {
            format = "{0} = [{1}]\n";
            newValue = string.Join(", ", this.desiredValue);
        }
        else if (typeof(T) == typeof(Vector))
        {
            newValue = this.desiredValue.ToString().Replace(",", ".").Replace(";", ", ");
        }
        else
        {
            newValue = this.desiredValue.ToString().Replace(",", ".");
        }

        string testCalibrationData = Regex.Replace(
           calibrationData, pattern,
           string.Format(format, propertyName, desiredValue));

        hiContext.SoundProgram.Blackboard.Properties[Directionality.CalibrationData.PropertyName].Value = compress
            .GetMethod("CompressString").Invoke(comCal, new object[] { testCalibrationData });

        return true;
    }

    private readonly string propertyName;
    private readonly T desiredValue;
}
Natro
Natro12/7/2022
No this is C#
Natro
Natro12/7/2022
Should I just hard cast then?
Natro
Natro12/7/2022
newValue = string.Join(", ", (int[])this.desiredValue);
Natro
Natro12/7/2022
But I am already checking if
typeof(T) == typeof(int[])
Natro
Natro12/7/2022
So in that case it should be fine?
Natro
Natro12/7/2022
Okay, thank you very much 🙂
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy