2012-01-10 69 views
-2

我還是比較新的c#編程,所以我試圖做一個類方法。 但它不會工作。c#將一個字符串轉換爲2個雙打

那麼我想要做什麼: 我得到了一個字符串,其名稱中有一個var。 x和y數字。 例如字符串將如下所示:「x02y42」

所以我想在我的應用程序(wpf窗口)中有一個函數,我可以將該字符串發送回來,並獲取2個雙精度,如下所示:「double = 02 「和」double = 42「(所以不是字符串)。

所以我做了一個新的類, 這個類中我有此代碼:

public double x,y(string parm) 
{ 
    //input shall be: string s = x12y04; 
    //inp s looks like x12y04 

    //splut the string on the Y (so this tech should also work for x89232y329) 
    //so this will create res[0] that is x89232 and an res[1] that is 329 
    string[] res = parm.Split(new string[] { "y" }, StringSplitOptions.RemoveEmptyEntries); 
    //now for the x in res[0], we replace it for a 0 so it are all numbers 
    string resx = res[0].Replace("x", "0"); 

    //now we will get the strings to doubles so we can really start using them. 
    double x = double.Parse(resx); 
    double y = double.Parse(res[1]); 

    //get the values back 
    return x, y; 

} 

現在(OFC)沒有奏效。但是如上所述,我對c#仍然很陌生。 那麼我在這裏做錯了什麼? 我可以把它放在1個功能嗎? 因此,在我的應用程序中,我可以以某種方式調用它?

+0

這種方法甚至不能編譯... sintax是錯的 – giammin 2012-01-11 00:00:25

回答

1

你將不得不返回一個包含兩個值的數組。例如:

public double[] Convert(string parm) 
{ 
    //input shall be: string s = x12y04; 
    //inp s looks like x12y04 

    //splut the string on the Y (so this tech should also work for x89232y329) 
    //so this will create res[0] that is x89232 and an res[1] that is 329 
    string[] res = parm.Split(new string[] { "y" }, StringSplitOptions.RemoveEmptyEntries); 
    //now for the x in res[0], we replace it for a 0 so it are all numbers 
    string resx = res[0].Replace("x", "0"); 

    //now we will get the strings to doubles so we can really start using them. 
    double x = double.Parse(resx); 
    double y = double.Parse(res[1]); 

    //get the values back 
    return new double[] { x, y }; 

} 
+0

或代替返回一個數組可以返回一個具有兩個屬性的對象或使用兩個輸出參數。 – 2012-01-10 23:21:51

+0

這正是我所期待的。在C#我是個新的程序員仍然需要有時瞭解,在何種情況下東西的作品。但現在我看到了這一點,它也使我在開始時做了什麼。謝謝! – Dante1986 2012-01-11 06:57:16

3

你必須有Python的背景,我可以從你的代碼中推斷出來。

在4.0之前的C#中,不可能以pythonic的方式返回多個值(元組)。您需要返回一個數組或通過out參數返回值。 (除非你定義你的類表示一個元組)。

在C#4.0中,您可以按照註釋中所述返回Tuple<double, double>

+1

這是不正確的。他可以返回Tuple 。 – 2012-01-10 23:13:38

+1

或者他可以返回Tuple'的'實例,如果他是在C#4:'返回Tuple.Create(X,Y);' – 2012-01-10 23:14:02

+2

@MattGreer或者他可以返回一個描述性的,新的類;) – 2012-01-10 23:17:44

3

如果你真的想使用元組,請使用F#而不是C#。在C#中,你將不得不使用一些代表元組的類型;這可能是Point類型或Tuple<,>類型。

變化

public double x,y(string parm) 
... 
return x, y; 

public Point GetXY(string parm) 
... 
return new Point(x, y); 

public Tuple<double, double> GetXY(string parm) 
... 
return Tuple.Create(x, y); 
0

不能從方法返回2對象

你應該返回雙鍵或一個元組的陣列或一個點(我想你正在使用的座標)

反正不是string.split最好是使用的indexOf(「X」)和「y」,然後串,所以你可以檢查所傳遞的字符串,避免indexoutofrange例外

