Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 根据条件返回消息的方法_Vb.net_String_Console Application - Fatal编程技术网

Vb.net 根据条件返回消息的方法

Vb.net 根据条件返回消息的方法,vb.net,string,console-application,Vb.net,String,Console Application,在这种情况下,如果try,catch块中存在异常,则方法应返回两条消息,如果没有异常,则应返回一条成功消息 我在另一个类中调用此方法,并将其结果分配给字符串。我可以连接exception方法并将其作为单个消息发送到此类。但是,我必须再次拆分此消息 有没有办法拆分字符串消息 示例代码: public class sendMessage sub main() method() end sub public function method() as strin

在这种情况下,如果try,catch块中存在异常,则方法应返回两条消息,如果没有异常,则应返回一条成功消息

我在另一个类中调用此方法,并将其结果分配给
字符串。我可以连接exception方法并将其作为单个消息发送到此类。但是,我必须再次拆分此消息

有没有办法拆分字符串消息

示例代码:

public class sendMessage
    sub main()
        method()
    end sub

    public function method() as string
        try
            ------------
            if response isnot nothing
                message= "success"
            end if
       catch
           message="failure"+" "
           message+=ex.tostring()
       end try
       return message
    end function 
end class


public class invokeMessage
    sub main()
        dim ob as new sendMessage
        dim message = ob.method()
        dim messageStatus=? ------------this can be success or failure
        dim messageException ----------- this has exception message in case of failure
    end sub
end class

如果需要从函数返回多个值,则:

1-定义返回类型

public class MyReturnResult
{
    public bool IsException;
    public string Message;
}

public MyReturnResult MyMethod()
{
    try
    {
       ...
    }
    catch (Exception e)
    {
        return new MyReturnResult() {IsException = true, Message = e.ToString()};
    }
    // can be inside try
    return new MyReturnResult() {IsException = false, Message = "Ok"};
}
2-使用著名的GetLastError主题

public string LastMessage;

public bool MyMethod()
{
    try
    {
       ...
    }
    catch (Exception e)
    {
        LastMessage = e.ToString();
        return true;
    }
    LastMessage = "Ok";
    return false; // no exception
}
3-使用
out
参数

// or make bool function and string out parameter
public string MyMethod(out bool isException)
{
    try
    {
       ...
    }
    catch (Exception e)
    {
        isException = true;
        return e.ToString();
    }
    isException = false;
    return "Ok";
}

在您的情况下,您可以定义特殊字符串(例如,
“blablaok”
或带有
null
值)以指示无异常情况,否则它将指示存在异常的内容,并包含与该异常消息不同的内容

public string MyMethod()
{
    try
    {
       ...
    }
    catch (Exception e)
    {
        return e.ToString();
    }
    return "blablablaOk";
}
然后你检查一下

if(MyMethod() == "blablablaOk")
{
    // no exception
}
else
{
}

这是哪种语言,您已经标记了3,并且没有代码阻止您的代码..缺少缩进和命名约定也没有帮助。如果您需要两个reurn 2值,我建议您使用out arguments,除非您专门使用“success”字符串来表示其他内容,为什么不让方法返回
String.Empty
表示成功,返回错误消息表示失败?然后,您可以检查返回值是否==
String.Empty
并进行相应的处理。这样的异常框是一种非常糟糕的做法。基本上可以追溯到15年前,它当时在VB6中的工作方式与此完全相同(有点类似)。问题在于,堆栈跟踪会丢失有价值的信息,因此对此类错误进行故障排除就成了问题。如果读完这篇文章后,你仍然想做这件事——在今天的发展世界里,没有什么能阻止你。
if(MyMethod() == "blablablaOk")
{
    // no exception
}
else
{
}