c# - Language-Ext- How do i get just the error message from an Error Type from Validation<Error,T> - Stack Overflo

In using LanguageExt, I am trying to display just the error message of a failed validation from using V

In using LanguageExt, I am trying to display just the error message of a failed validation from using Validation<Error,Unit> in LanguageExt.

However, I get the following string:

ValidationData(Fail, 0, [Starting number is not a valid number])

I can't seem to find a way to get just the error message which is

Starting number is not a valid number

Can anyone provide any advice on how to do this?

Here's a snippet of the code

ValidateStartingNumberError(int number)
    .Match(
        () => None,
        error => DisplayError(error.ToString())
    );

private Validation<Error, Unit> ValidateStartingNumberError(int number)
    => int.TryParse(this.number, out var parsedNumber) ?
         unit :
         Fail<Error,Unit>("Starting number is not a valid number");

private void DisplayError(string message)=> MessageBox.Show(message);

In using LanguageExt, I am trying to display just the error message of a failed validation from using Validation<Error,Unit> in LanguageExt.

However, I get the following string:

ValidationData(Fail, 0, [Starting number is not a valid number])

I can't seem to find a way to get just the error message which is

Starting number is not a valid number

Can anyone provide any advice on how to do this?

Here's a snippet of the code

ValidateStartingNumberError(int number)
    .Match(
        () => None,
        error => DisplayError(error.ToString())
    );

private Validation<Error, Unit> ValidateStartingNumberError(int number)
    => int.TryParse(this.number, out var parsedNumber) ?
         unit :
         Fail<Error,Unit>("Starting number is not a valid number");

private void DisplayError(string message)=> MessageBox.Show(message);
Share Improve this question edited Nov 20, 2024 at 11:21 Jeremy Loh asked Nov 20, 2024 at 8:38 Jeremy LohJeremy Loh 1951 silver badge12 bronze badges 3
  • What is DisplayError and what is Fail<Error,Unit>? – shingo Commented Nov 20, 2024 at 9:39
  • Sample code for DisplayError included. Fail<Error,Unit> is a class part of Language-Ext library – Jeremy Loh Commented Nov 20, 2024 at 11:22
  • How can you call a class? Or did you miss the new keyword? – shingo Commented Nov 20, 2024 at 11:31
Add a comment  | 

2 Answers 2

Reset to default 0

Looking at the documentation I think that you need the Message property:

ValidateStartingNumberError(int number)
    .Match(
        () => None,
        error => DisplayError(error.Message)
    );

Found the reason: I implemented the wrong .Match(...). It should be

ValidateStartingNumberError(int number)
    .Match(
        _ => None,
        error => DisplayError(error.Head.Message)
    );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742371349a4431327.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信