C
C#2y ago
Natro

✅ 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);
}
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?
14 Replies
Angius
Angius2y ago
Maybe has something to do with desiredValue being a string..?
Natro
Natro2y ago
I have another member variable called desiredValue that is of type T. It's weird convention I have to follow :/
Angius
Angius2y ago
Ah, yeah, desiredValue is probably that System.Int32[] string And joining a single string... results in a single string
MODiX
MODiX2y ago
Angius#1586
REPL Result: Success
string.Join(" | ", "henlo")
string.Join(" | ", "henlo")
Result: string
henlo
henlo
Compile: 474.770ms | Execution: 29.346ms | React with ❌ to remove this embed.
Natro
Natro2y ago
Natro
Natro2y ago
Natro
Natro2y ago
renamed the string desiredValue to newValue to avoid confusion
Angius
Angius2y ago
Huh Somewhere, desiredValue gets, somehow, turned into a string
Natro
Natro2y ago
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;
}
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;
}
No this is C#
MODiX
MODiX2y ago
Retax#0813
REPL Result: Success
class C<T> {
public static string Join(T t) => string.Join(", ", t);
}

C<int[]>.Join(new int[] { 1, 2, 3, 4, 5 })
class C<T> {
public static string Join(T t) => string.Join(", ", t);
}

C<int[]>.Join(new int[] { 1, 2, 3, 4, 5 })
Result: string
System.Int32[]
System.Int32[]
Compile: 528.068ms | Execution: 51.706ms | React with ❌ to remove this embed.
Natro
Natro2y ago
Should I just hard cast then? newValue = string.Join(", ", (int[])this.desiredValue);
MODiX
MODiX2y ago
Retax#0813
REPL Result: Success
class C<T> {
public static string Join(T[] t) => string.Join(", ", t);
}

C<int>.Join(new int[] { 1, 2, 3, 4, 5 })
class C<T> {
public static string Join(T[] t) => string.Join(", ", t);
}

C<int>.Join(new int[] { 1, 2, 3, 4, 5 })
Result: string
1, 2, 3, 4, 5
1, 2, 3, 4, 5
Compile: 542.995ms | Execution: 54.950ms | React with ❌ to remove this embed.
Natro
Natro2y ago
But I am already checking if typeof(T) == typeof(int[]) So in that case it should be fine? Okay, thank you very much 🙂
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.