Related
I am trying to make a property in my class. What would I need to do where once the property is initialized, it cannot be changed?
These are the actual instructions:
Create a class in the existing namespace, either in an existing code
file or in a new file, to represent the amount in pounds of dirt
sample. In this class (a) do not create constructors. (b) inherit the
sand class (to make use of the sand property). (c) add a property to
represent the sample name. This property may not be changed once
initialized. (d) add a property to represent and process assignments
the quantity of clay, with a minimum value of 0. (e) add methods to
return the weight of the sample, the percentage of sand in the sample
and the percentage of clay in the sample.
I am on part (c). I have tried to exclude setters. Then, I've tried to use readonly, but it cannot work because my class cannot have constructors.
public class AmountSand //parent class
{
public class AmountSand {
private double quantity;
public double Sand {
get {
return quantity;
}
set {
if (value >= 0) quantity = value;
}
}
}
public class AmountDirt: AmountSand { //part (b): inherited the parent class, AmountSand
private string name = null;
private double clay;
public string Name { //here is where the specific property starts
get {
return name;
}
set {
if (name == null)
name = value;
}
} //ends
public double Clay {
get {
return clay;
}
set {
if (value >= 0) clay = value;
}
}
Depends on from where you would like it to be initialized.
EDIT: sorry, i didn't read that your class could have ctors, but i'll keep this in for completeness. It seems kinda weird that your class can't have ctors. May I ask why?
From the ctor:
class MyClass
{
public MyClass()
{
Name = "Muhammed";
}
public MyClass(string newName)
{
Name = newName;
}
public string Name{get;}
}
If you'd like it to be initialized from outside the class, your code is not too far off. You could even remove the backing property. I'd use
if(string.IsNullOrEmpty(Name))
rather than comparing to null.
if you'd like it to be set from a method inside your class:
public string Name{get; private set;}
Strings are already immutable by nature, so you need to clarify what you're trying to accomplish.
However, if you simply don't want anything else to be able to set the value other than the class itself, then you can make the set accessor private.
Is it possible to have one variable point to another variable for shortcut purposes? For instance, let's say I have a variable in one class that is called SharedResources.proxTelescope, how do I get a variable in another class called prox to point to the first variable, in a sort of shortcut type of thing. I could just do var prox = SharedResources.proxTelescope;, but if proxTelescope changes, it won't reflect on prox, will it? How should I do it instead.
I would set things up as a property.
private <type> prox
{
get { return SharedResources.proxTelescope; }
set { SharedResources.proxTelescope = value }
}
You can create a property, something like:
public YourTypeHere prox
{
get { return SharedResources.proxTelescope; }
set { SharedResources.proxTelescope = value; }
}
If the variables are classes then they are reference type, so any change in one of them will be reflected in the other.
If the variables are structs, then they are value types and you have a problem, you can make some function(or property) to get the value.
If the value should be 'shared' among all instances, then it would probably be best to have the variable exist as a static member of the class it is on, and create an instance property that retreives the value of the static variable.
Another approach would be to use a Func<T>:
public class MyClass
{
public Func<DesiredType> ValueGetter {get;set;}
public DesiredType Value { get { return ValueGetter(); } }
}
This can then by used this way:
var myClass = new MyClass();
myClass.ValueGetter = () => SharedResources.proxTelescope;
var value = myClass.Value;
I have a windows form where I have several text boxes to introduce the values that will be used several methods.
My intention was to define the variables global, so they can be used throughout the whole programme, but although I get no compilation errors, the variables are empty when I check (there are numbers written in all of them). Here comes some of the variables, maybe I'm assigning wrong the values:
//This is in the mainform, where all the textboxes are stored
namespace WindowsFormsApplication1{
public partial class Rotor_Calc:Form
{
public Rotor_Calc()
{
InitializeComponent();
}
public string T_HotIn
{
get { return Temphotin.Text; }
set { Temphotin.Text = value; }
}
public string F_Cold
{
get { return flowCold.Text; }
set { flowCold.Text = value; }
}
// this is in a class named Globals
public class Globals
{
public static string THotIn;
public static string FlowCold;
public Globals(Rotor_Calc Rotor)
{
THotIn = Rotor.T_HotIn;
FlowCold = Rotor.F_Cold;
}
public static double Thin = Convert.ToDouble(Globals.THotIn);
public static double speedCold = Convert.ToDouble(Globals.FlowCold);
}
Then, in the following methods I write Globals.Thin, so it can take the value from the textbox, but when I compile it remains empty.
I would like also to take the computed value of another variable and write it in the results textbox. For this purpose I followed the same process backwards:
//In the main form define the textboxes
public string Eff_Hot
{
get { return effecthot.Text; }
set { effecthot.Text = value; }
}
public string Eff_Cold
{
get { return effectcold.Text; }
set { effectcold.Text = value; }
}
//in the globals method, take the value from the calculation method:
public class Globals{
public static string eff_cold;
public static string eff_hot;
public Globals(Rotor_Calc Rotor){
eff_cold = Rotor.Eff_Cold;
eff_hot = Rotor.Eff_Hot;
}
public static double effcold=Convert.ToDouble(eff_cold);
public static double effhot = Convert.ToDouble(eff_hot);
}
Of course the last is not working because the calculations inbetween give infinite values.
What am I exactly doing wrong? can somebody help?
I think your problem is the fact that this statement:
public static double effcold=Convert.ToDouble(eff_cold);
is executed as soon as the Globals class is accessed for the first time.
This means that it will always be executed before your constructor Globals(Rotor_Calc Rotor) is executed, i.e. it is executed before eff_cold and eff_hot are assigned a value.
Maybe you should move effcold=Convert.ToDouble(eff_cold); and effhot = Convert.ToDouble(eff_hot); inside the constructor, but I'm not sure since it's not too clear to me what you are trying to do.
If you are tryng to use Globals to access the value of your TextBoxes from every point of your application, you should keep in Globals a pointer to your Form, not the values. The statements in the constructor are executed just once. This means that you are storing in eff_cold and eff_hot the value of your TextBox at THAT moment, and those values won't be changed unless you execute the constructor again.
Furthermore, I don't like the idea of havin a constructor for a class that contains only static stuff.
A possible working version of your code (if I got it right) could be this one:
public class Globals
{
public static Rotor_Calc Rotor;
public static double EffHot
{
get { return Convert.ToDouble(Rotor.EffHot); }
set { Rotor.EffHot = value.ToString(); }
}
public static double EffCold
{
get { return Convert.ToDouble(Rotor.EffCold); }
set { Rotor.EffCold = value.ToString(); }
}
}
This should work as long as you set Globals.Rotor = yourInstanceOfRotor_Calc; (maybe with a
Globals.Rotor = this; after the InitializeComponent() of Rotor_Calc).
Anyway, I don't like to keep references to Forms in Globals, I'd prefer some sort of DataBinding.
EDIT according to your last comment:
You can just write a setter for Rotor, assign the values there and call the setter after InitializeComponent():
public class Globals
{
public static double EffHot;
public static double EffCold;
public static Rotor_Calc Rotor
{
set
{
EffHot = Convert.ToDouble(Rotor.EffHot);
EffCold = Convert.ToDouble(Rotor.EffCold);
}
}
}
What is the best practice to create pre-initialized class. For example
Chip chip = new Atmega8();
I would like to have its properties already defined like:
chip.Name = "Atmega8 AVR Chip";
and so on.
How to achieve it in C#?
Should I use readonly public properties or property with private set?
Have your constructor initialize the values:
class Atmega8 {
public Atmega8 ()
{
Name = "Atmega8 AVR Chip";
}
public string Name { get; set; }
}
If you intend Name to be the same for all instances, it might make sense to declare it abstract in the base class and override the getter:
abstract class Chip {
public abstract string Name { get; }
}
class Atmega8 : Chip {
public override string Name {
get { return "Atmega8 AVR Chip"; }
}
}
Because we haven't defined a set method, the value cannot be changed, much like a readonly variable except it isn't even stored anywhere and just returned on each call.
If you want the compiler to enforce that nothing can change the value of the field once initialized, then set it up as a read-only field, and populate it in the constructor of the class (or simply initialize it when you declare it; this doesn't work so well with inheritance though). If you don't care as long as nothing OUTSIDE the object can change it (meaning you will trust your own coding discipline to ensure it doesn't change internally), a get-only property with a backing field, or an auto-property with a private setter, are your bets.
IF you absitively posolutely DO NOT WANT the value to change for a particular class, EVER, then I would make it a get-only property returning either a string literal or a constant. I would recommend using the constant over the literal, as you can put the constants into their own static class which you can then use separately from each Chip class.
HOWEVER, there's a quirk of constants you should know. A constant value in .NET is stored in the manifest of not only the assembly containing the declaring code, but in every assembly that references the declaring assembly. Each assembly's code them uses the value from its own manifest. So, if the constant value EVER changes, any assembly that references the declaring assembly must be recompiled to update those assemblies' manifests with the new value. Otherwise, the constant will only have its new value when used from within the declaring assembly. For this reason, labeling a variable as constant should not be done lightly. Personally, my opinion is if the constant isn't some value on which the continued existence and functioning of the universe depends, like pi, e, the speed of light in a vacuum, Plank's Constant, Avogadro's Number, etc, then it isn't "constant". Anything else, like communication code ordinals, CAN change, even if doing so would break compatibility with every previous version of your program.
Depends what you want to accomplish.
It looks like you never want the value of Name to change. One approach would be to declare Name as abstract in Chip, and implement Name in each child class to return a constant string value.
abstract class Chip
{
public abstract string Name { get; }
}
class Amiga8 : Chip
{
public override string Name { get { return "Atmega8 AVR Chip"; } }
}
class Program
{
static void Main(string[] args)
{
Chip chip = new Amiga8();
Console.WriteLine(chip.Name);
}
}
In the constructor of the Atmega8 class you can set a property to something. Ie:
public Atmega8() {
Name = "Atmega8 AVR Chip";
}
If you do not want that to be changed in runtime you could mark the property as readonly ( only assignable through a constructor of declarative ).
private readonly string _Name = string.Empty;
public string Name {
get { return _name; }
}
public Atmega8() {
_Name = "Atmega8 AVR Chip";
}
Value of property cannot change -> Read-only public property.
Value of property can change -> Property with private set
If you don't want it to change, make the Name property a const or readonly on the Atmega8 class. Private set still allows the Name to change internally.
You're saying that you want the class to be populated at the same time it's initialized? Just populate the object in the constructor, like so:
class Test
{
public Test()
{
this.Name = "Hello World";
}
//if you need to pass information into the constructor:
public Test(string testName)
{
this.Name = testName;
}
}
Then, you can do this to initialize it:
Test test = new Test(); //default name of Hello World!
OR
Test test = new Test("Bingo!");
I am learning ASP.NET MVC and I can read English documents, but I don't really understand what is happening in this code:
public class Genre
{
public string Name { get; set; }
}
What does this mean: { get; set; }?
It's a so-called auto property, and is essentially a shorthand for the following (similar code will be generated by the compiler):
private string name;
public string Name
{
get
{
return this.name;
}
set
{
this.name = value;
}
}
So as I understand it { get; set; } is an "auto property" which just like #Klaus and #Brandon said is shorthand for writing a property with a "backing field." So in this case:
public class Genre
{
private string name; // This is the backing field
public string Name // This is your property
{
get => name;
set => name = value;
}
}
However if you're like me - about an hour or so ago - you don't really understand what properties and accessors are, and you don't have the best understanding of some basic terminologies either. MSDN is a great tool for learning stuff like this but it's not always easy to understand for beginners. So I'm gonna try to explain this more in-depth here.
get and set are accessors, meaning they're able to access data and info in private fields (usually from a backing field) and usually do so from public properties (as you can see in the above example).
There's no denying that the above statement is pretty confusing, so let's go into some examples. Let's say this code is referring to genres of music. So within the class Genre, we're going to want different genres of music. Let's say we want to have 3 genres: Hip Hop, Rock, and Country. To do this we would use the name of the Class to create new instances of that class.
Genre g1 = new Genre(); //Here we're creating a new instance of the class "Genre"
//called g1. We'll create as many as we need (3)
Genre g2 = new Genre();
Genre g3 = new Genre();
//Note the () following new Genre. I believe that's essential since we're creating a
//new instance of a class (Like I said, I'm a beginner so I can't tell you exactly why
//it's there but I do know it's essential)
Now that we've created the instances of the Genre class we can set the genre names using the 'Name' property that was set way up above.
public string Name //Again, this is the 'Name' property
{ get; set; } //And this is the shorthand version the process we're doing right now
We can set the name of 'g1' to Hip Hop by writing the following
g1.Name = "Hip Hop";
What's happening here is sort of complex. Like I said before, get and set access information from private fields that you otherwise wouldn't be able to access. get can only read information from that private field and return it. set can only write information in that private field. But by having a property with both get and set we're able do both of those functions. And by writing g1.Name = "Hip Hop"; we are specifically using the set function from our Name property
set uses an implicit variable called value. Basically what this means is any time you see "value" within set, it's referring to a variable; the "value" variable. When we write g1.Name = we're using the = to pass in the value variable which in this case is "Hip Hop". So you can essentially think of it like this:
public class g1 //We've created an instance of the Genre Class called "g1"
{
private string name;
public string Name
{
get => name;
set => name = "Hip Hop"; //instead of 'value', "Hip Hop" is written because
//'value' in 'g1' was set to "Hip Hop" by previously
//writing 'g1.Name = "Hip Hop"'
}
}
It's Important to note that the above example isn't actually written in the code. It's more of a hypothetical code that represents what's going on in the background.
So now that we've set the Name of the g1 instance of Genre, I believe we can get the name by writing
console.WriteLine (g1.Name); //This uses the 'get' function from our 'Name' Property
//and returns the field 'name' which we just set to
//"Hip Hop"
and if we ran this we would get "Hip Hop" in our console.
So for the purpose of this explanation I'll complete the example with outputs as well
using System;
public class Genre
{
public string Name { get; set; }
}
public class MainClass
{
public static void Main()
{
Genre g1 = new Genre();
Genre g2 = new Genre();
Genre g3 = new Genre();
g1.Name = "Hip Hop";
g2.Name = "Rock";
g3.Name = "Country";
Console.WriteLine ("Genres: {0}, {1}, {2}", g1.Name, g2.Name, g3.Name);
}
}
Output:
"Genres: Hip Hop, Rock, Country"
Those are automatic properties
Basically another way of writing a property with a backing field.
public class Genre
{
private string _name;
public string Name
{
get => _name;
set => _name = value;
}
}
It is a shortcut to expose data members as public so that you don't need to explicitly create a private data members. C# will creates a private data member for you.
You could just make your data members public without using this shortcut but then if you decided to change the implementation of the data member to have some logic then you would need to break the interface. So in short it is a shortcut to create more flexible code.
This is the short way of doing this:
public class Genre
{
private string _name;
public string Name
{
get => _name;
set => _name = value;
}
}
Basically, it's a shortcut of:
class Genre{
private string genre;
public string getGenre() {
return this.genre;
}
public void setGenre(string theGenre) {
this.genre = theGenre;
}
}
//In Main method
genre g1 = new Genre();
g1.setGenre("Female");
g1.getGenre(); //Female
Basically it helps to protect your data. Consider this example without setters and getter and the same one with them.
Without setters and getters
Class Student
using System;
using System.Collections.Generic;
using System.Text;
namespace MyFirstProject
{
class Student
{
public string name;
public string gender;
public Student(string cName, string cGender)
{
name = cName;
gender= cGender;
}
}
}
In Main
Student s = new Student("Some name", "Superman"); //Gender is superman, It works but it is meaningless
Console.WriteLine(s.Gender);
With setters and getters
using System;
using System.Collections.Generic;
using System.Text;
namespace MyFirstProject
{
class Student
{
public string name;
private string gender;
public Student(string cName, string cGender)
{
name = cName;
Gender = cGender;
}
public string Gender
{
get { return gender; }
set
{
if (value == "Male" || value == "Female" || value == "Other")
{
gender = value;
}
else
{
throw new ArgumentException("Invalid value supplied");
}
}
}
}
}
In Main:
Student s = new Student("somename", "Other"); // Here you can set only those three values otherwise it throws ArgumentException.
Console.WriteLine(s.Gender);
Its an auto-implemented property for C#.
The get/set pattern provides a structure that allows logic to be added during the setting ('set') or retrieval ('get') of a property instance of an instantiated class, which can be useful when some instantiation logic is required for the property.
A property can have a 'get' accessor only, which is done in order to make that property read-only
When implementing a get/set pattern, an intermediate variable is used as a container into which a value can be placed and a value extracted. The intermediate variable is usually prefixed with an underscore.
this intermediate variable is private in order to ensure that it can only be accessed via its get/set calls. See the answer from Brandon, as his answer demonstrates the most commonly used syntax conventions for implementing get/set.
They are the accessors for the public property Name.
You would use them to get/set the value of that property in an instance of Genre.
That is an Auto-Implemented Property. It's basically a shorthand way of creating properties for a class in C#, without having to define private variables for them. They are normally used when no extra logic is required when getting or setting the value of a variable.
You can read more on MSDN's Auto-Implemented Properties Programming Guide.
This mean that if you create a variable of type Genre, you will be able to access the variable as a property
Genre oG = new Genre();
oG.Name = "Test";
In the Visual Studio, if you define a property X in a class and you want to use this class only as a type, after building your project you will get a warning that says "Field X is never assigned to, and will always has its default value".
By adding a { get; set; } to X property, you will not get this warning.
In addition in Visual Studio 2013 and upper versions, by adding { get; set; } you are able to see all references to that property.
Its basically a shorthand. You can write public string Name { get; set; } like in many examples, but you can also write it:
private string _name;
public string Name
{
get { return _name; }
set { _name = value ; } // value is a special keyword here
}
Why it is used? It can be used to filter access to a property, for example you don't want names to include numbers.
Let me give you an example:
private class Person {
private int _age; // Person._age = 25; will throw an error
public int Age{
get { return _age; } // example: Console.WriteLine(Person.Age);
set {
if ( value >= 0) {
_age = value; } // valid example: Person.Age = 25;
}
}
}
Officially its called Auto-Implemented Properties and its good habit to read the (programming guide).
I would also recommend tutorial video C# Properties: Why use "get" and "set".
Such { get; set; } syntax is called automatic properties, C# 3.0 syntax
You must use Visual C# 2008 / csc v3.5 or above to compile.
But you can compile output that targets as low as .NET Framework 2.0 (no runtime or classes required to support this feature).
Get set are access modifiers to property.
Get reads the property field.
Set sets the property value.
Get is like Read-only access.
Set is like Write-only access.
To use the property as read write both get and set must be used.
Get is invoked when the property appears on the right-hand side (RHS)
Set is invoked when the property appears on the left-hand side (LHS)
of '=' symbol
For an auto-implemented property, the backing field works behind the scene and not visible.
Example:
public string Log { get; set; }
Whereas for a non auto-implemented property the backing field is upfront, visible as a private scoped variable.
Example:
private string log;
public string Log
{
get => log;
set => log = value;
}
Also, it is worth noted here is the 'getter' and 'setter' can use the different 'backing field'
A property is a like a layer that separates the private variable from other members of a class. From outside world it feels like a property is just a field, a property can be accessed using .Property
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName => $"{FirstName} {LastName}";
}
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName { get { return $"{FirstName} {LastName}"; } }
}
FullName is a Property. The one with arrow is a shortcut. From outside world, we can access FullName like this:
var person = new Person();
Console.WriteLine(person.FullName);
Callers do not care about how you implemented the FullName. But inside the class you can change FullName whatever you want.
Check out Microsoft Documentation for more detailed explanation:
https://learn.microsoft.com/en-us/dotnet/csharp/properties
Define the Private variables
Inside the Constructor and load the data
I have created Constant and load the data from constant to Selected List class.
public class GridModel
{
private IEnumerable<SelectList> selectList;
private IEnumerable<SelectList> Roles;
public GridModel()
{
selectList = from PageSizes e in Enum.GetValues(typeof(PageSizes))
select( new SelectList()
{
Id = (int)e,
Name = e.ToString()
});
Roles= from Userroles e in Enum.GetValues(typeof(Userroles))
select (new SelectList()
{
Id = (int)e,
Name = e.ToString()
});
}
public IEnumerable<SelectList> Pagesizelist { get { return this.selectList; } set { this.selectList = value; } }
public IEnumerable<SelectList> RoleList { get { return this.Roles; } set { this.Roles = value; } }
public IEnumerable<SelectList> StatusList { get; set; }
}
Properties are functions that are used to encapsulate data, and allow additional code to be executed every time a value is retrieved or modified.
C# unlike C++, VB.Net or Objective-C doesn’t have a single keyword for declaring properties, instead it uses two keywords (get/set) to give a much abbreviated syntax for declaring the functions.
But it is quite common to have properties, not because you want to run additional code when data is retrieved or modified, but because either you MIGHT want to do so in the future or there is a contract saying this value has to be a exposed as a property (C# does not allow exposing data as fields via interfaces). Which means that even the abbreviated syntax for the functions is more verbose than needed. Realizing this, the language designers decided to shorten the syntax even further for this typical use case, and added “auto” properties that don’t require anything more than the bare minimum, to wit, the enclosing braces, and either of the two keywords (separated by a semicolon when using both).
In VB.Net, the syntax for these “auto” properties is the same length as in c# —- Property X as String vs string X {get; set;}, 20 characters in both cases. It achieves such succinctness because it actually requires 3 keyword under the normal case, and in the case of auto properties can do without 2 of them.
Removing any more from either, and either a new keyword would have had to be added, or significance attached to symbols or white space.