1

Is there any way to avoid repetition where you are declaring multiple instances of the same type that have something in between that varies? declaring the fixed statements only once, then declaring the inner variables inside them multiple times without using any methods

  • Using methods is straightforward, I am trying to achieve this only in class declaration.

Supposing we have to declare multiple Strings like this:

String A = "You found a" + "Common" + "card";
String B = "You found a" + "Rare" + "card";
String C = "You found a" + "Legendary" + "card";

So what I want to have is declaring the "You found a" and "card" only once and declaring A, B and C only with their inner variable character (while, of course, having the repetitive statements AND not using any methods).

I wrote the above code as a simpler instance that may help me achieve the solution in an array like this:

AudioClip[] audioClips = 
{
    (AudioClip)Resources.Load("Audios/audio1.mp3", typeof(AudioClip)),
    (AudioClip)Resources.Load("Audios/audio2.mp3", typeof(AudioClip)),
    (AudioClip)Resources.Load("Audios/audio3.mp3", typeof(AudioClip)),
};

// This is Unity by the way, just a sample

As you can see we are having array inputs with one line of code that have only one variable character inside their Strings.

So is there any way to have something like this:

//Imaginary code:

String A = /* You found a */ "common" /* card */'
String B = /* You found a */ "Rare" /* card */;
String C = /* You found a */ "Legendary" /* card */;

and in the second:

AudioClip[] audioClips = 
{
    /* (AudioClip)Resources.Load("Audios/audio */ 1 /* .mp3", typeof(AudioClip)) */,
    /* (AudioClip)Resources.Load("Audios/audio */ 2 /* .mp3", typeof(AudioClip)) */,
    /* (AudioClip)Resources.Load("Audios/audio */ 1 /* .mp3", typeof(AudioClip)) */,
};

// The commented statements with /*, */ are what need to be there 
// without being written multiple times in each.

So is there a solution? I said not using any methods but if it's only achievable through methods how to do so in the AudioClip array.

Sorena
  • 41
  • 5
  • 2
    `So what I want to have is declaring the "You found a" and "card" only once` Google up what `const` does. – mjwills Mar 18 '19 at 20:43
  • 3
    Use `const string FoundCardType = "You found a {0} card";` Then you can use it like: `var cardType = "common"; var message = string.Format(FoundCardType, cardType);` – Rufus L Mar 18 '19 at 20:45
  • Why not using methods? C# does not have macros like C++ and other languages have. – D Stanley Mar 18 '19 at 20:48
  • 1
    putting any type of restriction on the answer like *without using any methods* can actually **hinder** performance. You do not want to try to outsmart the compiler (c# nor the JIT), they are extremely smart and not online in-lining functions but also removing complete code blocks if deemed it's never used (yes the JIT will do this). – Erik Philips Mar 18 '19 at 20:52

2 Answers2

4

You can use a const string with placeholders for the values you want to replace, then use string.Format to do the replacement when you need it, for example:

const string FoundCardMessage = "You found a {0} card";

private static void Main(string[] args)
{
    var cardTypes = new List<string> {"Common", "Rare", "Legendary"};

    foreach (var cardType in cardTypes)
    {
        var message = string.Format(FoundCardMessage, cardType);
        Console.WriteLine(message);
    }
}

Output

enter image description here

Rufus L
  • 36,127
  • 5
  • 30
  • 43
3

Does this count as "using methods"?

AudioClip[] audioClips = 
    Enumerable.Range(1,3)
              .Select( i => (AudioClip)Resources.Load("Audios/audio" + i + ".mp3", typeof(AudioClip)))
              .ToArray();

You can make it a little more fancy by using interpolation, formatting, whatever, but that's the gist.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • By not using methods I meant not putting the declaration in `void Main` or any other method, this was exactly what I was looking for. Thanks – Sorena Mar 18 '19 at 22:05
  • Could I have the `i` as a `string` and not a numeric value? – Sorena Mar 18 '19 at 22:20
  • @Sorena Well I suppose you could use `(new [] {"1", "2", "3"}).Select(...` instead. What's wrong with using numbers? – D Stanley Mar 19 '19 at 13:14
  • Thanks. In case that the variable isn't number, (like `audioOne, audioTwo ...`) – Sorena Mar 19 '19 at 20:57