C# - Update list item with new object values - c#

I have an object which has some properties and a few of those properties are Lists. Each list contains instances of other classes. What i want to do is take the first item from a list and overwrite those property values.
Here's a pseudo example of what i have:
public class User
{
public List<Address> Addresses = new List<Address>();
public User ( )
{
Addresses = fill with data;
}
}
public class TestUser
{
public User user; // Is filled somewhere in this class
public void TestUpdateList ( Address addr )
{
// The param "addr" contains new values
// These values must ALWAYS be placed in the first item
// of the "Addresses" list.
// Get the first Address object and overwrite that with
// the new "addr" object
user.Addresses[0] = addr; // <-- doesn't work, but should give you an idea
}
}
I hope this example shed some light on what i want to do.
So i am basically looking for a way to "update" an existing item in a list, which is in this case an object.

It is not entirely clear what you are trying to accomplish, however, see the following code -- there is an Address, a User, and an utility called FeatureX that replaces the first Address of a User with a given value.
class Address {
public string Street { get; set; }
}
class User {
public List<Address> Addresses = new List<Address>();
}
class FeatureX {
public void UpdateUserWithAddress(User user, Address address) {
if (user.Addresses.Count > 0) {
user.Addresses[0] = address;
} else {
user.Addresses.Add(address);
}
}
}
The following usage outputs 'Xyz' two times:
User o = new User();
Address a = new Address() { Street = "Xyz" };
new FeatureX().UpdateUserWithAddress(o, a);
Console.WriteLine(o.Addresses[0].Street);
o = new User();
o.Addresses.Add(new Address { Street = "jjj" });
new FeatureX().UpdateUserWithAddress(o, a);
Console.WriteLine(o.Addresses[0].Street);
Be aware that public fields may cause a lot of trouble if you share your DLL with a third party.

Your example doesn't compile because you're accessing the Addresses property via class name. That is only possible if it is static. So you need an instance of a user first, to update his addresses:
User u = new User(userID); // assuming that theres a constructor that takes an identifier
u.Addresses[0] = addr;
C# Language Specification: 10.2.5 Static and instance members

I think the problem is that Addresses is a private field.
This works:
[TestFixture]
public class ListTest
{
[Test]
public void UpdateTest()
{
var user = new User();
user.Addresses.Add(new Address{Name = "Johan"});
user.Addresses[0] = new Address { Name = "w00" };
}
}
public class User
{
public List<Address> Addresses { get;private set; }
public User()
{
Addresses= new List<Address>();
}
}
public class Address
{
public string Name { get; set; }
}

public void TestUpdateList ( User user, Address addr )
{
// The param "addr" contains new values
// These values must ALWAYS be placed in the first item
// of the "Addresses" list.
// Get the first Address object and overwrite that with
// the new "addr" object
user.Addresses[0] = addr; // <-- doesn't work, but should give you an idea
}

Related

Accessing list of object inside an object

