2013-04-25 42 views
1

我使用這個http://aforge.googlecode.com/svn/trunk/Samples/Imaging/ShapeChecker/MainForm.cs代碼,但我需要在變量中寫入值:中心,半徑拐角和標籤。但ProcessImage(Bitmap bitmap)位於需要的class2和class1列表中。寫浮點變量到標籤

如何從Class 2中將這些變量轉換爲Class 1,並將它們轉儲到文本框中?

class class1 
{ 

    AForge.Point center; 
    float radius; 
    List<IntPoint> corners;  

    private void ProcessImage(Bitmap bitmap) 
    { 
    ... 
    } 
} 

class class2 
{ 
??? label1.Text = center + radius + corners... ??? 
} 
+1

使那些然後你可以訪問它們。 – Dilshod 2013-04-25 17:55:45

+0

但當我將浮點數,點和列表轉換爲字符串? – user2308516 2013-04-25 18:31:54

回答

1

將您想要從class1獲得的數據字段標記爲公共。然後,在class2中,基於class1實例化一個對象,並且您將有權訪問這些數據字段。通過應用內置的.ToString()方法來轉換float和Point。您需要遍歷列表,然後調用IntPoint.ToString()方法。

class class1 
{ 

    public AForge.Point center; 
    public float radius; 
    public List<IntPoint> corners;  

    private void ProcessImage(Bitmap bitmap) 
    { 
    ... 
    } 
} 

class class2 
{ 
    class1 myClass1 = new class1(); 
    private void setTextLabel() 
    { 
     label1.Text = myClass1.center.ToString(); 
     label1.Text += myClass1.radius.ToString(); 
     foreach (IntPoint ip in myClass1.corners) 
     { 
      label1.Text += ip.X.ToString(); 
      label1.Text += ", "; 
      label1.Text += ip.Y.ToString(); 
     } 
    } 
} 

完整的工作實現如下。我做了一個Web項目。
的Class1.cs:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using AForge; 

namespace stack_question 
{ 
    public class Class1 
    { 
     public AForge.Point center; 
     public float radius; 
     public List<IntPoint> corners; 

     public Class1() 
     { 
      center = new AForge.Point(3.3F, 4.4F); 
      radius = 5.5F; 
      corners = new List<IntPoint>(); 

      corners.Add(new IntPoint(6, 7)); 
      corners.Add(new IntPoint(8, 9)); 
     } 
    } 
} 

Class2.aspx.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using AForge; 

namespace stack_question 
{ 
    public partial class Class2 : System.Web.UI.Page 
    { 
     Class1 myClass1 = new Class1(); 

     protected void Page_Load(object sender, EventArgs e) 
     { 
      setTextLabel(); 
     } 

     private void setTextLabel() 
     { 

      label1.Text += "Center: " + myClass1.center.ToString() + "<br/>"; 
      label1.Text += "Radius: " + myClass1.radius.ToString() + "<br/>"; 
      foreach (IntPoint ip in myClass1.corners) 
      { 
       label1.Text += "IntPoint: " + ip.X.ToString(); 
       label1.Text += ", "; 
       label1.Text += ip.Y.ToString() + "<br/>"; 
      } 
     } 
    } 
} 

最後,Class2.aspx:在網頁上

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Class2.aspx.cs" Inherits="stack_question.Class2" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <asp:Label ID="label1" runat="server" Text=""></asp:Label> 
    </div> 
    </form> 
</body> 
</html> 

輸出如下所示:

Center: 3.3, 4.4 
Radius: 5.5 
IntPoint: 6, 7 
IntPoint: 8, 9 
+0

不,方法'class1 myClass1作爲新的class1();'不起作用。 我創建瞭如下的實例:'class1 myClass1 = new class1();'但報告錯誤:''AForge.IntPoint'不包含'toString'的定義,也沒有擴展方法'toString'接受第一個參數可以找到類型'AForge.IntPoint'(你是否缺少使用指令或程序集引用?)' – user2308516 2013-04-26 07:47:37

+0

你有什麼想法嗎? – user2308516 2013-04-26 09:19:53

+0

您在實例化行的語法上是正確的。在第二次查看時,使用IntPoint時,必須在X和Y座標上調用.toString,而不是整個IntPoint。我修改了上面的代碼。 – Alex 2013-04-26 11:49:39