最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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)
    );

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论