1

I have a Asp.net application in Visual studios in my model I have this class

public class Goalkeeper
{     
    public static string Name { get; set; }
    public static string Position { get; set; }
    public static int Matches { get; set; }
    public static int Cleansheets { get; set; }        
}

Is there a way for me to set the values of these in the model so I can use thme in all my different views and controller actions so I dont have to set them like this for example (Goalkeeper.Matches = 234;) In every single action in my controller, because that seems very inefficient.

1
  • public int Matches {get{return this.Matches;} set{this.Matches= 234;}} Commented Sep 30, 2016 at 6:56

2 Answers 2

4

You can either:

Add a constructor to your Model, where you set the initial values:

public class Goalkeeper
{
    public Goalkeeper() {
        Position = "Goalkeeper";
        Matches = 5;
        Cleansheets = 0;
    }

    public static string Name { get; set; }
    public static string Position { get; set; }
    public static int Matches { get; set; }
    public static int Cleansheets { get; set; }
}

Or, initialize the properties directly:

public class Goalkeeper
{ 
    public static string Name { get; set; }
    public static string Position { get; set; } = "Goalkeeper";
    public static int Matches { get; set; } = 5;
    public static int Cleansheets { get; set; } = 0;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hello. Wondering how to Initialize the Goalkeeper class with multiple registers.
@LuisAlbertoDelgadodelaFlo you can create multiple constructors. For example public Goalkeeper(string name, string position) { ... }
4

Depending on the version of C# you are using you can initialize the property like this:

public static string Name { get; set; } = "Whatever";

This only works in C# 6 though

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.