Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

C# Using a variable from the mainwindow in another class??

Ben

Developer - JS
Developer
Joined
Jul 6, 2013
Messages
1,224
Reaction score
506
For a school project i have to design an exercise on the dutch grammar.

I already worked out most of the program but i am facing 1 problem that i don't know how to fix.

Situation:

I have a class called Grammar and then the mainwindow.

inside class is a method written for the validation between the word filled in in the textbox and the word that it has to be.

Code:
public void valideerwoord(TextBox antwoordTextBlock, Label scrabbleLabel)        {
            if (antwoordTextBlock.Text.Equals(scrabbleLabel.Content))
                {
                    antwoordTextBlock.Background = BrushJuist;                   
                }
                else
                {
                    antwoordTextBlock.Background = BrushFout;
                }
        }

In the mainwindow i've made the list of variables that is required.

Code:
string woord1 = generatorwoord1.kiesRandomWoord();   //            string woord2 = generatorwoord1.kiesRandomWoord();   //
            string woord3 = generatorwoord1.kiesRandomWoord();   //
            string woord4 = generatorwoord1.kiesRandomWoord();   //
            string woord5 = generatorwoord1.kiesRandomWoord();   //   Steekt een woord in de variablen
            string woord6 = generatorwoord1.kiesRandomWoord();   //   
            string woord7 = generatorwoord1.kiesRandomWoord();   //
            string woord8 = generatorwoord1.kiesRandomWoord();   //
            string woord9 = generatorwoord1.kiesRandomWoord();   //
            string woord10 = generatorwoord1.kiesRandomWoord();  //


            string mixedWoord1 = generatorwoord1.MixLetters();   //
            string mixedWoord2 = generatorwoord1.MixLetters();   //
            string mixedWoord3 = generatorwoord1.MixLetters();   //
            string mixedWoord4 = generatorwoord1.MixLetters();   //
            string mixedWoord5 = generatorwoord1.MixLetters();   //   Steekt een gemixed woord in de variabelen
            string mixedWoord6 = generatorwoord1.MixLetters();   //
            string mixedWoord7 = generatorwoord1.MixLetters();   //
            string mixedWoord8 = generatorwoord1.MixLetters();   //
            string mixedWoord9 = generatorwoord1.MixLetters();   //
            string mixedWoord10 = generatorwoord1.MixLetters();  //

Now i want the original word that is made in :

Code:
string woord1 = generatorwoord1.kiesRandomWoord();   //            string woord2 = generatorwoord1.kiesRandomWoord();   //
            string woord3 = generatorwoord1.kiesRandomWoord();   //
            string woord4 = generatorwoord1.kiesRandomWoord();   //
            string woord5 = generatorwoord1.kiesRandomWoord();   //   Steekt een woord in de variablen
            string woord6 = generatorwoord1.kiesRandomWoord();   //   
            string woord7 = generatorwoord1.kiesRandomWoord();   //
            string woord8 = generatorwoord1.kiesRandomWoord();   //
            string woord9 = generatorwoord1.kiesRandomWoord();   //
            string woord10 = generatorwoord1.kiesRandomWoord();  //

To be validated with the word that is filled in into the textbox. The problem is that they are in 2 diffirent classes so they won't recognize eachother.

Should i do it with inheritence from the class with the mainwindow?

I hope i explained it kinda good i'm not sure how to explain it.

ps. Its not allowed to make the variables public in the classes they have to be private.


Greetz
 
Joined
Feb 22, 2012
Messages
2,103
Reaction score
1,271
You have a few choices, read below a few quick examples:

For all examples, Main calls some static class, which calls the Foreign class.

Option 1 - Public and Static Methods

PHP:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WhoAreYou
{
    class Foreign
    {
        public static void YourMethod()
        {
            Console.WriteLine("Hello!");
        }
    }

    class Me
    {
        public static void Test()
        {
            Foreign.YourMethod(); // print "Hello!"
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Me.Test();
        }
    }
}

Basically, on the method, you declare it as static, then you can access anywhere.

Option 2 - Declaring Classes

PHP:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WhoAreYou
{
    class Foreign
    {
        public void YourMethod()
        {
            Console.WriteLine("Hello!");
        }
    }

    class Me
    {
        public static void Test()
        {
            Foreign forN = new Foreign(); // Declares Class
            forN.YourMethod(); // Invoke 'YourMethod' inside Foreign Class...

            // Alternative Method:
            (new Foreign()).YourMethod();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Me.Test();
        }
    }
}

