user2308516
user2308516

Reputation: 7

Write float variable to label

I use this http://aforge.googlecode.com/svn/trunk/Samples/Imaging/ShapeChecker/MainForm.cs code, but I need to write values ​​in variables: center, radius corners and a label. But ProcessImage(Bitmap bitmap) is located in class2 and class1 listing in need.

How do I get these variables from Class 2 to Class 1, and as they dump into the textbox?

class class1
{

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

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

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

Upvotes: 1

Views: 2101

Answers (1)

Alex
Alex

Reputation: 126

Mark the data fields you want from class1 as public. Then, in class2, instantiate an object based on class1, and you'll have access to those data fields. You convert the float and the Point by applying the built-in .ToString() methods. You'll need to iterate through the list, and then call the IntPoint.ToString() method.

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();
        }
    }
}

A complete, working implementation is found below. I did this as a web project.
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/>";
            }
        }
    }
}

Finally, 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>

Output on the web page looks like the following:

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

Upvotes: 1

Related Questions