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.
Related
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
};
This question already has answers here:
Overlay data from JSON string to existing object instance
(4 answers)
Closed 4 years ago.
I want to create a new object, and during object creation make an RPC call to get properties for it, and then return the object populated with the properties. See this example:
using Newtonsoft.Json;
class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Person(int Id)
{
// here would be an RPC call to get the FirstName,LastName. result is JSON
string result = "{\"Id\": 1, \"FirstName\": \"Bob\", \"LastName\": \"Jones\"}";
this = JsonConvert.DeserializeObject<Person>(result);
}
}
class Program
{
static void Main(string[] args)
{
var p = new Person(1);
// p.FirstName should be Bob
}
}
I don't know how to do this in the constructor without getting a StackOverflow Exception.
One option to consider is to use a static method inside Person:
public static Person GetPerson(int Id)
{
// here would be an RPC call to get the FirstName,LastName. result is JSON
string result = "{\"Id\": 1, \"FirstName\": \"Bob\", \"LastName\": \"Jones\"}";
return JsonConvert.DeserializeObject<Person>(result);
}
This avoids the recursive nature of your original code.
Another option is to change the class to a struct. structs allow you to assign to this (unlike classes). And they have a default constructor (separate to your one taking a parameter) so that there is no recursive behaviour.
This question already has answers here:
Do properties always have a value when unset?
(4 answers)
Closed 4 years ago.
I found this code on a PDF tutorial:
public class Chien
{
public static int NombreDeChiens { get; set; }
private string prenom;
public Chien(string prenomDuChien)
{
prenom = prenomDuChien;
NombreDeChiens++;
}
public void Aboyer()
{
Console.WriteLine("Wouaf ! Je suis " + prenom);
}
}
There is no initialization in this code though in the constructor there is this incrementation. So how is it possible?
A property is nothing but a field wrapped with a get- and a set-method. As fields do have an initial value, properties also do.
E.g. the following code:
public int MyProperty { get; set; }
is translated to something like this by the compiler:
private int _myProperty; // the actual name of the field defers and is only known by the compiler
public int get_MyProperty() { return this._myProperty; }
public void set_MyProperty(int value) { this._myProperty = value; }
So the question boils down to "have fields a default-value"? The answer to this question is yes, they do: null for reference-types and the default-value for all structs, e.g. 0 for int, or 0f for float.
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.
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);