This version, Main calls the class Me, and invokes method Test.
Method Test is static.

On Test's method, it calls the 'Foreign Class', declaring them insight.

You have two choices of using, tho the first one is the right way to use. The second is just an example how it could be called.


Option 3 - Sending Class Definition as an Argument

PHP:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WhoAreYou
{
    class Foreign
    {
        public void YourMethod()
        {
            Console.WriteLine("Hello!");
        }
    }

    class Me
    {
        public static void Test(Foreign forN)
        {
            forN.YourMethod(); // Invoke 'YourMethod' inside Foreign Class, which was declared in other class.
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            // Instead of other versions, we are declaring Foreign HERE.
            Foreign forN = new Foreign();
            Me.Test(forN);

            // or...
            Me.Test((new Foreign()));
        }
    }
}

On this time, The Foreign class is declared on main method, for later then be called from the other class.

-------------------------------------------------------------------

Sorry for it, I'm not very good with teach and stuff, if you want to ask anything about the codes I wrote just question down here, I'm terrible explaining xD
 

Ben

Developer - JS
Developer
Joined
Jul 6, 2013
Messages
1,224
Reaction score
506
You have a few choices, read below a few quick examples:

For all examples, Main calls some static class, which calls the Foreign class.

Option 1 - Public and Static Methods

PHP:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WhoAreYou
{
    class Foreign
    {
        public static void YourMethod()
        {
            Console.WriteLine("Hello!");
        }
    }

    class Me
    {
        public static void Test()
        {
            Foreign.YourMethod(); // print "Hello!"
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Me.Test();
        }
    }
}

Basically, on the method, you declare it as static, then you can access anywhere.

Option 2 - Declaring Classes

PHP:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WhoAreYou
{
    class Foreign
    {
        public void YourMethod()
        {
            Console.WriteLine("Hello!");
        }
    }

    class Me
    {
        public static void Test()
        {
            Foreign forN = new Foreign(); // Declares Class
            forN.YourMethod(); // Invoke 'YourMethod' inside Foreign Class...

            // Alternative Method:
            (new Foreign()).YourMethod();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Me.Test();
        }
    }
}

This version, Main calls the class Me, and invokes method Test.
Method Test is static.

On Test's method, it calls the 'Foreign Class', declaring them insight.

You have two choices of using, tho the first one is the right way to use. The second is just an example how it could be called.


Option 3 - Sending Class Definition as an Argument

PHP:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WhoAreYou
{
    class Foreign
    {
        public void YourMethod()
        {
            Console.WriteLine("Hello!");
        }
    }

    class Me
    {
        public static void Test(Foreign forN)
        {
            forN.YourMethod(); // Invoke 'YourMethod' inside Foreign Class, which was declared in other class.
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            // Instead of other versions, we are declaring Foreign HERE.
            Foreign forN = new Foreign();
            Me.Test(forN);

            // or...
            Me.Test((new Foreign()));
        }
    }
}

On this time, The Foreign class is declared on main method, for later then be called from the other class.

-------------------------------------------------------------------

Sorry for it, I'm not very good with teach and stuff, if you want to ask anything about the codes I wrote just question down here, I'm terrible explaining xD

But if i make them static, i can't use instance variables like Test num = new Test()

----------------------------------------------------------------------------------------------------------------------------------------------------

Like how i got it atm is that it will take the content of the label and will validate if the text on the textbox is the same as the content of the label, if thats true it'll make the textbox green, else red.

In the class taal
Code:
class Taal
{

private SolidColorBrush BrushJuist = new SolidColorBrush(Colors.Green);
 private SolidColorBrush BrushFout = new SolidColorBrush(Colors.Red);


        public void valideerwoord(TextBox antwoordTextBlock, Label scrabbleLabel)
        {
            if (antwoordTextBlock.Text.Equals(scrabbleLabel.Content))
                {
                    antwoordTextBlock.Background = BrushJuist;                   
                }
                else
                {
                    antwoordTextBlock.Background = BrushFout;
                }
        }
}

In the main:

