【问题标题】:object oriented approach from vb6 backgroundvb6背景下的面向对象方法
【发布时间】:2012-06-19 17:35:54
【问题描述】:

我来自 vb6 背景,我正在慢慢测试 c# 水域。我的问题是我很难在我的程序中采用面向对象的方法,因为我倾向于按照我在 vb6 中编码的方式来设计我的程序。以我正在创建的软件的这个数据输入部分为例,正如你所见,我仍然按照我在 vb6 中编码的方式对其进行编码。

namespace WLMS
{
public partial class frmBA : Form
{
    enum status
    {
        add,
        edit,
        delete,
        complete,
        datafill,
    }

    status stat;
    clsSqlCommands sqlCommands = new clsSqlCommands();
    string connectionString = ConfigurationManager.ConnectionStrings["Main"].ConnectionString;
    int dataID = 0;


    public frmBA()
    {
        InitializeComponent();

    }

    private void displayInGrid()
    { 
        DataTable dt = new DataTable();
        dt = sqlCommands.dataFill("select series,baName,baLoc from tblBA order by baName",connectionString);
        if (dt != null)
        {
            dgBA_List.DataSource = dt;
            dgBA_List.Columns[0].HeaderText = null;
            dgBA_List.Columns[1].HeaderText = "BA NAME";
            dgBA_List.Columns[2].HeaderText = "BA LOCATION";
            dgBA_List.Columns[0].Visible = false;
            dgBA_List.Columns[1].Width = 100;
            dgBA_List.Columns[2].Width = 200;
            dataID = 0;        
        }
    }

    private void frmBA_Load(object sender, EventArgs e)
    {
        displayInGrid();
    }

    private void tlADD_Click(object sender, EventArgs e)
    {
        groupBox1.Enabled = true;
        clearTextBoxes(groupBox1);
        txtBAName.Focus();
        stat = status.add;
    }

    private void tlEDIT_Click(object sender, EventArgs e)
    {
        if (dataID != 0)
        {
            groupBox1.Enabled = true;
            stat = status.edit;
        }
        else
            MessageBox.Show("click on item to edit");
    }


    private void tlDELETE_Click(object sender, EventArgs e)
    {
        deleteData();
    }

    private void tlSAVE_Click(object sender, EventArgs e)
    {
        if (checkFilledTextBoxes(groupBox1) == true)
        {
            switch (stat)
            {
                case status.add:
                {
                    addNewData();
                    break;
                }
                case status.edit:
                {
                    editData();
                    break;
                }
                default:
                {
                    break;
                }
            }
        }

    }

    private bool checkForDuplicates()
    {
        DataRow dtr = sqlCommands.getOneRow("select count(*) as cnt from tblba where baName = '" + txtBAName.Text + "' and baLoc = '" + txtBALoc.Text + "'", connectionString);
        if (Convert.ToInt16(dtr["cnt"]) < 1)
        {
            return false;
        }
        else
            return true;
    }

    private void editData()
    {
        if (!checkForDuplicates())
        {
            sqlCommands.dataManipulate("update tblBa set baName = '" + txtBAName.Text + "',baLoc =  '" + txtBALoc.Text + "' where series = " + dataID + "", connectionString);
            clearTextBoxes(groupBox1);
            groupBox1.Enabled = false;
            stat = status.complete;
            displayInGrid();
            MessageBox.Show("Record Edited");
        }
        else
            MessageBox.Show("Duplicate record");
    }

    private void deleteData()
    {
        DialogResult dialogResult = MessageBox.Show("Are you sure?", "", MessageBoxButtons.YesNo);
        if (dialogResult == DialogResult.Yes)
        {
            sqlCommands.dataManipulate("delete from tblBa where series = " + dataID + "", connectionString);
            clearTextBoxes(groupBox1);
            groupBox1.Enabled = false;
            stat = status.complete;
            displayInGrid();
            MessageBox.Show("Record deleted");
        }
        else
            MessageBox.Show("Duplicate record");
    }

    private void addNewData()
    {
        if (!checkForDuplicates())
        {
            sqlCommands.dataManipulate("insert into tblBa (baName,baLoc) values ('" + txtBAName.Text + "','" + txtBALoc.Text + "')", connectionString);
            clearTextBoxes(groupBox1);
            txtBAName.Focus();
            stat = status.complete;
            displayInGrid();

            MessageBox.Show("Record Added");
        }
        else
            MessageBox.Show("Duplicate record");
    }

    private void clearTextBoxes(GroupBox gprx)
    {
        foreach (TextBox txtBx in gprx.Controls.OfType<TextBox>())
        {
            txtBx.Text = "";
        }
    }

