How to assign a value to an enum? [duplicate] - c#

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 3 years ago.
Trying to assign a value to the _cat.CatType.
I was wondering how to do this without getting a null reference error?
using System;
public class Program
{
private CatClass _cat;
public void Main()
{
_cat.CatType = CatType.Active;
Console.WriteLine(_cat.CatType.ToString());
}
public enum CatType
{
New,
Active,
Inactive
}
public class CatClass
{
public CatType CatType
{
get;
set;
}
}
}
Ideally I want to assign it something like this _cat.CatType = CatType.Active

You need to initialise it with the new keyword
Used to create objects and invoke constructors
public void Main()
{
_cat = new CatClass();
_cat.CatType = CatType.Active;
Console.WriteLine(_cat.CatType.ToString());
}

You need to create an instance of the class.
private CatClass _cat = new CatClass;

Instantiate _cat with the new keyword first:
_cat = new CatClass
{
CatType = CatType.Active
};

Related

Custom class out of existing one [duplicate]

This question already has answers here:
Comparing Object properties using reflection
(5 answers)
Closed 3 years ago.
I would like to know if there is a way to create a custom class out of an existing class in this manner:
original class:
public class Person
{
string name;
}
later in code:
var diffPerson = CreateDiffClass<Person>();
diffPerson.name.Value = "name";
diffPerson.name.Changed = false;
this diffPerson is not of Person type, instead it is custom created one, that for every variable in Person have one in itself so that the new variable is a tuple where T is the type of the variable.
I want it to create a system for comparing 2 instances of the same class. one old instance and one new and save the new value and if it changed
I don't really know how to describe it except showing in this example so I hope it is understandable..
I want this to be generic and work on any given class
Thanks
You can declare a generic class like this
public class CustomValue<T>
{
public T Value { get; set; }
public bool Changed { get; set; }
}
and then use it like this
public class Person
{
public CustomValue<string> Name;
}
later in code
var diffPerson = new Person();
diffPerson.Name = new CustomValue<string>();
diffPerson.Name.Value = "name";
diffPerson.Name.Changed = false;

Providing Access to a Class Private Member Attributes [duplicate]

This question already has answers here:
Properties vs Methods
(16 answers)
Closed 4 years ago.
I was wondering what is more efficient or the best practice for returning attributes of private members. For example:
class Foo
{
private List<int> fooList;
public Foo()
{
Random random = new Random();
fooList = new List<int>(random.Next(1, 100));
}
//
public int Count { get { return fooList.Count; } }
// or
public int Count() { return fooList.Count; }
}
Which is best if I do not want to give public access to my list?
Based on your example, you should stay with a property. Because the fooList.Count is a property.

How to copy a class object in C# [duplicate]

This question already has answers here:
How do you do a deep copy of an object in .NET? [duplicate]
(10 answers)
Creating a copy of an object in C# [duplicate]
(4 answers)
Closed 5 years ago.
As per my understanding in C#, when an object is assigned to another object of same type, its reference is copied instead of the value. So I would like to know how to assign an object to another object such that its values are copied and not referenced to the second object..
You can see that test2.ErrorDetail is changed to "DTL2", while test1.ErrorDetail remains "DTL1".
public class TestClone : ICloneable
{
public bool IsSuccess { get; set; }
public string ErrorCode { get; set; }
public string ErrorDetail { get; set; }
public object Clone()
{
return this.MemberwiseClone();
}
}
static void Main(string[] args)
{
var test1 = new TestClone() { IsSuccess = true, ErrorCode = "0", ErrorDetail = "DTL1" };
var test2 = (TestClone) test1.Clone();
test2.ErrorDetail = "DTL2";
}
You have to clone an object instead of assigning a reference.

i am trying to pass values to an array in another class [duplicate]

This question already has answers here:
How do define get and set for an array data member?
(9 answers)
Closed 6 years ago.
i am trying to pass values to an array in another class
this is an example of what i am trying to do in detail:
public class CustomString
{
private string[] StringToAppend;
public string[] StringToAppend1
{
get
{
return StringToAppend;
}
set
{
StringToAppend = value;
}
}
public Class Form1:Form
{
CustomString strng1 = new CustomString();
strng1.StringToAppend1 = {"sssf","vfdr";} //Fails to compile Here
}
strng1.StringToAppend1 = {"sssf","vfdr";} //Fails to compile Here
That is the incorrect syntax for initialising a string array
strng1.StringToAppend1 = new[]{"sssf","vfdr"} ;
Above is the correct syntax.

Accessing property through string name [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to set object property through Reflection
If I have the following program:
public class MyClass
{
public int MyIntProp {
get;
set;
}
public string MyStringProp {
get;
set;
}
}
public class MyMainClass
{
private const string PropertyName = "MyIntProp";
private MyClass _myClass;
public MyMainClass()
{
_myClass = new MyClass();
// _myClass.PropertyName = 5;
}
}
What I want to do is be able to assign a value of 5 to the MyIntProp property. Is it possible to do this using a string name? I though I saw something like this done before using LINQ, but I can't seem to remember the syntax or where I found it.
You can use Reflection with GetProperty method:
typeof(MyClass).GetProperty(PropertyName).SetValue(_myClass, 5);

Categories