I have class named ResponseModel and one object inside that class named Errors refer to the class ErrorsResponseModel, and that class has bunch of objects which have List<string> data type . I would like to know on how to accessing the List<string> objects without going through like: VariableClassA.ObjectOfClassAWhichReferToTheClassB.FirstListOfString and VariableClassA.ObjectOfClassAWhichReferToTheClassB.SecondListOfString, the data of List<string> objects comes from the JSON data.
I have tried only to access one object per one object as I am not really sure on how to do generic without going through one object per one object, which is if I update the model of class B itself, then I need to make sure that I didn't missed out the necessary checking of that newly created object inside class B.
Here is the code of the model:
public sealed class ResponseModel
{
public ErrorsResponseModel Errors { get; set; }
}
public sealed class ErrorsResponseModel
{
public List<string> Username { get; set; }
public List<string> Password { get; set; }
public List<string> Nickname { get; set; }
}
Here is what I have tried so far:
string jsonData = "{"Errors":{"Username":["The username field is required."],"Password":["The password field is required."],"Nickname":["The nickname field is required."]}}";
var jsonConvertedData = JsonConvert.DeserializeObject<ResponseModel>(jsonData);
var usernameErrors = jsonConvertedData.Errors.Username;
var passwordErrors = jsonConvertedData.Errors.Password;
var nicknameErrors = jsonConvertedData.Errors.Nickname;
I expect to loop any object of class ErrorsResponseModel that the length of List<string> inside that class is more than 0 . I can't change the response data from the JSON, as it is comes from the third party.
EDIT: I have tried the following in JavaScript and it works, how can I do the same in C#?
in C#, I return to the front end like using the following return Json(jsonConvertedData), and in frontend, I do like the following:
$.ajax({
..... the AJAX settings
success: function (data) {
$.each(data.Errors, function (i, v) {
if (v.length > 0) {
console.log(v);
}
});
}
The above code in Javascript is looping through the message inside each object inside ErrorsResponseModel and read it through to the console.
Let ErrorsResponseModel inherit Dictionary
public sealed class ErrorsResponseModel : Dictionary<string, List<string>>
{
//If you still want to access data with property.
public List<string> Username => this["Username"];
...
}
Now you can loop through Errors like a normal dictionary
foreach (var item in jsonConvertedData.Errors)
if(item.Value.Count > 0)
Console.WriteLine($"{item.Key} => {item.Value[0]}");
dynamic is another choice
var jsonConvertedData = JsonConvert.DeserializeObject<dynamic>(jsonData);
foreach (var item in jsonConvertedData.Errors)
if(item.Count > 0)
foreach(var v in item.Value)
Console.WriteLine(v);
One way to do this would be to create a class to hold the related data, for example:
class User
{
public string Name { get; set; }
public string Nickname { get; set; }
public string Password { get; set; }
}
Then we can make a method that populates a list of this class from the ErrorsResponseModel class (after first validating that the counts of all the lists are the same):
public List<User> GetUsers(ErrorsResponseModel errors)
{
if (errors == null || errors.Username == null) return null;
if (errors.Username.Count == 0) return new List<User>();
if (errors.Nickname?.Count != errors.Password?.Count ||
errors.Password?.Count != errors.Username.Count)
{
throw new InvalidDataException("Unequal number of Usernames/Passwords/Nicknames");
}
return errors.Username
.Select((userName, index) =>
new User
{
Name = userName,
Nickname = errors.Nickname[index],
Password = errors.Password[index]
}).ToList();
}

C# Lists - do I use Class Methods (Get/ Set etc) again once the data is in a list?

A quick question on OOP. I am using a list together with a class and class constructor. So I use the class constructor to define the data set and then add each record to my list as the user creates them.
My questions is once the data is in the list and say I want to alter something is it good practice to find the record, create an instance using that record and then use my class methods to do whatever needs doing - and then put it back in the list?
For example below I have my class with constructor. Lets say I only want the system to release strCode if the Privacy field is set to public. Now just using Instances I would use for example Console.WriteLine(whateverproduct.ProductCode) but if the record is already in a list do i take it out of the list - create an instance and then use this method?
class Product
{
private String strCode;
private Double dblCost;
private Double dblNet;
private String strPrivacy;
public Product(String _strCode, Double _dblCost, Double _dblNet, String _strPrivacy)
{
strCode = _strCode;
dblCost = _dblCost;
dblNet = _dblNet;
strPrivacy = _strPrivacy;
}
public string ProductCode
{
get
{
if (strPrivacy == "Public")
{
return strCode;
}
else
{
return "Product Private Can't release code";
}
}
}
Lets say we have the following:
public class Test
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Amount { get; set; }
private string _test = "Some constant value at this point";
public string GetTest()
{
return _test;
}
public void SetTest()
{
//Nothing happens, you aren't allow to alter it.
//_test = "some constant 2";
}
}
public class Program
{
public static void Main(string[] args)
{
List<Test> listOfTest = new List<Test>()
{
new Test() {Id = 0, Name = "NumberOne", Amount = 1.0M},
new Test() {Id = 1, Name = "NumberTwo", Amount = 2.0M}
};
Test target = listOfTest.First(x => x.Id == 0);
Console.WriteLine(target.Name);
target.Name = "NumberOneUpdated";
Console.WriteLine(listOfTest.First(x => x.Id == 0).Name);
Console.WriteLine(listOfTest.First(x => x.Id == 0).GetTest());//This will alsways be "Some constant value at this point";
Console.ReadLine();
}
}
Technically you could do away with the SetTest method entirely. However, I included it to demonstrate, what it would look like, if you wanted to alter _test.
You don't want to ever create a new instance of a class, you already have an instance of. you can just alter the class where it is allowed by the author of the class, where you need to. And keep that class reference for as long as you need it. Once you are done, the reference will be garbage collected, once the program finds no active reference to your object(instance).

C# - How to return an Array to set a Class property

I am trying to create a Class Method which can be called to Query the Database. The function itself works but for some reason, when the Array is returned, they're not set.
My function code is:
public Configuration[] tbl_bus(string type, string match)
{
// Create Obejct Instance
var db = new rkdb_07022016Entities2();
// Create List
List<Configuration> ConfigurationList = new List<Configuration>();
// Allow Query
if (type.ToLower() == "bustype")
{
foreach (var toCheck in db.tblbus_business.Where(b => b.BusType == match))
{
// Create Class Instance
var model = new Configuration { Name = toCheck.Name, BusinessID = toCheck.BusinessID };
// Append to the property
ConfigurationList.Add(model);
}
}
else if (type.ToLower() == "businessid")
{
foreach (var toCheck in db.tblbus_business.Where(b => b.BusinessID == match))
{
// Create Class Instance
var model = new Configuration { Name = toCheck.Name, BusinessID = toCheck.BusinessID };
// Append to the property
ConfigurationList.Add(model);
}
}
return ConfigurationList.ToArray();
}
And my Configuration code is:
public class Configuration
{
// Properties of the Database
public string Name { get; set; }
public string BusinessID { get; set; }
public string Address { get; set; }
}
public Configuration Config { get; set; }
public Controller()
{
this.Config = new Configuration();
}
On my Handler I am doing:
// Inside the NameSpace area
Controller ctrl;
// Inside the Main Void
ctrl = new Controller();
ctrl.tbl_bus("bustype", "CUS");
context.Response.Write(ctrl.Config.Name);
I tried watching the Class function and it does create the Array, only, when I watch the ctrl.Config.Name it is always set to NULL. Could anyone possibly help me in understanding why the return isn't actually setting the properties inside the Configuration class?
Edit: The function does run and it fetches 3006 rows of Data when matching the bus_type to customer. (Its a large Database) - Only, the properties are never set on return.
Edit: Is there a specific way to return an Array to a Class to set the Properties?
Thanks in advance!
Change your Configs in Controller to array
public Configuration[] Configs { get; set; }
Change your tbl_bus function to void, and set the Configs inside the function.
public void tbl_bus(string type, string match)
{
// do your code
// set the configs here
Configs = ConfigurationList.ToArray();
}
Hope it helps.
Although this is not a complete answer to your question, the problem probably lies in the fact that you're not doing anything with the array returned by the method. You're simply discarding it right away. If you change your code to
ctrl = new Controller();
Configuration[] config = ctrl.tbl_bus("bustype", "CUS");
you will be able to reference the array later on.
Console.WriteLine(config.Length);
Now you can use it to set any properties you like.

Can't add to a list within a class object

First, I'm going to apologize if this is a stupid question. I've been using C# for 16 hours after having not programmed anything since VB6. I'm just trying to hack together a small program for personal use that reads from an old access database and spits out a formatted report in Excel. I apologize for the messy/inefficient code.
Overview: I have two class types, "Zone" and "Device". Each "Zone" has a List of Devices in it. The main program has a List of Zones. Each database has a varying number of "zones" in it, and each "zone" has a varying number of devices assigned to it. I need to parse, sequentially, the zone list and the devices on each zone. I started with structs and arrays and popular opinion seems to be that those are both bad ways to do it, and I wasn't having much luck anyway, so I moved to lists and classes, and it was going well.
I can pull all the "zones" from the database, add them to the list, assign them their labels and IDs. The problem is when I go to read the "devices" from the database, I can't add them to the list within the Zone.
This is the error I get: "Object reference not set to an instance of an object." Which I gather means the object is null?
Here's the relevant code:
Device Class:
public class Device
{
public string Label;
public string Address;
public string Type;
public Device(string Label, string Address, string Type)
{
this.Address = Address;
this.Label = Label;
this.Type = Type;
}
}
Zone Class:
public class Zone
{
public string Label;
public short ID;
public List<Device> Devices;
public Zone(string Label, short ID) {
this.Label = Label;
this.ID = ID;
// ADDED AS PER SUGGESTIONS BELOW
this.Devices = new List<Device>();
}
// Added this to see if it would work, it would not.
public void AddDevice(string Label, string Address, string Type) {
Devices.Add(new Device(Label, Address, Type));
}
}
Initializing and populating Zone List (on button click) (completes successfully)
List<Classes.Zone> Zones = new List<Classes.Zone>();
dbZoneReader = myZoneSelect.ExecuteReader();
while (dbZoneReader.Read())
{
Classes.dbItem dbRow = new Classes.dbItem();
dbRow.Address = Convert.ToInt16(dbZoneReader["DeviceAddress"].ToString());
dbRow.DeviceType = Convert.ToInt16(dbZoneReader["DeviceType"].ToString());
dbRow.Label = dbZoneReader["DeviceLabel"].ToString();
if (dbRow.Label != "" && dbRow.Address > 0)
{
Zones.Add(new Classes.Zone(dbRow.Label,dbRow.Address));
}
}
Adding Devices to their respective Zones:
while (dbReader.Read()) {
Classes.dbItem dbRow = new Classes.dbItem();
string tempZones;
// Acquire/convert device information
dbRow.Node = Convert.ToInt16(dbReader["NodeAddress"].ToString());
dbRow.Loop = Convert.ToInt16(dbReader["LoopSelection"].ToString());
dbRow.Address = Convert.ToInt16(dbReader["DeviceAddress"].ToString());
dbRow.TypeID = Convert.ToInt16(dbReader["TypeID"].ToString());
dbRow.FlashScanID = Convert.ToInt16(dbReader["FlashScanID"].ToString());
dbRow.DeviceType = Convert.ToInt16(dbReader["DeviceType"].ToString());
dbRow.Label = dbReader["DeviceLabel"].ToString();
// Find "proper" zone ID (some zones have multiple IDs, only one is relevant)
tempZones = dbReader["DevicePointMappingList"].ToString();
tempZones = tempZones.Replace("Z", "");
var elements = tempZones.Split(new[] { ',' }, System.StringSplitOptions.RemoveEmptyEntries);
if (elements.Length >= 2) {
ZoneCheck z = new ZoneCheck();
foreach (string items in elements) { if (z.Check(items)) { dbRow.Zone = Convert.ToInt16(items); } }
} else {
if (elements.Length == 1) { dbRow.Zone = Convert.ToInt16(elements[0]); }
else { dbRow.Zone = 0; }
}
// Only add devices that aren't assigned to zone 0, which is non-existent
if (dbRow.Zone > 0) {
// Add new device to zone's device list [THIS IS WHERE IT FAILS]
Zones.Find(z => z.ID == dbRow.Zone).Devices.Add(new Classes.Device("Test", "test", "Test"));
}
}
I've gone through and found out exactly where it fails, and it's the last line where it tries to add the device. Searching here and on google has lead me to believe that I need to initialize the object list... which I believe I've done? I've tried initializing it within the Zone class constructor, and when the Zone is added (which is what it's set too now).
I've confirmed that the Zone object exists, and that the Detectors list within that Zone object isn't null. Kinda stumped, figure I'm doing something that I shouldn't be doing and just don't know better, or I'm missing something really obvious.
The problem is in your Zone class. You need to initialize the List<Device> as follows.
public class Zone
{
public string Label;
public short ID;
public List<Device> Devices;
public Zone(string Label, short ID) {
this.Label = Label;
this.ID = ID;
this.Devices = new List<Device>();
}
// Added this to see if it would work, it would not.
public void AddDevice(string Label, string Address, string Type) {
Devices.Add(new Device(Label, Address, Type));
}
}
The reason is that when you write public List<Device> Devices;, you're not actually creating an object. You're creating a variable that can hold an instance of the specified object. It's only when you pair the variable declaration up with object initialization ( = new List<Device>();) that you get a usable instance of the object.
Thinking of the same issue in terms of a simpler object may help:
public class Foo
{
public string bar; // bar isn't an actual instance of an object, it's just a spot that can hold a string
public void ManipulateBarWithRuntimeError()
{
bar.Substring(0, 1); // "bar" isn't actually set to anything, so how can we take a substring of it? This is going to fail at runtime.
}
public void ManipulateBarWithoutRuntimeError()
{
bar = "Hello, world!";
bar.Substring(0, 1); // bar is actually set to a string object containing some text, so now the Substring method will succeed
}
}
I think the problem is in your Zone class.
Here is my version of your Zone class:
public class Zone
{
public string Label;
public short ID;
public List<Device> Devices;
public Zone(string Label, short ID) {
this.Label = Label;
this.ID = ID;
this.Devices = new List<Device>();
}
// Added this to see if it would work, it would not.
public void AddDevice(string Label, string Address, string Type) {
Devices.Add(new Device(Label, Address, Type));
}
}
This is an only change that I made to your class;
this.Devices = new List<Device>();
Now it might work...
You can also initialize the list inside a getter
public class Zone
{
public string Label;
public short ID;
private List<Device> _devices;
public List<Device> Devices
{
get
{
return this._devices ?? (this._devices = new List<Device>());
}
}
public Zone(string Label, short ID)
{
this.Label = Label;
this.ID = ID;
}
// Added this to see if it would work, it would not.
public void AddDevice(string Label, string Address, string Type)
{
Devices.Add(new Device(Label, Address, Type));
}
}

Create an instance for appened string

I am trying to create a generic method where I have to append some string to Generic data type and create instance of that appended string.
Eg:
I have two classes
Object1,
Object2,
Object3,
Object4,....Object100 and
Object1Address,
Object2Address,
Object3Address,
Object4Address,....Object100Address
Right now I have method for each Object like MethodObject1(), MethodObject2(),MethodObject3(), MethodObject4()
MethodObject1()
{
Object1 object = new Object1();
Object1Address objectAddress = new Object1Address();
TestMethod();
}
MethodObject2()
{
Object2 object = new Object2();
Object2Address objectAddress = new Object2Address();
TestMethod();
}
MethodObject3()
{
Object3 object = new Object3();
Object3Address objectAddress = new Object3Address();
TestMethod();
}
MethodObject4()
{
Object4 object = new Object4();
Object4Address objectAddress = new Object4Address();
TestMethod();
}
.
.
.
MethodObject100()
{
Object100 object = new Object100();
Object100Address objectAddress = new Object100Address();
TestMethod();
}
But there are around 100 object so I have to create 100 methods in similar way.
So I thought of creating an generic method.
There is a method which returns only the an array of Object name from an XML file.
Eg: ObjectName[] objectName = GetObjects(); // Returns an array of Object Names from XML file so that
objectName[0] = Object1;
objectName[1] = Object2;
objectName[2] = Object3;
objectNmae[3] = Object4;
.
.
.
objectName[99] = Object100;
I am looping through objectName array
foreach(var objectItem in objectName)
{
MethodName<objectItem>();
}
In my generic method I can create an instance of object, like
MethodName<T>()
{
T t = new T();
Some how I have to get the name of T and append Address to T
So that I can create TAddress instance like
TAddress tAddress = new TAddress();
}
Is there any way I can do this using C sharp?
Even with the edits, I still can't follow what you're asking for. However, in an attempt to make forward progress, I'll offer an answer that might at least help isolate what you want.
public class ThingWithAddress<T>
{
public T Item { get; private set; }
public string Address { get; private set; }
public static ThingWithAddress<T> Create(T item, string address)
{
return new ThingWithAddress<T>
{
Item = item,
Address = address,
};
}
}
Then you can get an object that has both a person and address like:
Person person = ...;
var personWithAddress = ThingWithAddress.Create(person, "some address");
Don't take this the wrong way, but you might need to learn enough C# to formulate a good question, since as it stands, the question seems confusing and inconsistent.
Your case doesn't really require a generic method.
What you probably want is to override ToString method in your classes.
class PersonAddress()
{
public override string ToString()
{
return string.Concat(this.Street, ", ", this.City, ", ", this.Postcode);
}
}
class Person()
{
public override string ToString()
{
return string.Concat(this.Name, ", ", this.PersonAddress.ToString());
}
}
Notice how Person class reuses PersonAddress.ToString() method.

Categories