    private Boolean checkFilledTextBoxes(GroupBox gprx)
    {
        foreach (TextBox txtBx in gprx.Controls.OfType<TextBox>())
        {
            if (txtBx.Text == "")
                return false;
        }
        return true;
    }

    private void tlEXIT_Click(object sender, EventArgs e)
    {
        this.Dispose();
    }

    private void dgBA_List_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        int rowIndex = e.RowIndex;
        DataGridViewRow row = dgBA_List.Rows[rowIndex];
        dataID = Convert.ToInt16(row.Cells[0].Value);
        txtBALoc.Text = row.Cells[1].Value.ToString();
        txtBAName.Text = row.Cells[2].Value.ToString();
        groupBox1.Enabled = false;
    }
}
}

它有两个文本框,位于一个组框内,一个 datagridview 和 5 个工具条按钮,用于添加、编辑、删除、保存和退出。我的问题是,如何重组我的代码以采用面向对象的方法?

请帮忙..谢谢

【问题讨论】:

  • 你所说的“面向对象的方法”到底是什么意思?您是否觉得您需要以某种方式将您的代码转换成一些内部散发着光芒的美丽抽象对象,因为在某种程度上,您觉得您编写的代码并不能安抚面向对象的上帝?我看你上面写的没有错。尽管有些人可能会说,VB6 一种面向对象的语言。它只是不进行类继承。嗯,这太糟糕了。我见过很多糟糕的代码,人们编写了乏味的继承层次结构——只是因为他们可以。你想达到什么目的?
  • @MarkBertenshaw 尝试学习不同的方法永远不会有坏处。为什么要吐槽?为什么是攻击性的语气?以上是一个合理的问题。
  • @DennisTraub 感谢您的支持。实际上这是我在 stackoverflow 上的第一篇文章,我担心我的头会因为向新手提问而立即被置于危险境地。 :)
  • @MarkBertenshaw,如果我的问题有点含糊,请原谅我,但您的假设是正确的。这正是我的感受。 :) 恐怕我没有按照应该完成的方式进行编码,并且我不符合 OO 方法。实际上我没有受过正规的编程教育。我刚在家里练习并搜索了诸如stackoverflow之类的精彩论坛。
  • @DenmisTraub - 同意 - 一点都不疼。咆哮? - 不。我只是说那里的代码完全没问题,Overmind 不必觉得他做错了什么。我观察到一些程序员似乎认为 OO 的目的是进行漂亮的抽象,而不是完成任务。 VB6 让事情顺利完成。 Overmind - 对你有好处!我以同样的方式进入编程领域,并因此拥有了一个有趣的职业生涯。你能谦虚地提出这样的问题真是太棒了。请记住,有些答案有些主观:-)

标签: c# vb6


【解决方案1】:

当您使用 GUI 时,我建议您阅读一下 MVC 模式 (Winforms) Looking for clean WinForms MVC tutorial for C# 或 MVVM 模式 (WPF) MVVM: Tutorial from start to finish?

我还建议阅读一本不错的设计模式书。我在第一次开始时发现非常有用的是 Head First Design Patterns 这本书。很容易跟上。他们使用的语言是 Java,但这在语法上非常接近 C#。

也许还可以阅读 Martin Fowler 这篇关于 GUI 架构的优秀文章:http://martinfowler.com/eaaDev/uiArchs.html

编辑:继丹尼斯的评论之后。您还可以查看用于 Winform 开发的 MVP 模式。实现该模式的几种不同方式:http://martinfowler.com/eaaDev/SupervisingPresenter.htmlhttp://martinfowler.com/eaaDev/PassiveScreen.html

【讨论】:

  • 在 Windows.Forms 应用程序中,我更喜欢 MVP 而不是 MVC。
  • @Dennis - 我从未使用过 MVP,因此无法发表评论。我知道 Martin Fowler 对此有一些很好的文章。我会用这些链接更新我的答案。
【解决方案2】:

简单的答案 - 你不能。

取决于技术。

  • ASP.NET - 使用MVC,那就顺其自然了。
  • WPF:好吧,UI 无论如何都在 XAML 中。 Winforms:你不能,控制是在设计器中完成的,数据绑定不太好。

你可以在 WInForms 中做的最好的事情实际上不是将逻辑保存在表单中,而是保存在另一个对象中,但是表单是按照设计者想要的方式完成的,或者你失去了所有兼容性。

【讨论】:

  • 坏了。这与模式无关,它与 (a) Windows 设计人员的工作方式和 (b) 对数据绑定的有限支持(已在 WPF 中修复)有关。你可以做很多事情,但没有一个是值得失去设计师支持的。遗憾的是,这就是 winforms 目前所在的位置。 WPF 让事情变得更好。
猜你喜欢
  • 2014-08-24
  • 1970-01-01
  • 2010-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-29
  • 2011-04-29
相关资源
最近更新 更多