我如何获取和如果否则显示类中的消息框
我正在学习 C# Windows 窗体应用程序,我一直在开发一个简单的应用程序,该应用程序提示用户输入他们的姓名,单击按钮,然后显示他们在消息框中输入的内容,并在他们离开时显示错误消息字段空白。我正在使用一个类和方法来处理这个逻辑。 我的第一部分工作正常,但我无法让错误消息工作。我考虑过使用 if / else 因为这是我在其他帖子上看到的,但我无法让它工作。我遇到各种各样的错误,我在课堂上遇到的当前错误是:
错误 1 需要可转换为“字符串”类型的对象
以下是代码:
// Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void btnDone_Click(object sender, EventArgs e)
{
string in_name;
in_name = Console.ReadLine();
Class1 name = new Class1();
MessageBox.Show(name.GetName("Name: " + inputName.Text));
}
}
}
// Class1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
class Class1
{
//Declaring identifiers:
string yourName;
// Methods
public string GetName(string in_name)
{
if (in_name == null)
{
MessageBox.Show("You must enter a name");
return;
}
else
{
return yourName = in_name;
}
}
}
}
I'm learning c# Windows Form App and I've been working on a simple application that is to prompt a user to enter their name, click a button then display what they entered in a messagebox and to display an error message if they leave the field blank. I'm using a class and method to handle this logic.
I got the first part working, but i cannot get the error message to work. I thought about using if / else since that's what i've seen on other posts but I can't get it to work. I get all kinds of errors, the current one i get in the class is:
Error 1 An object of a type convertible to 'string' is required
Here is the code:
// Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void btnDone_Click(object sender, EventArgs e)
{
string in_name;
in_name = Console.ReadLine();
Class1 name = new Class1();
MessageBox.Show(name.GetName("Name: " + inputName.Text));
}
}
}
// Class1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
class Class1
{
//Declaring identifiers:
string yourName;
// Methods
public string GetName(string in_name)
{
if (in_name == null)
{
MessageBox.Show("You must enter a name");
return;
}
else
{
return yourName = in_name;
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须从
GetName
方法返回一些内容。将您的方法更改为此
You must return something from your
GetName
method.Change your method to this