该不该MessageBox的思考

本文探讨了在函数设计中如何处理错误反馈的问题,强调了逻辑处理与界面处理分离的重要性,并提出通过返回值来代替MessageBox提示的方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一个函数,执行过程中遇到问题该不该MessageBox来提示的问题,我认为这涉及的是界面处理与逻辑处理分开的原则问题,

如果该函数是逻辑处理功能的,建议不要进行MessageBox之类的操作,你可以返回不同的值,通过这些值告诉调用者是否出现错误以及具体什么错误

如:

BOOL CImportDataDlg::CheckFreeSpace(CString path)

{

ULARGE_INTEGER AvailableToCaller, Disk, Free;

char root = path.GetAt(0);

CString strRoot;

strRoot.Format("%c://",root);

if (CheckDevice(root))

{

if (GetDiskFreeSpaceEx(strRoot,&AvailableToCaller,&Disk, &Free))

{

CString VideoPath;

// ASSERT(g_DbConnect.IsOpen());

// CAdoRecordSet set;

// set.SetAdoConnection(&g_DbConnect);

//

// CString strSQL;

// strSQL.Format("SELECT [VideoFilePath] FROM VidoAngleData WHERE ([GameID] = %d AND [Angle] = 0)",m_GameID);

//

// BOOL bOk = set.Open(strSQL);

// ASSERT(bOk);

// set.GetCollect("VideoFilePath",VideoPath);

m_VideoData_path_Ctrl.GetWindowText(VideoPath);

ASSERT(!VideoPath.IsEmpty());

 

// if(!DIRECTORYEXISTS_S(VideoPath))

// {

// MessageBox("Video doesn't exist!");

// return FALSE;

// }

ULARGE_INTEGER DirSize;

DirSize.QuadPart = 0;

if(GetDirSize(VideoPath,DirSize))

{

if(DirSize.QuadPart >Free.QuadPart)

{

/*MessageBox("The Disk does not have enough space to save the video, Please choose another Disk!");*/

return FALSE;

}

}

 

}

return TRUE;

}

else

return FALSE;

}

 

这个函数的目的是判断两个空间的大小是否满足要求,不满足给出提示,这个函数应该完全可以复用的,但是我如果在里面加入MessageBox可能在一些不想要这个提示消息的地方也产生这个提示,这是我不希望看到的,所以应该把它一出去,根据返回值来判断

 

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 WindowsFormsApp1 { public partial class Form1: Form { public Form1() { } private void Form1_Load(object sender, EventArgs e) { } private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e) { } private void button1_Click(object sender, EventArgs e) { var repository = new WordRepository(); repository.AddWord(txtWord.Text, txtMeaning.Text, txtExample.Text); MessageBox.Show("单词已成功添加!"); } private void button2_Click(object sender, EventArgs e) { var repository = new WordRepository(); repository.DeleteWord(txtWord.Text); // 假设这里有根据单词删除的方法 MessageBox.Show("单词已成功删除!"); } private void button3_Click(object sender, EventArgs e) { var repository = new WordRepository(); repository.UpdateWord(txtWord.Text, txtMeaning.Text, txtExample.Text); // 假设这里有更新单词的方法 MessageBox.Show("单词已成功更新!"); } private void button4_Click(object sender, EventArgs e) { var repository = new WordRepository(); var word = repository.GetWord(txtWord.Text); // 假设这里有获取单词信息的方法 if (word != null) { txtMeaning.Text = word.Meaning; txtExample.Text = word.Example; } else { MessageBox.Show("未找到该单词!"); } } } } 个人单词簿系统,它包括个人单词簿主界面和随机考查、编辑界面。基本可以实现单词的录入、测试、导出功能。并且可以自己管理词库,具有对词库表中词汇做出适当的添加、删除和修改的功能(不能低于200个单词)。 2.功能要求: (1)单词录入。 (2)单词删除。 (3)单词修改。 (4)单词查询。 (5)单词测试。 给我修改完善代码
最新发布
06-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值