Home > Net >  I would like to construct a new class using 3 different lists when I call a specific method
I would like to construct a new class using 3 different lists when I call a specific method

Time:02-10

I am making a random weapon generator, I have 3 classes (weapon prefix, weapon base, and weapon suffix)

I can currently pull info from each of the three classes to construct a Console.WriteLine() in the Main() method that displays the various strings and floats attached to the individual classes, in the way that I'd like.

However I would like to create a new class: Class Generated Weapon

That will assemble the variables into a class I can use for other functions. (Damage calculations, display info, etc..)

I'm intentionally leaving the details out, as I'm trying to learn c# and I'd rather be pointed in the right direction, instead of just being given a solution.

If someone could point me in the right direction, or give me a hint as to how to accomplish this, I'd be extremely grateful. Thanks in advance.

CodePudding user response:

define the class

public class GeneratedWeapon{
     public string Froodle {get;set;}
     public int Nargle {get;set;}
     public bool Wingler {get;set;}
}

I have no idea what attributes it has, so I guessed.

Assuming 3 objects exist somehow

WeaponPrefix  wp = ??
WeaponBase wb = ??
WeaponSuffix ws = ??

now you can do

var gen = new GeneratedWeapon{
    Froodle = wp.Froodle,
    Nargle = ws.Nargle * 42,
    Wingler = wb.Wongle > 99
};

or you can have

public class GeneratedWeapon{
     public WeaponPrefix   WeaponPrefix   {get;set;}
     public WeaponBase  WeaponBase  {get;set;}
     public WeaponSuffix  WeaponSuffix  {get;set;}
}

and do

var gen = new GeneratedWeapon{
    WeaponPrefix   = wp,
    WeaponBase  = ws,
    WeaponSuffix  = wb
};

if you just want to create an object that aggregates the three other objects

finally if you just want to lump them together temporaily (to store in a list or pass as a single argument say) you can do

var weaponTuple = (wp,wb,ws);

read up on c# tuples.

  •  Tags:  
  • Related