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... ???
}
4

1 回答 1

1

将您想要从 class1 中获取的数据字段标记为公开。然后,在 class2 中,基于 class1 实例化一个对象,您将可以访问这些数据字段。您可以通过应用内置的 .ToString() 方法来转换浮点数和点数。您需要遍历列表,然后调用 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();
        }
    }
}

下面找到了一个完整的工作实现。我将其作为一个网络项目进行。
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
于 2013-04-25T18:57:42.067 回答