Complex numbers class from Numerics does not have any Parse method
How to write Parse methods
Is it good idea to place them into separate class as extension method
This is my attempt but I am not sure that it is correct
public static class Extension
{
public static bool TryParse(this Complex c,String s, out Complex z)
{
double re,im;
bool b1,b2;
s.Trim();
if(!s.EndsWith("i") && !s.EndsWith("j"))
{
b1= double.TryParse(s,out re);
z = new Complex(re,0);
return b1;
}
int index = s.IndexOf('+',1);
if(index < 0)
index = s.IndexOf('-',1);
s = s.Substring(0,s.Length-1);
if(s.Substring(index+1) == "")
s+="1";
if(index < 0)
{
b2 = double.TryParse(s,out im);
z = new Complex(0,im);
return b2;
}
b1 = double.TryParse(s.Substring(0,index),out re);
b2 = double.TryParse(s.Substring(index),out im);
z = new Complex(re,im);
return b1 && b2;
}
}
I am not sure that it is correct
I have no idea how I can parse complex number in the format z = (a,b)
where a and b are doubles
There is another Parse method
public static Complex Parse(string s)
Maybe it is good idea to write to Microsoft with request
to update Complex class from Numerics