I'm trying to set 'label1.Text' to the value of an object

reiiign

New member
Joined
Oct 27, 2015
Messages
1
Programming Experience
Beginner
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            LoadVocabulary();
            Generate();
        }

        public class VocabularyWord
        {
            public string Word;
            public string Definition;
            public int Number;
        }

        public void LoadVocabulary()
        {
            int wordcount = 0;

            VocabularyWord Ensconced = new VocabularyWord();
            Ensconced.Word = "Ensconced";
            Ensconced.Definition = "establish or settle in a comfortable, safe, or secret place.";
            Ensconced.Number = 1;
            wordcount = wordcount + 1;

            VocabularyWord Comrades = new VocabularyWord();
            Comrades.Word = "Comrades";
            Comrades.Definition = "a companion who shares one's activities or is a fellow member of an organization.";
            Comrades.Number = 2;
            wordcount = wordcount + 1;
        }

        public void Generate()
        {
            Random Randomize = new Random();
            int RandomNumber = Randomize.Next(1, 2);

            if (RandomNumber == 1)
            {
                label1.Text = Ensconced.Word;
            }
            else
            {
             label1.Text = Comrades.Word;
            }
        }
}

//I think it may be because I'm using 'public' wrong? I have no idea. Please help.
 
Last edited:
You are declaring the Ensconced and Comrades variables inside the LoadVocabulary method so they only exist inside that method. If you want to be able to access them in another method as well then they need to be declared outside both.
 
Back
Top Bottom