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
.