I'm trying to make this program work as a glossary where you're supposed to be able to add your words in two languages. When happy with the number of words you've added you press a button and you can test yourself on those words.
In doing this I eventually ended up making a custom class containing the word in both languages and if it had been used in the word-test before. Now I'm at a stop where I try to fill a textbox with the word from the class that is laying within the list and I can't figure out how to get the data out.
I've tried a bunch of stuff but being a newbie I just dig the hole further and further down and I just can't get up...
namespace Uppgift_13._1__Glosprogramet_
{
public partial class Glosprogram : Form
{
List<glosa> glosLista = new List<glosa>();
Random slump = new Random();
glosa glosor = new glosa("", "", false);
int valAvSpråk; //Choice of language
int valAvGlosa; //Choice of glossary
string valAvOrd; //Choice of word (glossary and then what language (which word of that glossary))
public Glosprogram()
{
InitializeComponent();
}
private void btnLäggTillGlosa_Click(object sender, EventArgs e)
{
/*Här läggs ett objekt in i listan enligt en klass jag skapat som
innehåller det svenska ordet, den engelska översättningen och ifall den anänts i glostestet.*/
glosLista.Add( (new glosa(tbxSvensktOrd.Text, tbxEngelsktOrd.Text, false) ) );
btnStartaGlostest.Enabled = true;
}
private void btnStartaGlostest_Click(object sender, EventArgs e)
{
/*Känner vi oss klara med mängden inlagda glosor så kan vi då klicka på starta glostest
för att testa oss på glosorna. Då låses fönstret för att lägga till nya glosor och
ett svenskt eller engelskt ord dyker upp i en av rutorna som man sedan får svara på
i den andra rutan och också det andra språket då.*/
gbxLäggTillGlosor.Enabled = false;
gbxGlostest.Enabled = true;
// 0=Svenska rutan, 1=Engelska rutan
valAvSpråk = slump.Next(0, 2);
valAvGlosa = slump.Next(0, glosLista.Count 1);
//THIS IS WHERE I AM HAVING MY PROBLEMS... CAN'T QUITE FIGURE IT OUT.
if(valAvSpråk == 1) //So if english is the language of choice, it should fill that textbox with the random
{ //glossary in english and then lock it and let the user answer in Swedish. The rest is comparing the answer to the varibale in the class, within the list, again.. Am lost...
tbxEngelsktOrdGlostest.Text = "" glosLista.;
tbxEngelsktOrdGlostest.ReadOnly = true;
}
else
{
}
}
private void btnSvara_Click(object sender, EventArgs e)
{
glosLista.Insert(valAvGlosa, new glosa(glosor.svenskGlosa, glosor.engelskGlosa, true));
tbxEngelsktOrdGlostest.Clear();
tbxSvensktOrdGlostest.Clear();
tbxResultat.AppendText("" "\r\n");
}
private void btnNyaGlosor_Click(object sender, EventArgs e)
{
//Tömma listan på glosor, svenska och engelska, rensa resultatlistan.
gbxLäggTillGlosor.Enabled = true;
gbxGlostest.Enabled = false;
}
}
}
And then my class that I created for thisnamespace Uppgift_13._1__Glosprogramet_
{
class glosa
{
private string privatSvenskGlosa; //Swedish translation of word
private string privatEngelskGlosa; //English translation of word
private bool privatAnväntGlostest = false; //Is used in word-test
public glosa(string msvenskGlosa, string mengelskGlosa, bool manväntGlostest)
{
this.svenskGlosa = msvenskGlosa;
this.engelskGlosa = mengelskGlosa;
this.använtGlostest = manväntGlostest;
}
public string svenskGlosa
{
get
{
return privatSvenskGlosa;
}
set
{
privatSvenskGlosa = value.ToLower();
}
}
/*Samtidigt gör klassen om alla inmatningar till att enbart vara skrivet i små bokstäver
för att underlätta att hitta i indexet senare samt inte få fel för att man skrivit
med stor bokstav i början en gång och sedan skriver med liten bokstav en annan.*/
public string engelskGlosa
{
get
{
return privatEngelskGlosa;
}
set
{
privatEngelskGlosa = value.ToLower();
}
}
public bool använtGlostest
{
get
{
return privatAnväntGlostest;
}
set
{
privatAnväntGlostest = value;
}
}
}
}
CodePudding user response:
seems like you want to choose a random item from a list
you need
// pick the element from the list
var glosEnt = glosLista[valAvGlosa];
if(valAvSpråk == 1)
{
tbxEngelsktOrdGlostest.Text = glosEnt.engelskGlosa;
tbxEngelsktOrdGlostest.ReadOnly = true;
}
CodePudding user response:
I will just answer the title’s question “How to extract variable from a class with multiple variables, within a list?” (I don’t like reading non-English code).
Let’s say you have a list:
var list = new List<MyClass>
{
new MyClass { PropertyA = “A”, PropertyB = “B” },
new MyClass { PropertyA = “1”, PropertyB = “2” }
}
First find the instance (of the class) that you need:
var obj = list.FirstOrDefault(x => x.PropertyA == “1”);
Then get a member’s value:
if (obj != null)
{
var myVariable = obj.PropertyB;
}
If you’re sure your search will find the object, then you can use First
instead of FirstOrDefault
(both are from the namespace System.Linq
) and skip the null-check.
If you need a random element:
var random = new Random(); // random number generator
var randomElement = list[random.Next(list.Count)];
Note about System.Random
:
It is better to make one instance of the Random
class and reuse that instead of using it once and creating a new instance for each random number, because if you instantiate the Random
class, its first value (and the series of numbers that comes next) depends on the current DateTime
(in .NET Framework) and if instantiated within a very small time span, those instances may give the same “random” value. More info: https://docs.microsoft.com/en-us/dotnet/api/system.random?view=net-6.0#instantiating-the-random-number-generator