Code:
public partial class MainWindow : Window    {
        private Taal testitem;
        public MainWindow()
        {
            InitializeComponent();


            testitem = new Taal();

Code:
private void controleerButton_Click(object sender, RoutedEventArgs e)
        {




            testitem.valideerwoord(antwoordTextBox1,scrabbleLabel1); //
            testitem.valideerwoord(antwoordTextBox2, scrabbleLabel2); //
            testitem.valideerwoord(antwoordTextBox3, scrabbleLabel3); //
            testitem.valideerwoord(antwoordTextBox4, scrabbleLabel4); //
            testitem.valideerwoord(antwoordTextBox5, scrabbleLabel5); //  controleerd of het ingevoerde woord
            testitem.valideerwoord(antwoordTextBox6, scrabbleLabel6); //  overeenkomt met het gevraagde woord
            testitem.valideerwoord(antwoordTextBox7, scrabbleLabel7); //
            testitem.valideerwoord(antwoordTextBox8, scrabbleLabel8); //
            testitem.valideerwoord(antwoordTextBox9, scrabbleLabel9); //
            testitem.valideerwoord(antwoordTextBox10, scrabbleLabel10);//

See so here it will take the label, i already tried to change the method in the class Taal to take a string instead of label but that didn't work.

As i've got the words read in by another method in the class Taal

Code:
 string woord1 = generatorwoord1.kiesRandomWoord();   //
            string woord2 = generatorwoord1.kiesRandomWoord();   //
            string woord3 = generatorwoord1.kiesRandomWoord();   //
            string woord4 = generatorwoord1.kiesRandomWoord();   //
            string woord5 = generatorwoord1.kiesRandomWoord();   //   Steekt een woord in de variablen
            string woord6 = generatorwoord1.kiesRandomWoord();   //   
            string woord7 = generatorwoord1.kiesRandomWoord();   //
            string woord8 = generatorwoord1.kiesRandomWoord();   //
            string woord9 = generatorwoord1.kiesRandomWoord();   //
            string woord10 = generatorwoord1.kiesRandomWoord();  //

Hope this helps
 
Joined
Feb 22, 2012
Messages
2,103
Reaction score
1,271
But if i make them static, i can't use instance variables like Test num = new Test()

----------------------------------------------------------------------------------------------------------------------------------------------------

Like how i got it atm is that it will take the content of the label and will validate if the text on the textbox is the same as the content of the label, if thats true it'll make the textbox green, else red.

In the class taal
Code:
class Taal
{

private SolidColorBrush BrushJuist = new SolidColorBrush(Colors.Green);
 private SolidColorBrush BrushFout = new SolidColorBrush(Colors.Red);


        public void valideerwoord(TextBox antwoordTextBlock, Label scrabbleLabel)
        {
            if (antwoordTextBlock.Text.Equals(scrabbleLabel.Content))
                {
                    antwoordTextBlock.Background = BrushJuist;                   
                }
                else
                {
                    antwoordTextBlock.Background = BrushFout;
                }
        }
}

In the main:

Code:
public partial class MainWindow : Window    {
        private Taal testitem;
        public MainWindow()
        {
            InitializeComponent();


            testitem = new Taal();

Code:
private void controleerButton_Click(object sender, RoutedEventArgs e)
        {




            testitem.valideerwoord(antwoordTextBox1,scrabbleLabel1); //
            testitem.valideerwoord(antwoordTextBox2, scrabbleLabel2); //
            testitem.valideerwoord(antwoordTextBox3, scrabbleLabel3); //
            testitem.valideerwoord(antwoordTextBox4, scrabbleLabel4); //
            testitem.valideerwoord(antwoordTextBox5, scrabbleLabel5); //  controleerd of het ingevoerde woord
            testitem.valideerwoord(antwoordTextBox6, scrabbleLabel6); //  overeenkomt met het gevraagde woord
            testitem.valideerwoord(antwoordTextBox7, scrabbleLabel7); //
            testitem.valideerwoord(antwoordTextBox8, scrabbleLabel8); //
            testitem.valideerwoord(antwoordTextBox9, scrabbleLabel9); //
            testitem.valideerwoord(antwoordTextBox10, scrabbleLabel10);//

See so here it will take the label, i already tried to change the method in the class Taal to take a string instead of label but that didn't work.

As i've got the words read in by another method in the class Taal

Code:
 string woord1 = generatorwoord1.kiesRandomWoord();   //
            string woord2 = generatorwoord1.kiesRandomWoord();   //
            string woord3 = generatorwoord1.kiesRandomWoord();   //
            string woord4 = generatorwoord1.kiesRandomWoord();   //
            string woord5 = generatorwoord1.kiesRandomWoord();   //   Steekt een woord in de variablen
            string woord6 = generatorwoord1.kiesRandomWoord();   //   
            string woord7 = generatorwoord1.kiesRandomWoord();   //
            string woord8 = generatorwoord1.kiesRandomWoord();   //
            string woord9 = generatorwoord1.kiesRandomWoord();   //
            string woord10 = generatorwoord1.kiesRandomWoord();  //

Hope this helps

So basically you need to get

Code:
 string woord1 = generatorwoord1.kiesRandomWoord();   //
            string woord2 = generatorwoord1.kiesRandomWoord();   //
            string woord3 = generatorwoord1.kiesRandomWoord();   //
            string woord4 = generatorwoord1.kiesRandomWoord();   //
            string woord5 = generatorwoord1.kiesRandomWoord();   //   Steekt een woord in de variablen
            string woord6 = generatorwoord1.kiesRandomWoord();   //   
            string woord7 = generatorwoord1.kiesRandomWoord();   //
            string woord8 = generatorwoord1.kiesRandomWoord();   //
            string woord9 = generatorwoord1.kiesRandomWoord();   //
            string woord10 = generatorwoord1.kiesRandomWoord();  //

from another method?
 

Ben

Developer - JS
Developer
Joined
Jul 6, 2013
Messages
1,224
Reaction score
506
Yea i need to use the variable woord1,woord2 etc in the method valideer() in order to check if the correct word was filled in as the person will see a scrabbled version of that woord1.

Example:

Woord1 = "droppy"
In the label ppl will see: "prodpy"

So the system should take the value inside the textbox and the woord1 variable
 
Joined
Feb 22, 2012
Messages
2,103
Reaction score
1,271
This?:

Code:
class Taal
{
public string woord1, woord2, woord3, woord4, woord5, woord6, woord7, woord8, woord9, woord10;

public void DeclareAll(){
            woord1 = generatorwoord1.kiesRandomWoord();   //
            woord2 = generatorwoord1.kiesRandomWoord();   //
            woord3 = generatorwoord1.kiesRandomWoord();   //
            woord4 = generatorwoord1.kiesRandomWoord();   //
            woord5 = generatorwoord1.kiesRandomWoord();   //   Steekt een woord in de variablen
            woord6 = generatorwoord1.kiesRandomWoord();   //   
            woord7 = generatorwoord1.kiesRandomWoord();   //
            woord8 = generatorwoord1.kiesRandomWoord();   //
            woord9 = generatorwoord1.kiesRandomWoord();   //
            woord10 = generatorwoord1.kiesRandomWoord();  //
}

For getting this on main class:

testitem.woord1, testitem.woord2, etc... Don't forget that you need to invoke DeclareAll.

That means:
add after testitem = new Taal(); (on the main class):

testitem.DeclareAll();


Hardly explained haha, as I said, I'm a bad teacher!
 
Last edited:

Ben

Developer - JS
Developer
Joined
Jul 6, 2013
Messages
1,224
Reaction score
506
This?:

Code:
class Taal
{
public string woord1, woord2, woord3, woord4, woord5, woord6, woord7, woord8, woord9, woord10;

public void DeclareAll(){
            woord1 = generatorwoord1.kiesRandomWoord();   //
            woord2 = generatorwoord1.kiesRandomWoord();   //
            woord3 = generatorwoord1.kiesRandomWoord();   //
            woord4 = generatorwoord1.kiesRandomWoord();   //
            woord5 = generatorwoord1.kiesRandomWoord();   //   Steekt een woord in de variablen
            woord6 = generatorwoord1.kiesRandomWoord();   //   
            woord7 = generatorwoord1.kiesRandomWoord();   //
            woord8 = generatorwoord1.kiesRandomWoord();   //
            woord9 = generatorwoord1.kiesRandomWoord();   //
            woord10 = generatorwoord1.kiesRandomWoord();  //
}

For getting this on main class:

testitem.woord1, testitem.woord2, etc... Don't forget that you need to invoke DeclareAll.

That means:
add after testitem = new Taal(); (on the main class):

testitem.DeclareAll();


Hardly explained haha, as I said, I'm a bad teacher!

Ill try it tomorrow...nap time
 
Back
Top