0

The goal is to make a string that represents the fields and they can be separated by any characters but the order of the fields is important.

public class TransactionContext
{
    public string BranchId { get; set; }

    public string AccountId { get; set; }

    public int UserId { get; set; }

    public int CheckNum { get; set; }

    public int NumberOfRecords { get; set; }

    public string StartNextId { get; set; }

    public string StartPreviousId { get; set; }

    public string NextId { get; set; }

    public string PreviousId { get; set; }

    public int RecordCount { get; set; }

    public string ToPageContextString(string separator)
    {
        return BranchId + separator +
               AccountId + separator +
               UserId + separator +
               CheckNum + separator+
               NumberOfRecords + separator +
               StartNextId + separator +
               StartPreviousId + separator +
               NextId + separator +
               PreviousId + separator +
               RecordCount + separator;
    }
}

I tried using StringBuilder for a better solution and I get the following.

    public string ToPageContextString(string separator)
    {
        var contextBuilder = new StringBuilder()
        return contextBuilder.Append(BranchId + separator)
                             .Append(AccountId + separator)
                             .Append(UserId + separator)
                             .Append(CheckNum + separator)
                             ...
                             .ToString();

    }

My last aproach would be using reflection and a custom order attribute like suggested here Get properties in order of declaration using reflection.

Is they a more elegant solution ?

Tiisetso Tjabane
  • 2,088
  • 2
  • 19
  • 24
  • This sounds like an XY problem - perhaps if you explain what you are trying to do and why, then we could suggest a better method. – DavidG May 27 '20 at 23:26
  • "Elegant" as in shorter/cleaner code, or as in more flexible/hands-off if new fields are added? By the way, the point of the `StringBuilder` is to avoid creating intermediate `string`s, which you counteract with `.Append(BranchId + separator)` because that's still creating an intermediate `string` per field. `.Append(BranchId).Append(separator)` would avoid that. – Lance U. Matthews May 27 '20 at 23:27
  • @BACON I am looking for a shorter/cleaner code – Tiisetso Tjabane May 27 '20 at 23:29
  • 2
    How does `string.Join(separator, BranchId, AccountId, UserId, ...)` strike you? Using the [`params` overload of `string.Join()`](https://learn.microsoft.com/dotnet/api/system.string.join?#System_String_Join_System_String_System_String___). – Lance U. Matthews May 27 '20 at 23:31
  • @DavidG I need to create function that creates custom delimiter with the fields in a specific order. I used a class to hold the fields rather creating a function that takes in 10+ fields – Tiisetso Tjabane May 27 '20 at 23:31
  • I realise what you are trying to do here, but this will NOT work because any of those fields could contain the separator string you are using. That's why I asked for the *reason* you are doing this. – DavidG May 27 '20 at 23:33
  • @BACON I like that, its far cleaner than my current soltuion – Tiisetso Tjabane May 27 '20 at 23:33
  • 1
    Use string join : string.Join(",",new string[] {BranchId,AccountId, UserId,CheckNum,NumberOfRecords,StartNextId,StartPreviousId,NextId,PreviousId, RecordCount}); – jdweng May 27 '20 at 23:33

1 Answers1

1

In terms of shortest code I don't think you'll get much shorter than the params overload of string.Join()...

public string ToPageContextString(string separator)
{
    return string.Join(
        separator,
        BranchId,
        AccountId,
        UserId,
        CheckNum,
        NumberOfRecords,
        StartNextId,
        StartPreviousId,
        NextId,
        PreviousId,
        RecordCount
    );
}
Lance U. Matthews
  • 15,725
  • 6
  • 48
  • 68
  • And when any of those values contains the string in `separator` it will all fall apart. – DavidG May 27 '20 at 23:38
  • Data sanitization was not mentioned or requested in the question, though. – Lance U. Matthews May 27 '20 at 23:39
  • 1
    I know, doesn't mean it isn't needed - I'd put money on it being a useful thing to have though. If a dev on my team submitted this code, I'd send it back to be fixed. – DavidG May 27 '20 at 23:40
  • If you'd send back this code then you'd likely send back the original code in the question, too, since they both make the same assumptions about the data they're formatting. That's fine, I guess, but with the small window of code we've been given to look through I don't see anything that shows the author is **clearly** on the wrong path with this approach, so all I can do is answer the question as-asked. To me, if data sanitization were a consideration it would be part of either the original question or a new question, but that's on the author to determine and communicate, not for me to guess. – Lance U. Matthews May 28 '20 at 00:16
  • That's why I asked the OP to clarity and they refused to do so, meaning I will not provide them with an answer, especially one that might give them unintended consequences in the future. Better solutions are far more preferable to quick answers. – DavidG May 28 '20 at 00:19