CustomeProfile class, list of key-value pair is not saving - c#

public class KeyValue : ProfileBase
{
public KeyValue()
{ }
public List<KeyValuePair<string, string>> pair
{
get { return (List<KeyValuePair<string,string>>)base["pair"]; }
set { base["pair"] = value; }
}
public string Name
{
get { return base["Name"] as string; }
set { base["Name"] = value; }
}
}
Whats the problem in this CustomProfile class.I am saving the list of key-value pairs but when i am accessing it show null in each pair, but it gives name correctly

Related

AsParallel writing to same object

I have a Dictionary with Key-Value pair where Value is a complex Object.
Becaouse of a speed I have used AsParalle().
Problem that occures is that in some of my return objects are placed multiple results of other Key-Value pair.
For examle:
ResutDict has multiple Key-Value pairs.
public IDictionary<string, IOutboundResult> ResultDict
{
get { return m_ResultDict; }
set { m_ResultDict = value; }
}
ResultDict.AsParallel()
.WithDegreeOfParallelism(amountOfThreads)
.ForAll(kvp => { DO YOUR THING AND RETURN OBJECT S_Out}
All of them should return one Object. That later will be serialized into XML.
public class S_Out : ISerializable
{
private List<details> detailField;
[System.Xml.Serialization.XmlElement("details")]
public List<details> details
{
get { return detailField; }
set { detailField = value; }
}
}
public class details
{
private string idField;
private string countryField;
private string town_cityField;
private string postcodeField;
private List<extract> extractField;
public string id
{
get { return idField; }
set { idField = value; }
}
public string country
{
get { return countryField; }
set { countryField = value; }
}
public string town_city
{
get { return town_cityField; }
set { town_cityField = value; }
}
public string postcode
{
get { return postcodeField; }
set { postcodeField = value; }
}
[XmlElement("extract")]
public List<extract> extract
{
get { return extractField; }
set { extractField = value; }
}
}
This list of details is there only for some other cases, but in this particular case should contain only one detail.
During Parallel different Threads write in there so in some files I have no detail object, and in some of them whole list of details.
It should be one detail for one xml file.
Have anyone Idea why is this happening?

Define Dictionary with prepoluated keys c#

I have a class Properties and in that I have defined a dictionary like this :
public class Properties
{
public IDictionary<string, string> ExtendedProperties
{
get;
set;
}
}
In the dictionary, there will be 3 keys always present say Name,Number and Age with the option of adding more KeyValuePairs during runtime.
I want to have the 3 keys described above to be present in the dictionary by default while initializing it in my code so that I can use it directly like this:
Properties objProps = new Properties();
objProps.ExtendedProperties["Name"] = "SomeName";
I know I can achieve this in my code by adding the KeyValuePair to the dictionary but I was hoping to have it setup directly in the class using get-set to include the 3 keys. I could not find any solution that does that in the class itself. I looked into this Creating dictionaries with predefined keys but did not find it satisfactory.
how can I achieve this?
Since C# 6 you can do something like:
using System;
using System.Collections.Generic;
public class Properties
{
public IDictionary<string, string> ExtendedProperties { get; set; }
public Properties(string name, string number, string age)
{
this.ExtendedProperties = new Dictionary<string, string>()
{
["Name"] = name,
["Number"] = number,
["Age"] = age
};
}
}
As you can see you need to define it in the constructor.
There is also some cool function you might want to use:
public int this[int param]
{
get { return array[param]; }
set { array[param] = value; }
}
Documentation
If you add something like this, you can do new Properties()["Name"]
Example with your code:
using System;
using System.Collections.Generic;
public class Properties
{
private IDictionary<string, string> extendedProperties;
public string this[string key]
{
get { return extendedProperties[key]; }
set { extendedProperties[key] = value; }
}
public Properties()
{
this.extendedProperties = new Dictionary<string, string>()
{
["Name"] = "something",
["Number"] = "something",
["Age"] = "something"
};
}
}
Like this:
public class Properties
{
public IDictionary<string, string> ExtendedProperties
{
get;
set;
}
public Properties()
{
this.ExtendedProperties = new Dictionary<string, string>()
{
{ "Name", String.Empty },
{ "Number", String.Empty },
{ "Age", String.Empty },
};
}
}
You might want to have a look at some documentation: https://msdn.microsoft.com/en-us/library/bb531208.aspx
How about adding the 3 entries in the constructor?
using System;
using System.Collections.Generic;
namespace My.Namespace
{
public class Properties
{
public IDictionary<string, string> ExtendedProperties { get; set; }
public Properties()
{
ExtendedProperties = new Dictionary<string, string>
{
["Name"] = String.Empty,
["Number"] = String.Empty,
["Age"] = String.Empty
};
}
}
}
You can do this.
public class Properties
{
public IDictionary<string, string> ExtendedProperties
{
get;
set;
}
public Properties(string [] fields)
{
ExtendedProperties = new Dictionary<string, string> ();
foreach(var s in fields)
{
ExtendedProperties.Add(s,string.Empty);
}
}
}
Usage :
Properties p = new Properties(new [] {"Name","Number", "Age"});
Working fiddler code
I would go with implementing IDictionary<string, string> because it is more safe and easier to extend with other keys: (long class to follow)
class Properties : IDictionary<string, string>
{
private Dictionary<string, string> _staticProps;
private Dictionary<string, string> _otherProps;
public Properties()
{
_staticProps = new Dictionary<string, string>
{
{"Name", "" },
{"Number", "" },
{"Age", "" }
};
_otherProps = new Dictionary<string, string>();
}
public ICollection<string> Keys
{
get
{
return (ICollection<String>)_otherProps.Keys.Concat(_staticProps.Keys);
}
}
public ICollection<string> Values
{
get
{
return (ICollection<String>)_otherProps.Values.Concat(_staticProps.Values);
}
}
public int Count
{
get
{
return _otherProps.Count + _staticProps.Count;
}
}
public bool IsReadOnly
{
get
{
throw new NotImplementedException();
}
}
public string this[string key]
{
get
{
if (_otherProps.ContainsKey(key))
{
return _otherProps[key];
}
if(_staticProps.ContainsKey(key))
{
return _staticProps[key];
}
throw new KeyNotFoundException(key);
}
set
{
if (_otherProps.ContainsKey(key) || _staticProps.ContainsKey(key))
{
throw new ArgumentException("key exists: " + key);
}
_otherProps[key] = value;
}
}
public bool ContainsKey(string key)
{
return _otherProps.ContainsKey(key) || _staticProps.ContainsKey(key);
}
public void Add(string key, string value)
{
_otherProps.Add(key, value);
}
public bool Remove(string key)
{
if (_staticProps.ContainsKey(key))
{
throw new ArgumentException("key is static, cannot be removed: " + key);
}
return _otherProps.Remove(key);
}
public bool TryGetValue(string key, out string value)
{
return _otherProps.TryGetValue(key, out value) || _staticProps.TryGetValue(key, out value);
}
public void Add(KeyValuePair<string, string> item)
{
if (_staticProps.ContainsKey(item.Key))
{
throw new ArgumentException("key exist an is static: " + item.Key);
}
_otherProps.Add(item.Key, item.Value);
}
public void Clear()
{
_otherProps.Clear();
foreach (var key in _staticProps.Keys)
{
_staticProps[key] = string.Empty;
}
}
public bool Contains(KeyValuePair<string, string> item)
{
return _otherProps.Contains(item) || _staticProps.Contains(item);
}
public void CopyTo(KeyValuePair<string, string>[] array, int arrayIndex)
{
// define yourself how you want to handle arrayIndex between the two dictionaries
}
public bool Remove(KeyValuePair<string, string> item)
{
if (_staticProps.ContainsKey(item.Key))
{
throw new ArgumentException("key is static, cannot be removed: " + item.Key);
}
return _otherProps.Remove(item.Key);
}
public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
{
return _otherProps.Concat(_staticProps).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return _otherProps.Concat(_staticProps).GetEnumerator();
}
}
I would encapsulate this logic in property itself:
public class Properties
{
private IDictionary<string, string> _extendedProperties;
public IDictionary<string, string> ExtendedProperties
{
get
{
return
_extendedProperties == null ?
new Dictionary<string, string>() { { "Name", "" }, { "Number", "" }, { "Age", "" } } :
_extendedProperties;
}
set
{
_extendedProperties = value;
//here you can also check if value misses those key to add them to _extendedProperties
}
}
}

Custom Collection/List To Store Custom Objects

I have a customer object class:
public class customerObject
{
private string _address1;
private string _address2;
private string _address3;
private string _category;
private string _country;
private string _county;
private string _custcode;
private string _fullname;
private string _int_rep_hou;
private string _int_rep_key;
private double _lat;
private double _lng;
private string _postcode;
private string _rep_code;
private string _telephone;
public customerObject()
{
}
public string Address1
{
get { return _address1; }
set { _address1 = value; }
}
public string Address2
{
get
{
return _address2;
}
set { _address2 = value; }
}
public string Address3 { get { return _address3; } set { _address3 = value; } }
public string Category
{
get { return _category; }
set { _category = value; }
}
public string Country { get { return _country; } set { _country = value; } }
public string County { get { return _county; } set { _county = value; } }
public string Custcode
{
get { return _custcode; }
set { _custcode = value; }
}
public string Fullname
{
get { return _fullname; }
set { _fullname = value; }
}
public string Int_rep_hou
{
get { return _int_rep_hou; }
set { _int_rep_hou = value; }
}
public string Int_rep_key
{
get { return _int_rep_key; }
set { _int_rep_key = value; }
}
public double Lat { get { return _lat; } set { _lat = value; } }
public double Lng { get { return _lng; } set { _lng = value; } }
public string Postcode { get { return _postcode; } set { _postcode = value; } }
public string Rep_code
{
get { return _rep_code; }
set { Rep_code = value; }
}
public string Telephone { get { return _telephone; } set { _telephone = value; }
}
}
I have a CustomCollections class
public class CustomerCollection
{
public List<customerObject> Customers { get; set; }
}
My method that loops through dt rows and converts to a customer object
public List<Valueobjects.CustomerCollection> dolist(DataTable temptablename)
{
//Create Collection Object
Valueobjects.CustomerCollection Collection = new Valueobjects.CustomerCollection();
foreach (DataRow row in temptablename.Rows)
{
//Create Customer Object
Valueobjects.customerObject Customer = new Valueobjects.customerObject();
//set values of customer object
Customer.Rep_code = "";
Customer.Int_rep_key = "";
Customer.Int_rep_hou = "";
Customer.Fullname = row["Fullname"].ToString().Trim();
Customer.Custcode = row["Custcode"].ToString().Trim();
Customer.Category = row["Category"].ToString().Trim();
Customer.Address1 = row["Address1"].ToString().Trim();
Customer.Address2 = row["Address2"].ToString().Trim();
Customer.Address3 = row["Address3"].ToString().Trim();
Customer.Postcode = row["Postcode"].ToString().Trim();
Customer.Country = row["Country"].ToString().Trim();
Customer.Telephone = row["Telephone"].ToString().Trim();
Customer.Lat = Convert.ToDouble(row["Lat"]);
Customer.Lng = Convert.ToDouble(row["Lng"]);
Customer.County = row["County"].ToString().Trim();
//add to the collection (list)
Collection.Customers.Add(Customer);
}
temptablename = null;
return Collection;
}
However when I create a new Customer object and a new CustomerCollection object I am getting an error when adding the customer to the collection list.
Error:
Error 32 Cannot implicitly convert type
'Classes.Valueobjects.CustomerCollection' to
'System.Collections.Generic.List'
Your method is returning a List<CustomerCollection>:
public List<Valueobjects.CustomerCollection> dolist(DataTable temptablename)
{
//...
}
But the code is trying to return a CustomerCollection:
return Collection;
Just as the error says, these two types are different.
If a CustomerCollection is already a collection of customers, then semantically what is a List<Valueobjects.CustomerCollection>? A collection of collections? It seems like you're over-pluralizing your objects :)
There are two approaches here. Either return a CustomerCollection from the method:
public CustomerCollection dolist(DataTable temptablename)
{
//...
}
Or use a List<Customer> if you want to use generic lists as your collection containers:
public List<Customer> dolist(DataTable temptablename)
{
//...
var Collection = new List<Customer>();
//...
Collection.Add(Customer);
//...
return Collection;
}
Side note: You may want to stick to C# conventions for variable naming. As you can see from the code highlighting here on Stack Overflow, your variable names can easily be mistaken for classes/types, which can cause confusion when supporting the code.
Return a CustomerCollection instead of a List<Valueobjects.CustomerCollection>:
public Valueobjects.CustomerCollection Dolist(DataTable temptablename)
{
// ...
Your object has a list, it is not a list.
MSDN: Inheritance

Dictionary<dynamic, dynamic> Value items can't be accessed. C#

I'm playing around with Dictionaries and the new fancy 4.0 dynamic types inside a dictionary.
I have a Dictionary:
Dictionary<dynamic, dynamic> dynamicDic
And I populate it like this:
dynamicDic.Add("First", new Class1());
dynamicDic.Add("Second", new Class2());
For the sake of testing/practising Class1 and Class2 are quite simple:
public class Class1
{
public string Element { get; set; }
public List<Class2> Class2 { get; set; }
}
public class Class2
{
public string Property { get; set; }
public string Field;
}
I create two other classes that map class1 and class2 and they are virtually the same so ClassMap1 and ClassMap2. I'll just include CalssMap1 though:
public class ClassMap1: BaseClassMap1
{
public ClassMap1()
{
var r = new Class1();
Children = new Dictionary<string, dynamic>
{
{"Element", r.GetType().GetProperty("Element")},
{"Class1", r.GetType().GetProperty("Class1")}
};
Name = "Root";
ObjectType = typeof (Class1);
Parent = "RootElement";
HasParent = false;
HasChildren = true;
IsClass = r.GetType().IsClass;
}
}
And I create a base class: BaseClass1()
public class BaseClass1
{
private String _Name;
public String Name
{
get { return _Name; }
set { _Name = value; }
}
private Type _ObjectType;
public Type ObjectType
{
get { return _ObjectType; }
set { _ObjectType = value; }
}
private String _Parent;
public String Parent
{
get { return _Parent; }
set { _Parent = value; }
}
private Dictionary<string, dynamic> _Children;
public Dictionary<string, dynamic> Children
{
get { return _Children; }
set { _Children = value; }
}
private bool _HasParent;
public bool HasParent
{
get { return _HasParent; }
set { _HasParent = value; }
}
private bool _HasChildren;
public bool HasChildren
{
get { return _HasChildren; }
set { _HasChildren = value; }
}
private bool _IsClass;
public bool IsClass
{
get { return _IsClass; }
set { _IsClass = value; }
}
}
I populate the classes with data, not really important what data :)
Yet when I try to access the values through a Linq statement:
var a = _classObjects.SingleOrDefault(x => x.Key == node.Name).Value;
a only gives me:-
a.Equals(), a.GetType(), a.GetEnumerator() or a.ToString()
I would like to be able to have it do this instead (with intellisense)...
a.Children
a.Name
a.HasParent
etc...
Anyone got any ideas where I'm going wrong?
Oops got that completely wrong... Sorry :|
Edited above...
dynamic classes are all about run-time (NOT compile-time) discovery. How do you expect Intellisense to know what to do?
Using dynamic classes incurs a high performance overhead. I really suggest that you make it: Dictionary<string, dynamic> dynamicDic.
Or define a MyBaseClass and make it: Dictionary<string, MyBaseClass> myDic.

Convert a class into an array

I have a DisplayedData class ...
public class DisplayedData
{
private int _key;
private String _username;
private String _fullName;
private string _activated;
private string _suspended;
public int key { get { return _key; } set { _key = value; } }
public string username { get { return _username; } set { _username = value; } }
public string fullname { get { return _fullName; } set { _fullName = value; } }
public string activated { get { return _activated; } set { _activated = value; } }
public string suspended { get { return _suspended; } set { _suspended = value; } }
}
And I want to to put the objects from this class into an array where all objects inside of this class should be converted into an String[]
I have..
DisplayedData _user = new DisplayedData();
String[] _chosenUser = _user. /* Im stuck here :)
or can I create an array where all the items inside are consist of variables of different datatype so that the integer remains an integer and so the strings too?
You can create an array "with your own hands" (see Arrays Tutorial):
String[] _chosenUser = new string[]
{
_user.key.ToString(),
_user.fullname,
_user.username,
_user.activated,
_user.suspended
};
Or you could use Reflection (C# Programming Guide):
_chosenUser = _user.GetType()
.GetProperties()
.Select(p =>
{
object value = p.GetValue(_user, null);
return value == null ? null : value.ToString();
})
.ToArray();

Categories