如何在visual basic 2010中指定这些异常 [英] How to specify these exception in visual basic 2010

查看:64
本文介绍了如何在visual basic 2010中指定这些异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在第一个文本框中输入4个文本框用于输入名称,2,3,4用于输入日期格式DD-MM-YYYY。如果用户在该框中输入Null或Numbers,我想为name指定一个例外。处理日期文本框中输入的字符以及长度的例外,这意味着如果用户输入的数字不是正确的格式。请帮我指定那些例外情况。圣诞快乐



I made 4 Text boxes in that first text box is for entering Name and 2,3,4 are for entering Date in format DD-MM-YYYY. i want to specify an exception for name if user inputs Null or Numbers in that box. And an exception to handle characters input in date text boxes and also length, that means if user inputs numbers not in the correct format.please help me how to specify those exceptions.Merry Christmas

Dim i As Integer = 0
       Dim l As Integer = 0
       Dim Nm As String

       Nm = (TextBox3.Text).ToString
       Dim lcn As String = ""
       If String.IsNullOrEmpty(textBox4.Text) AndAlso String.IsNullOrEmpty(textBox5.Text) AndAlso String.IsNullOrEmpty(textBox6.Text) Then
           MessageBox.Show("Please Enter Name & Date of Birth!!!")
           Return
       End If

推荐答案

为什么要将所有内容都放在一段不可读的代码中?



首先,只需使用DateTimePicker控件作为日期。这使您和应用程序的用户更容易。



其次,只需创建一个函数来检查值是否正确。您还可以添加一个简单的函数,根据条件显示消息。就像assert一样。

Why stuff everything in one piece of unreadable code?

First of all, simply use a DateTimePicker control for dates. It makes it a lot easier for you and the users of the application.

Second, just make a function for checking if the value is correct. You can also add a simple function that shows a message based on a condition. Just somewhat like assert would.
Function ShowErrorIf(isValid as boolean, ShowThisErrorMessage as string)
 if not isValid then 
    MessageBox.Show(ShowThisErrorMessage)
 end if
End function




...
if ShowErrorIf(String.IsNullOrEmpty(txtBirthdate.Text), "Please enter a valid birthdate") and
   ShowErrorIf(String.IsNullOrEmpty(txtName.Text), "Please enter a valid name") and
   ... etc...





祝你好运!



Good luck!


如果绝对需要使用文本框输入日期然后去找它,否则我建议日期只使用DateTimePicker控件和用户输入的任何值,在按钮事件处理程序方法或任何事件处理程序方法中,您将使用日期来检索它:



If it is absolutely required to use textbox to enter date then go for it, but otherwise I''d suggest for date just use DateTimePicker control and whatever value the user has to input, in a button event handler method or any event handler method where you will use the date u can retrieve it by:

DateTime birthday = dateTimePicker1.Value;





这样您就不必担心用户输入日期的例外情况或格式。



并且为了处理文本框中的异常,最好制作另一种方法,将输入文本作为字符串参数传递,并制作一些if-else语句来检查它们并强行抛出例外,例如,如果这是您的按钮单击事件处理程序方法:





This way you don''t have to worry about exceptions or formats the user enters the date.

And for handling exceptions in the textbox, its best to make another method where you pass the input text as a string parameter and make some if-else statements to check them and forcefully throw an exception, for example if this is your button click event handler method:

public void Button1_Click(Object sender, EventArgs e)
{
    try
    {
        //This is the string input from the textbox
        string inputText = textBox1.Text;

        //This is the datetime input from the DateTimePicker controls
        DateTime date1 = dateTimePicker1.Value;
        DateTime date1 = dateTimePicker2.Value;
        DateTime date1 = dateTimePicker3.Value;

        //Then call the method where you are checking for the exceptions
        CheckExceptionsMethod(inputText);
    }
    catch(Exception ex) //If any custom exception is thrown it will go to the catch method
    {
        MessageBox.Show(ex.Message); //This will display the custom exception message
    }
}

public void CheckExceptionsMethod(string inputText)
{
    if(inputText.Equals("") || inputText.Text == null) //double checking if there is null value
    {
        //Throwing custom exception type of NullEntryException
        throw(new NullEntryException("Please enter a value in the textbox.", null));
    }
    else if(inputText.Contains("0123456789")) //This will be true if any number exists in text
    {
        //Throwing custom exception type of NumberEntryException
        throw(new NumberEntryException("Please do not enter numbers in the textbox.", null));
    }
}





然后,最后一项任务是为您创建两个自定义类,用于您拥有的例外情况抛出,因为它们并不存在我为你制造它们。您可以通过继承ApplicationException类来创建任何自定义异常类。因此,创建两个新类如下,非常短的编码,所以不用担心。只需继承ApplicationException类并创建一个构造函数,因为当我抛出异常时,我正在初始化这些类的对象。





Then the last task is for you to create two custom classes for the exceptions you have thrown, because they do not really exist I made them up for you. You can make any custom exception class by inheriting the ApplicationException class. So create two new classes as following, very short coding so no worries. Just inherit the ApplicationException class and make a constructor because when I am throwing the exception I was initializing an object of these classes.

public class NullEntryException : ApplicationException
{
    public NullEntryException(string Message, Exception Inner): base(Message, Inner)
    {
        //No need to write any code, the base class is called so it will take care of it
    }
}

public class NumberEntryException : ApplicationException
{
    public NumberEntryException(string Message, Exception Inner): base(Message, Inner)
    {
        //No need to write any code, the base class is called so it will take care of it
    }
}


这篇关于如何在visual basic 2010中指定这些异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