gpt4 book ai didi

c# - 如何将类方法的值返回到调用它的相应页面

转载 作者:行者123 更新时间:2023-11-30 00:07:00 24 4
gpt4 key购买 nike

所以我的申请中有两页。我还有一个 MySQL 类,其中包含连接参数和要执行的查询。问题是,我需要两个页面在类中使用相同的方法并将数据返回到触发它的表单。

这种方式对于一个页面来说效果很好,我只是不知道如何在两个或多个页面上使用它。

//MySQL Library
using MySql.Data.MySqlClient;
using MySql.Data;

namespace Masca.users.MySQL
{

public class registrationSQL
{
// The SQL class now knows about the registration page
public users.RegistrationPages.registrationForm Registration;

// The SQL class now knows about the registration Form and datagrid page
public users.RegistrationPages.registrationFormGrid RegistrationFormGrid;

这是我希望全局都可以访问的方法。

    // I guess this part is what I cant figure out
// This is where you set where it's supposed to ruturn the strings right?
// Well What I don't get is how you add another page to return strings to if it calls the method.
public void foward (users.RegistrationPages.registrationForm RegistrationForm)
{
//Declarations
string datasource = "localhost";
string port = "3306";
string username = "root";
string password = "Password";

//Connection
string sqlcon = "datasource = " + datasource + ";" + "port=" + port + ";" + "username=" + username + ";" + "password=" + password + ";";
//Query
string query = "select * from temp.signup where tag = (select min(tag) from temp.signup where tag > '" + RegistrationForm.tag.Text + "' );";

//MySQL declarations
MySqlConnection con = new MySqlConnection(sqlcon);
MySqlCommand cmd = new MySqlCommand(query, con);
MySqlDataReader rdr;

//Excecution
try
{
//If the connection is Open
con.Open();
{
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
//String population
// GetString & GetColumnValueAsString are predefined functions
// They check if a field is null or an integer before returning it as a string
string sid = GetString(rdr, "id");
string smember = GetColumnValueAsString(rdr, "member");
string stag = GetColumnValueAsString(rdr, "tag");

string sfirst = GetString(rdr, "first");
string ssecond = GetString(rdr, "second");
string sthird = GetString(rdr, "third");
string sfourth = GetString(rdr, "surname");

string sdob = rdr.GetString("dob");
string ssex = rdr.GetString("sex");

string susername = GetString(rdr, "username");
string spassword = GetString(rdr, "password");
string ssecurity = GetString(rdr, "question");
string sanswer = GetString(rdr, "answer");

string spemail = GetString(rdr, "pemail");
string swemail = GetString(rdr, "wemail");

string sdoc = rdr.GetString("doc");

string scell = GetString(rdr, "cell");
string shome = GetString(rdr, "home");
string sext = GetColumnValueAsString(rdr, "ext");

string sstreet = GetString(rdr, "street");
string ssurbub = GetString(rdr, "surbub");
string scity = GetString(rdr, "city");
string sregion = GetString(rdr, "region");

string sdept = rdr.GetString("dept");

string sbank = GetString(rdr, "bank");
string sbranch = GetString(rdr, "branch");
string saccount = GetString(rdr, "account");

// Bindings
// This is another thing
// If another page calls this method besides the 'RegistrationForm'
// How am I to direct the strings to that one instead of 'RegistrationForm'

RegistrationForm.id.Text = sid;
RegistrationForm.member.Text = smember;
RegistrationForm.tag.Text = stag;

RegistrationForm.first.Text = sfirst;
RegistrationForm.second.Text = ssecond;
RegistrationForm.third.Text = sthird;
RegistrationForm.surname.Text = sfourth;

RegistrationForm.dob.Text = sdob;
RegistrationForm.sex.Text = ssex;

RegistrationForm.username.Text = susername;
RegistrationForm.password.Text = spassword;
RegistrationForm.question.Text = ssecurity;
RegistrationForm.answer.Text = sanswer;

RegistrationForm.pemail.Text = spemail;
RegistrationForm.wemail.Text = swemail;

RegistrationForm.doc.Text = sdoc;

RegistrationForm.cell.Text = scell;
RegistrationForm.home.Text = shome;
RegistrationForm.ext.Text = sext;

RegistrationForm.street.Text = sstreet;
RegistrationForm.surbub.Text = ssurbub;
RegistrationForm.city.Text = scity;
RegistrationForm.region.Text = sregion;

RegistrationForm.dept.Text = sdept;

RegistrationForm.bank.Text = sbank;
RegistrationForm.branch.Text = sbranch;
RegistrationForm.account.Text = saccount;
}

// Close the connection
con.Close();
}
}

catch (Exception ex)
{
ModernDialog.ShowMessage(ex.Message , "SQL related error", MessageBoxButton.OK);
}
}

这就是我在“registrationForm”中调用此方法的方式

private void next_Click(object sender, RoutedEventArgs e)
{
RegistratonSql.foward(this);
}

这基本上只是允许使用页面中的文本框浏览记录。关于如何解决这个问题有任何线索吗?

最佳答案

如果我理解正确的话,你说你已经有一个访问你的数据的类,所以你的问题一半已经解决了。你真正的问题是因为你在一个类上混淆了你的担忧。在开发过程中,我们的目标是让每个类都有一个单一的目的,而您当前的问题正是其原因。

您只需将数据访问代码拆分到一个类,并准备好数据以在其他类的 UI 中显示。因此,您的 RegistrationPages 类应该只获取数据并具有名称类似于 GetThisDataGetThatData 的方法(显然不完全一样)。

您的其他类都需要此数据访问类的实例来访问其数据,然后将其包装起来以供显示和/或编辑......也许是这样的:

RegistrationPages dataAccess = new RegistrationPages();
SomeCollectionType data = dataAccess.GetThisData();
ObservableCollection<SomeDataType> dataForUi = PrepareDataForUi(data);
SomeUiProperty = dataForUi;

虽然另一个类可以做这样的事情:

RegistrationPages dataAccess = new RegistrationPages();
SomeOtherCollectionType data = dataAccess.GetThatData();
ObservableCollection<SomeOtherDataType> dataForUi = PrepareDataForUi(data);
SomeOtherUiProperty = dataForUi;

关于c# - 如何将类方法的值返回到调用它的相应页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24446397/

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