gpt4 book ai didi

c# - 在多个函数之间使用对象

转载 作者:太空宇宙 更新时间:2023-11-03 23:39:40 25 4
gpt4 key购买 nike

我正在创建一个垃圾邮件检查器。一种方法扫描电子邮件,另一种方法向一组单词和短语添加一个已知标志以进行检查;这两种方法都是 Tester 类的一部分。目前我每个方法都有一个按钮,但是每个事件都会创建自己的垃圾邮件对象。我如何让这两个事件使用同一个对象,从而允许扫描识别我刚刚添加的标志?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HW8_DR
{
public partial class Spam_Scanner : Form
{
public Spam_Scanner()
{
InitializeComponent();
}

private void testButton_Click(object sender, EventArgs e)
{
Tester scan = new Tester();
scan.tester(Convert.ToString(emailBox.Text));
this.SpamRatingBox.Text = string.Format("{0:N1}%", Tester.countSpam / Tester.wordCount * 100);
this.WordsBox.Text = Tester.posSpam;
this.OutputPanal.Visible = true;
this.pictureBox1.Visible = false;
}

private void addButton_Click(object sender, EventArgs e)
{
Tester scan = new Tester();
scan.addSpam(Convert.ToString(addFlagBox.Text));
this.addFlagBox.Text = "";
}
}
}

最佳答案

Tester 变量移动到类字段,如下所示:

public partial class Spam_Scanner : Form
{
Tester scan;

public Spam_Scanner()
{
InitializeComponent();
scan = new Tester();
}

private void testButton_Click(object sender, EventArgs e)
{
scan.tester(Convert.ToString(emailBox.Text));
this.SpamRatingBox.Text = string.Format("{0:N1}%", Tester.countSpam / Tester.wordCount * 100);
this.WordsBox.Text = Tester.posSpam;
this.OutputPanal.Visible = true;
this.pictureBox1.Visible = false;
}

private void addButton_Click(object sender, EventArgs e)
{
scan.addSpam(Convert.ToString(addFlagBox.Text));
this.addFlagBox.Text = "";
}
}

关于c# - 在多个函数之间使用对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29526319/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com