1

for a tool that I am writing I need a "random text" generator. I want to allow the user to be able to choose from premade strings like these:

const string baseCollection = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
const string numbers = "0123456789";
const string specialChars = "°^!\"§$%&/{([)]=}?\\`´*+~'#,;.:-_><|";
const string germanAddition = "ÄÖÜäöü";
const string frenchAddition = "éàèùâêîôûäëïü眫»";
const string russianAddition = " бвгджзклмнпрстфхцчшщйаэыуояеёюиьъ";

Which in terms should be used to run this method.

public string RanText(int length, ???)
{
    string charCollectionString = "";
    foreach(string str in charCollectionStrings) {
        charCollectionString += str;
    }

    //stuff

    return finalString;
}

I have thought of using an Enum but those do not allow Strings. What would be the cleanest way of creating a range of possible arguments?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
HardReset
  • 95
  • 1
  • 9
  • Enums seem to be the answer here. What's your problem with them? Do you want the caller to be able to pass in multiple choices? e.g. `baseCollection` AND `numbers` – Sweeper Feb 24 '20 at 08:48
  • You could write an `if else` block checking which options they chose, and then concatenate a string within each block. `else if(choice4) { masterString += germanAddition }`, and then pass that string to your `RanText`method as your `charCollectionString`. – ThePerplexedOne Feb 24 '20 at 08:50
  • @Sweeper YES! Thats exactly what I want. E.g: User creates: new TestHelper() -> Calls TestHelper.RanText(15, baseCollection, german) for a Random text with both charsets – HardReset Feb 24 '20 at 09:39
  • Check [this](https://stackoverflow.com/questions/8447/what-does-the-flags-enum-attribute-mean-in-c) out. – Sweeper Feb 24 '20 at 09:40
  • I have already seen the possibility of using an Enum with an IfElse or a Switch but wouldn't that be extremely messy over time? Like imagine some day there will be 30+ possible charsets.. There must be a cleaner way (i hope) – HardReset Feb 24 '20 at 09:43

1 Answers1

2

Have you considered using a Dictionary<>? For example:

public enum CType
{
    Base,
    Numbers,
    Special,
    German,
    French,
    Russian
}

public readonly Dictionary<CType, string> Collections = new Dictionary<CType, string>
{
    { CType.Base, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" },
    { CType.Numbers, "0123456789" },
    { CType.Special, "°^!\"§$%&/{([)]=}?\\`´*+~'#,;.:-_><|" },
    { CType.German, "ÄÖÜäöü" },
    { CType.French, "éàèùâêîôûäëïüçœ" },
    { CType.Russian, "бвгджзклмнпрстфхцчшщйаэыуояеёюиьъ" }
};

public string RanText(int length, CType[] parameters)
{
    string charCollectionString = "";
    foreach (CType param in parameters)
    {
        charCollectionString += Collections[param];
    }
}

Then:

RanText(1, new[] { CType.Base, CType.Numbers, CType.Russian });
Innat3
  • 3,561
  • 2
  • 11
  • 29
  • Sorry, but I never used Dictionarys. Can I make one of the Parameters of my RanText method use an entry. Like: public string RanText(int length, params anyEntryFromThisDict) – HardReset Feb 24 '20 at 09:41
  • Dictionaries aren't needed at all here. The `enum` values are sequential ints, starting from `0`. This could easily be a `string[6]` array, or `List`, with the enum value used as the index – Panagiotis Kanavos Feb 24 '20 at 10:40
  • @PanagiotisKanavos true, I'm only using the Dictionary for clarity – Innat3 Feb 24 '20 at 11:01