將您想要從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
使那些然後你可以訪問它們。 – Dilshod 2013-04-25 17:55:45
但當我將浮點數,點和列表轉換爲字符串? – user2308516 2013-04-25 18:31:54