Reputation: 29
I'm pretty new to coding though I have a fairly okay-ish grasp on some of the basics. One thing I've been really struggling with, however, is using "Random". I'm currently trying to create something that will allow me to generate random elements from a class in my program. How could I tweak the code below to allow for this? I tried creating a class with a few elements that could potentially be generated but this program only allows me to call strings or integers currently.
namespace Random_practice
{
class Program
{
static void Main(string[] args)
{
var e = new Random();
var myList = new List<string> {"tom","paul","jim" };
int count = myList.Count;
int indexValue = e.Next(count);
Console.WriteLine(myList[indexValue]);
Console.ReadLine();
}
}
}
I basically want to be able to place class elements where the names tom, paul, and jim are and have the program randomly pull one up when run. I realize this is probably a pretty nooby question but I'm trying to learn! lol
This is an example of a class I'd like to pull from:
namespace Random_practice
{
public class Encounters
{
public static void Encounter1()
{
Console.WriteLine("You encounter Tom!");
Console.ReadLine();
}
public static void Encounter2()
{
Console.WriteLine("You encounter Paul!");
Console.ReadLine();
}
public static void Encounter3()
{
Console.WriteLine("You encounter Jim!");
Console.ReadLine();
}
}
}
Basically I want to be able to use something like "myEncounters.Encounter1" in my list of elements for the program to choose from in place of just typing out names in string form.
Upvotes: 0
Views: 476
Reputation: 21201
I'm going to take a stab at what I think you're trying to describe. You currently have this:
namespace Random_practice
{
class Program
{
static void Main(string[] args)
{
var e = new Random();
var myList = new List<string> {"tom","paul","jim" };
int count = myList.Count;
int indexValue = e.Next(count);
Console.WriteLine(myList[indexValue]);
Console.ReadLine();
}
}
}
You want to have this:
namespace Random_practice
{
class Program
{
static void Main(string[] args)
{
var e = new Random();
var myList = new List<CLASS_NOT_YET_DEFINED> { obj1, obj2, obj3 };
int count = myList.Count;
int indexValue = e.Next(count);
Console.WriteLine(myList[indexValue]);
Console.ReadLine();
}
}
}
where CLASS_NOT_YET_DEFINED
is a class (a type) we haven't created yet, and obj1
et al are instances of that class.
For this example, I'm going to create a type called "Senator":
public class Senator
{
public string Name { get; set; }
public string ActionTaken { get; set; }
public string Result { get; set; }
public override string ToString()
{
return string.Format("{0} {1}.{2}{3}", Name, ActionTaken, Environment.NewLine, Result);
}
}
So, then your code is like so:
namespace Random_practice
{
class Program
{
static void Main(string[] args)
{
var e = new Random();
var myList = new List<Senator> { };
int count = myList.Count;
int indexValue = e.Next(count);
Console.WriteLine(myList[indexValue]);
Console.ReadLine();
}
}
}
Right now, myList
is empty, so let's populate it with a few objects (each one is an instance of my made-up type, Senator
):
namespace Random_practice
{
class Program
{
static void Main(string[] args)
{
var e = new Random();
var myList = new List<Senator>
{
new Senator
{
Name = "Ruufus",
ActionTaken = "hath embraced thee",
Result = "Thou art loved"
},
new Senator
{
Name = "Septus",
ActionTaken = "hath disdained thee",
Result = "Thou art forsaken"
},
new Senator
{
Name = "Brutus",
ActionTaken = "hath betrayed thee",
Result = "Thou art slain"
}
};
int count = myList.Count;
int indexValue = e.Next(count);
Console.WriteLine(myList[indexValue]);
Console.ReadLine();
}
}
}
Console.WriteLine()
expects to write a string to the console window, so when you do this:
Console.WriteLine(myList[indexValue]);
then the ToString()
method is called on your object. Every type has a default ToString()
method that can be called, but in this case I've overridden (using the override
keyword) that default implementation, using string.Format() to combine the properties of my object into a string suitable for use in the console window.
You can see how this selects a random Senator each time the program is run, here: https://dotnetfiddle.net/tHcGOn
For even more fun**, we can use indexers and the Action type shown in the answer from @Cleptus: https://dotnetfiddle.net/hXvJY8
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
var e = new Random();
var names = new List<string>
{
"Ruufus", "Septus", "Brutus"
};
int count = names.Count;
int name = e.Next(count);
int action = e.Next(count);
var senator = new Senator { Name = names[name] };
Console.WriteLine(senator[action]);
Console.ReadLine();
}
}
public class Senator
{
public string Name
{
get;
set;
}
public string ActionTaken
{
get;
set;
}
public string Result
{
get;
set;
}
public void Embrace()
{
ActionTaken = "hath embraced thee";
Result = "Thou art loved";
}
public void Disdain()
{
ActionTaken = "hath disdained thee";
Result = "Thou art forsaken";
}
public void Betray()
{
ActionTaken = "hath betrayed thee";
Result = "Thou art slain";
}
List<Action> actions;
public Senator()
{
actions = new List<Action>() {
this.Embrace,
this.Disdain,
this.Betray
};
}
public string this[int index]
{
get
{
actions[index]();
return this.ToString();
}
}
public override string ToString()
{
return string.Format("{0} {1}.{2}{3}", Name, ActionTaken, Environment.NewLine, Result);
}
}
**Random
must be a fan of Shakespearean tragedies - Brutus keeps betraying me...
Upvotes: 0
Reputation: 3541
So you need to randomly call a method of a class, then you need to declare a list/array of methods.
Note: Do note all the methods must have the same signature/declaration. And this code will change if the methods should return a value.
If they do not return anything they would be Action and if they return something they would be Func
class Program
{
static void Main(string[] args)
{
var e = new Random();
Encounter myEncounters = new Encounter();
List<Action> actions = new List<Action>() {
myEncounters.Encounter1,
myEncounters.Encounter2,
myEncounters.Encounter3
};
int count = actions.Count;
int indexValue = e.Next(count);
Action randomMethod = actions[indexValue];
//now we call the method
randomMethod();
Console.ReadLine();
}
}
public class Encounter
{
public void Encounter1()
{
Console.WriteLine("You encounter Tom!");
Console.ReadLine();
}
public void Encounter2()
{
Console.WriteLine("You encounter Paul!");
Console.ReadLine();
}
public void Encounter3()
{
Console.WriteLine("You encounter Jim!");
Console.ReadLine();
}
}
Upvotes: 1