public double[] GetCoordinates(string inputValue) 
    { 
     string normalized = inputValue.ToLower(); 
     int indexX = normalized.IndexOf("x")+1; 
     int indexY = normalized.IndexOf("y")+1; 

     if (indexX == 0 || indexY == 0) 
     { 
      throw new ArgumentException(); 
     } 

     //if string is y45x22 i reverse them 
     if (indexX > indexY) 
     { 
      indexX ^= indexY; 
      indexY ^= indexX; 
      indexX ^= indexY; 
     } 
     string xstring = normalized.Substring(indexX, normalized.Length - indexY); 
     string ystring = normalized.Substring(indexY, normalized.Length - indexY); 

     double[] rtn = new double[2]; 

     if (!double.TryParse(xstring, out rtn[0]) || !double.TryParse(ystring, out rtn[1])) 
     { 
      throw new ArgumentException(); 
     } 
     return rtn; 
    } 
0
class Point 
    { 
     public Point(int x, int y) { this.x = x; this.y = y; } 
     public double x { get; set; } 
     public double y { get; set; } 
    } 

public Point ParseString(string parm) 
{ 
    //input shall be: string s = x12y04; 
    //inp s looks like x12y04 

    //splut the string on the Y (so this tech should also work for x89232y329) 
    //so this will create res[0] that is x89232 and an res[1] that is 329 
    string[] res = parm.Split(new string[] { "y" }, StringSplitOptions.RemoveEmptyEntries); 
    //now for the x in res[0], we replace it for a 0 so it are all numbers 
    string resx = res[0].Replace("x", "0"); 

    //now we will get the strings to doubles so we can really start using them. 
    double x = double.Parse(resx); 
    double y = double.Parse(res[1]); 

    //get the values back 
    return new Point(x,y); 

} 
0

我簡化了碼位:

// C# method names cannot contain character , 
public double[] Parse(string param) 
{ 
    string[] res = param.Split(new string[] { "x", "y" }, StringSplitOptions.RemoveEmptyEntries); 

    double x = double.Parse(res[0]); 
    double y = double.Parse(res[1]); 

    //get the values back as array 
    return new double[] { x, y }; 
} 
0

這是我會做的......

static Tuple<Double, Double> ParseCoordinates(string parm) 
    { 
     string[] res = parm.Replace("x","") 
          .Replace("y",",") 
          .Split(",".ToCharArray()); 
     double x, y; 
     double.TryParse(res[0], out x); 
     double.TryParse(res[1], out y); 

     return new Tuple<double, double>(x, y); 

    } 

,如果你沒有訪問到C#4 ...

static bool ParseCoordinates(string parm, out double x, out double y) 
    { 
     string[] res = parm.Replace("x","") 
          .Replace("y",",") 
          .Split(",".ToCharArray()); 

     bool parseX = double.TryParse(res[0], out x); 
     bool parseY = double.TryParse(res[1], out y); 

     return parseX && parseY; 

    } 
0

您可以創建一個對像類如下─

public class Pair<T, U> { 
     public Pair() { 
     } 

     public Pair(T first, U second) { 
     this.First = first; 
     this.Second = second; 
     } 

    public T First { get; set; } 
    public U Second { get; set; } 
    }; 

,並返回類型爲在您的方法中配對對象並返回 new Pair(x,y);

0

首先,「X,Y」是不是有效的方法名。其次,一種方法只能返回一件事。關鍵是要返回一個代表你的兩個值的對象。像一個System.Drawing.Point()或一個元組()。

這裏的東西應該爲你工作:

 Console.WriteLine(GetXY("x53y2384")); 
0

您可以使用輸出參數,如果你真的想有這種類型的風格:

public Tuple<double, double> GetXY(string parm) 
    { 
     Regex regex = new Regex(@"x(?<x>\d+)y(?<y>\d+)"); 

     Match match = regex.Match(parm); 
     if (!match.Success) 
      return new Tuple<double, double>(0, 0); 

     double x, y; 
     double.TryParse(match.Groups["x"].Value, out x); 
     double.TryParse(match.Groups["y"].Value, out y); 

     return new Tuple<double, double>(x, y); 
    } 

你可以做類似測試。不過,我同意大多數,你的X/Y數據應該返回作爲保持x和y數據的單獨類別的其他海報。

static void Main(string[] args) 
    { 
     double x, y; 

     parse("x12y04", out x, out y); 

     //x and y now contain 12 and 4 
    } 

    static void parse(String parm, out double x, out double y) 
    { 
     string[] res = parm.Split(new string[] { "y" }, StringSplitOptions.RemoveEmptyEntries); 
     string resx = res[0].Replace("x", "0"); 

     x = double.Parse(resx); 
     y = double.Parse(res[1]); 
    }