Accessing method in a class from MainWindow

Koosshh56

I'm trying to create a WPF application for students' registration with a simple form containing Name, matriculation number, credits and so on. I have a Mainwindow.xaml and a Student.cs.

In my MainWindow.xaml I have an advance button that should advance the level of the students, based on the credits (if the student has more than 120 credits, level should be advanced to "2")

This is the Student.cs with Advance() method

class Student
{
    private int matric;                            
    private int level;                  
    private int credits;                

    public Student() { }       

    public int Matric                   
    {
        get { return matric; }          
        set                            
        {
           //there should be a range check for the 
            matric = value;
        }
    }

    public int Level
    {
        get { return level; }
        set { level = value; }
    }

    public int Credits
    {
        get { return credits; }
        set { credits = value; }
    }

    //this is my problem:

    public int Advance() 
    {
        if (Credits >= 0 && Credits < 120)
        {
            return Level;
        }
        else if (credits >= 120)
        {
            return Level++;
        }
        else if (credits >= 240)
        {
            return Level++;
        }
        else if (Credits >= 360)
        {
            return Level++;
        }
        else
        {
            return 0;
        }
    }
}       

MainWindow.xaml, just the part with the button and the textboxes

<TextBox x:Name="txtLevel" HorizontalAlignment="Left" Height="23" Margin="184,266,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
    <TextBox x:Name="txtCredit" HorizontalAlignment="Left" Height="23" Margin="184,328,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
    <Button x:Name="btnAdvance" Content="Advance" HorizontalAlignment="Left" Margin="324,267,0,0" VerticalAlignment="Top" Width="75" Click="btnAdvance_Click"/>

And where I'm trying to call the method MainWindow.xaml.cs

public partial class MainWindow : Window
{
    Student student;
    public MainWindow()
    {
        InitializeComponent();
    }

    private void btnSet_Click(object sender, RoutedEventArgs e)
    {
        student = new Student();            
        student.Credits = int.Parse(txtCredit.Text);
        student.Level = int.Parse(txtLevel.Text);

    }

    private void btnAdvance_Click(object sender, RoutedEventArgs e)
    {
        student.Advance();              //this should be the call of the method
    }
}

Of course it's not working... Can anyone help me?

EDIT This is what I have now, still not working

    public void Advance()
    {
        if (Credits >= 0 && Credits < 120)
        {
            Level = 1;
        }
        else if (credits >= 120 && Level == 1)
        {
            Level = 2;
        }
        else if (credits >= 240 && Level == 2)
        {
            Level = 3;
        }
        else if (Credits >= 360 && Level == 3)
        {
            Level = 4;
        }
        else if (Level == 4)
        {
            MessageBox.Show("You can't advance more!");
        }
        else
        {
            MessageBox.Show("Advance is not possible!");
        }
    }
Bobby

Really you should be doing this with binding, where you bind the Level textbox to the Level property of the student, implementing iNotifyPropertyChanged on your model. I would suggest you research binding and redesign it this way.

However, if you wish to proceed in your current design, here are changes I would suggest to achieve the behavior you are expecting:

1) In btnSet_Click, remove this line: student.Level = int.Parse(txtLevel.Text); Your Level should not be set by the TextBox; it should be set by the Advance method.

2) Your Advance method should look like follows:

public int Advance()
    {
        if (Credits >= 0 && Credits < 120)
        {
            level = 1;
        }
        else if (credits >= 120 && credits < 240)
        {
            level = 2;
        }
        else if (credits >= 240 && credits < 360)
        {
            level = 3;
        }
        else if (Credits >= 360)
        {
            if (level != 4)
            {
                level = 4;
            }
            else 
            {
                MessageBox.Show("You can't advance more!");
            }
        }
        else
        {
            MessageBox.Show("Advance is not possible!");
        }

        return level;
    }

3) Add the IsReadOnly="True" attribute to the Level Textbox, as it should not be settable from the interface.

<TextBox x:Name="txtLevel" IsReadOnly="True" HorizontalAlignment="Left" Height="23" Margin="184,266,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>

4) Since you are not using binding, in your Advance_click, you will need to post the returned value back to the interface:

private void btnAdvance_Click(object sender, RoutedEventArgs e)
{
    txtLevel.Text = student.Advance();              //this should be the call of the method        
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling MainWindow method from a UserControl class

From Dev

Accessing instance method from class method

From Dev

Accessing class variables from ActionListener method

From Dev

accessing instance methods from class method proc

From Dev

Accessing class variables from ActionListener method

From Dev

Accessing a public method from a class inside a SurfaceListBox

From Dev

Getting Error in Accessing Method from another class

From Dev

Accessing a method from an inheritance class at the main

From Dev

Accessing a get method in base class from a child class not returning value

From Dev

Restricting child class from accessing parent class method

From Dev

Accessing an extended class method from a sibling extended class (PHP OOP)

From Dev

Change image in MainWindow from class

From Dev

Accessing instance variables from another class from a static method

From Dev

Accessing a class method from within an Active Record scope?

From Dev

JMH: accessing BenchmarkParams from @Setup method of @State class

From Dev

Accessing private variables from a super class using an overridden abstract method

From Dev

Accessing variable from another method within the same class

From Dev

Python: Import errors. Accessing method from other class

From Dev

Accessing method from another class in Cocos2D

From Dev

Accessing a class method from within an Active Record scope?

From Dev

Accessing variable from another method within the same class

From Dev

JMH: accessing BenchmarkParams from @Setup method of @State class

From Dev

C++ difficulties accessing method from a deep inner class

From Dev

Accessing NSIndexPath from method

From Dev

Accessing a method from another method in the same class which is passed as props to another class

From Dev

Accessing SQLalchemy from a class

From Dev

Accessing function from class

From Dev

Printing to an element of MainWindow from an outside class

From Dev

Log BackgroundWorker activity in a different class from the MainWindow

Related Related

  1. 1

    Calling MainWindow method from a UserControl class

  2. 2

    Accessing instance method from class method

  3. 3

    Accessing class variables from ActionListener method

  4. 4

    accessing instance methods from class method proc

  5. 5

    Accessing class variables from ActionListener method

  6. 6

    Accessing a public method from a class inside a SurfaceListBox

  7. 7

    Getting Error in Accessing Method from another class

  8. 8

    Accessing a method from an inheritance class at the main

  9. 9

    Accessing a get method in base class from a child class not returning value

  10. 10

    Restricting child class from accessing parent class method

  11. 11

    Accessing an extended class method from a sibling extended class (PHP OOP)

  12. 12

    Change image in MainWindow from class

  13. 13

    Accessing instance variables from another class from a static method

  14. 14

    Accessing a class method from within an Active Record scope?

  15. 15

    JMH: accessing BenchmarkParams from @Setup method of @State class

  16. 16

    Accessing private variables from a super class using an overridden abstract method

  17. 17

    Accessing variable from another method within the same class

  18. 18

    Python: Import errors. Accessing method from other class

  19. 19

    Accessing method from another class in Cocos2D

  20. 20

    Accessing a class method from within an Active Record scope?

  21. 21

    Accessing variable from another method within the same class

  22. 22

    JMH: accessing BenchmarkParams from @Setup method of @State class

  23. 23

    C++ difficulties accessing method from a deep inner class

  24. 24

    Accessing NSIndexPath from method

  25. 25

    Accessing a method from another method in the same class which is passed as props to another class

  26. 26

    Accessing SQLalchemy from a class

  27. 27

    Accessing function from class

  28. 28

    Printing to an element of MainWindow from an outside class

  29. 29

    Log BackgroundWorker activity in a different class from the MainWindow

HotTag

Archive