I have to create a json file with an array inside in c# so this is my code but throws an exception:
Method name expected
before new Dictionary<string, int>[2]();
Have you got any suggestions?
int[] lung = results.ToArray();
Dictionary<string, Dictionary<string, int>[]> padre = new Dictionary<string, Dictionary<string, int>[]>();
Dictionary<string, int>[] udemy = new Dictionary<string, int>[2]();
padre.Add("Lunghezza", udemy);
udemy[0].Add("attributo", lung[0]);
udemy[1].Add("attributo2", lung[1]);
string js = JsonConvert.SerializeObject(padre);
System.Diagnostics.Debug.WriteLine(js);
This isn't how you construct an array;
new Dictionary<string, int>[2]()
You don't need the () at all - you're not calling a method or a constructor; you're creating an array instance. Just:
new Dictionary<string, int>[2]
You'll need to populate the array as well, of course, e.g.
udemy[0] = new Dictionary<string, int>();
udemy[1] = new Dictionary<string, int>();
Using a simpler array type makes it easier to see the problem:
// Invalid
int[] x = new int[2]();
// Valid
int[] x = new int[2];
As a side note, a type of Dictionary<string, Dictionary<string, int>[]> is pretty horrible - I would strongly encourage you to redesign that to use more meaningful types.
int[] lung = results.ToArray();
Dictionary<string, Dictionary<string, int>[]> padre = new Dictionary<string, Dictionary<string, int>[]>();
Dictionary<string, int>[] udemy = new Dictionary<string, int>[2]();
// populate
udemy[0].Add("attributo", lung[0]);
udemy[1].Add("attributo2", lung[1]);
// then add
padre.Add("Lunghezza", udemy);
string js = JsonConvert.SerializeObject(padre);
System.Diagnostics.Debug.WriteLine(js);
Related
I have a unique double corresponding to a variation of three strings. I want to populate a dictionary or something such that I can call something like dict[key1][key2][key3] and get the value.
I've tried a whole bunch of things like
Dictionary<string, Dictionary<string, double>> dict = new Dictionary<string, Dictionary<string, double>> {
{ "Foo", {"Bar", 1.2 } },
{ "Foo", {"Test", 3.4 } }
};
Which gives me syntax errors and errors like "Error 4 A namespace cannot directly contain members such as fields or methods"
And
Dictionary<double, Tuple<string, string>> dict = {
{1.23, "Blah", "Foo"}
};
Which gives me errors like "Error 1 Can only use array initializer expressions to assign to array types. Try using a new expression instead."
And
object dict = new Dictionary<string, Dictionary<string, Dictionary<string, string>>>();
dict["k1"] = new Dictionary<string, Dictionary<string, string>>();
dict["k1"]["k2"] = new Dictionary<string, string>();
dict["k1"]["k2"]["k3"] = 3.5;
Which gives me syntax errors and errors like "Error 2 Invalid token '"k1"' in class, struct, or interface member declaration"
How should I go about this? Thanks in advance.
![enter image description here][1]
Edit: Trying Jonesy's code:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
string[] grades = { "Grade 1", "Grade 5", "Grade 8", "ASTM A325", "316 Stainless", "Monel", "Brighton Best 1960" };
string[] sizes = { "#1", "2", "3", "4", "5", "6", "8", "10", "12", "1/4", "5/16", "3/8", "7/16", "1/2", "9/16", "5/8", "3/4", "7/8", "1", "1-1/8", "1-1/4", "1-3/8", "1-1/2" };
var dict = new Dictionary<string, Dictionary<string, Dictionary<string, double>>>();
dict["k1"] = new Dictionary<string, Dictionary<string, double>>();
dict["k1"]["k2"] = new Dictionary<string, double>();
dict["k1"]["k2"]["k3"] = 3.5;
public Form1()
{
InitializeComponent();
}
your last attempt is close, you want:
var dict = new Dictionary<string, Dictionary<string, Dictionary<string, double>>>();
dict["k1"] = new Dictionary<string, Dictionary<string, double>>();
dict["k1"]["k2"] = new Dictionary<string, double>();
dict["k1"]["k2"]["k3"] = 3.5;
you want var instead of object
(or Dictionary<string, Dictionary<string, Dictionary<string, double>>> if you like scrolling)
and your very last string should be a double.
As I understood, you have data and want to perform lookup in it. Why can't you just use some database for that purpose?
But if you really want to hardcode all values, you can. Just don't initialize dictionary manually, make simplifications - parse data in runtime.
Something like this. (I suppose, that you are novice in c# programming, so I've created new Console Application and copy-pasted all the code for your convenience)
public class Program
{
// harcode all data as string
const string RawData =
"k11,k12,k13=3.4;" +
"k21,k22,k23=4.42;" +
"k31,k32,k33=5.91;" +
"k41,k42,k43=8.14;" +
"k51,k52,k53=4.13;" +
"k61,k62,k63=5.4";
static void Main(string[] args)
{
// create dictionary from hardcoded string
var data = ParseData();
// use Tuple as key for data lookup
var value = data[Tuple.Create("k11", "k12", "k13")];
// check, that value equals expected one
Debug.Assert(value == 3.4);
}
private static IDictionary<Tuple<string, string, string>, double> ParseData()
{
var parsedData =
RawData
.Split(';')
.Select(ParseRow)
.ToDictionary(x => x.Item1, x => x.Item2);
return parsedData;
}
private static Tuple<Tuple<string, string, string>, double> ParseRow(string row)
{
var parts = row.Split('=');
var coefficients = ParseCoefficients(parts[0]);
var value = Double.Parse(parts[1], CultureInfo.InvariantCulture);
return Tuple.Create(coefficients, value);
}
private static Tuple<string, string, string> ParseCoefficients(string row)
{
var coeffs = row.Split(',');
var result = Tuple.Create(coeffs[0], coeffs[1], coeffs[2]);
return result;
}
}
As another simplification, you can use custom class as dictionary key instead of nested dictionaries. Write your own(pay attention, that it should override equality members Equals and GetHashCode), or use something from base class library. Tuple<string, string, string> is the perfect one.
I have a ListView and I am passing value to this list using ArrayAdapter.
This is what I did.
first I created string array:
private static readonly string[] menuItemNames = new string[] {
"Home", "New", "Delete", "Submit", "Help"
};
And then created ArrayAdapter:
this.MyMenuList.Adapter = new ArrayAdapter<string> (this, Resource.Layout.item_menu, Resource.Id.MenuItemText, menuItemNames);
And this seems to work fine. The values from menuItemNames array goes into the list and makes a new item. But next to each item, I have an icon.
How Can I set the icon of each one?
This is what I tried:
Drawable sideIcon1 = context.Resources.GetDrawable (Resource.Drawable.home_icon);
Drawable sideIcon2 = context.Resources.GetDrawable (Resource.Drawable.new_icon);
Drawable sideIcon3 = context.Resources.GetDrawable (Resource.Drawable.delete_icon);
Drawable sideIcon4 = context.Resources.GetDrawable (Resource.Drawable.submit_icon);
Drawable sideIcon5 = context.Resources.GetDrawable (Resource.Drawable.help_icon);
Then, made drawable array:
Drawable[] sideMenuItemIconsDrawables = new Drawable[] {
sideIcon1, sideIcon2, sideIcon3, sideIcon4, sideIcon5
};
And then make a new ArrayAdapter:
this.MyMenuList.Adapter = new ArrayAdapter<Drawable> (this, Resource.Layout.item_menu, Resource.Id.ImgView, sideMenuItemIconsDrawables);
This doesn't seem to work.
What do I nee to pass instead of drawable to change the image of that ImageView ?
Thank you for your time.
// ===========================================================
EDIT:
So this is what I tried. But it crashes my app before even the view loads.
The adapter count changes but it just crashes after that.
IList<IDictionary<string, object>> menuItemNames = new List<IDictionary<string, object>> ();
Dictionary<string, object> sideItem1 = new Dictionary<string, object> ();
sideItem1.Add ("text", "SideMenuItem1");
sideItem1.Add ("icon", Resource.Drawable.menu_Icon);
Dictionary<string, object> sideItem2 = new Dictionary<string, object> ();
sideItem2.Add ("text", "SideMenuItem2");
sideItem2.Add ("icon", Resource.Drawable.back_arrow_box);
Dictionary<string, object> sideItem3 = new Dictionary<string, object> ();
sideItem3.Add ("text", "SideMenuItem3");
sideItem3.Add ("icon", Resource.Drawable.bar_addAbsence);
Dictionary<string, object> sideItem4 = new Dictionary<string, object> ();
sideItem4.Add ("text", "SideMenuItem4");
sideItem4.Add ("icon", Resource.Drawable.bar_addExpense);
Dictionary<string, object> sideItem5 = new Dictionary<string, object> ();
sideItem5.Add ("text", "SideMenuItem5");
sideItem5.Add ("icon", Resource.Drawable.bar_addTime);
menuItemNames.Add (sideItem1);
menuItemNames.Add (sideItem2);
menuItemNames.Add (sideItem3);
menuItemNames.Add (sideItem4);
menuItemNames.Add (sideItem5);
string[] sideMenuFromArr = { "text", "icon" };
int[] sideMenuToArr = { Resource.Id.textSideMenuItem, Resource.Id.imgViewSideMenuItemIcon };
SimpleAdapter sideMenuListAdapter = new SimpleAdapter (context, menuItemNames, Resource.Layout.item_menu, sideMenuFromArr, sideMenuToArr);
this.absHomeSideMenuList.Adapter = sideMenuListAdapter;
There are several ways of solving that, to me I think using SimpleAdapter is the simplest way. Make a List of HashMap of string and object. Create your layout, populate your List with the text and icons you wish to use and pass the layout and List to your SimpleAdapter constructor.
Something like:
List<HashMap<String, Object>> menuItemNames = new ArrayList<HashMap<String,Object>>();
HashMap<String, Object> row = new HashMap<String, Object>();
row.put("icon", R.drawable.home_icon);
row.put("text", "Home");
menuItemNames.add(row);
row = new HashMap<String, Object>();
row.put("icon", android.R.drawable.new_icon);
row.put("text", "New");
menuItemNames.add(row);
row = new HashMap<String, Object>();
row.put("icon", android.R.drawable.delete_icon);
row.put("text", "Delete");
menuItemNames.add(row);
int[] to = {R.id.sideMenuItemIconsDrawables, R.id.MenuItemText}; //IDs of your text and icon
String[] from = {"icon", "text"};
adapter = new SimpleAdapter(this, menuItemNames, R.layout.item_menu, from, to);
listView.setAdapter(adapter);
I hope this helps.
I want to add items to a dictionary that contains a dictionary within please refer the code below.
Dictionary<string, Dictionary<string, int>> dict = new Dictionary<string, Dictionary<string, int>>();
foreach (var item in TrainingCourseList)
{
// I have to add item.ID,item.Name,item.Score something like below
dict.Add(item.ID,item.Name,item.Score);
}
I guess Tuple<string, string, int> is the best suit for your case, not Dictionary:
var list = TrainingCourseList
.Select(item => Tuple.Create(item.ID, item.Name, item.Score));
if you want to add an entry to the outer dictionary you have to do something like
Dictionary<string, Dictionary<string, int>> dict = new Dictionary<string, Dictionary<string, int>>();
foreach (var item in TrainingCourseList)
{
// i have to add item.ID,item.Name,item.Score something like below
dict.Add(item.ID,new Dictionary<string,int>{{item.Name,item.Score}};
}
Try
Dictionary<string, Dictionary<string, int>> dict = new Dictionary<string, Dictionary<string, int>>();
foreach (var item in TrainingCourseList)
{
// i have to add item.ID,item.Name,item.Score something like below
dict.Add(item.ID, new Dictionary<string, int> { item.Name, item.Score });
}
How can I copy a Dictionary<string, string> to another new Dictionary<string, string> so that they are not the same object?
Assuming you mean you want them to be individual objects, and not references to the same object pass the source dictionary into the destination's constructor:
Dictionary<string, string> d = new Dictionary<string, string>();
Dictionary<string, string> d2 = new Dictionary<string, string>(d);
"so that they are not the same object."
Ambiguity abound - if you do actually want them to be references to the same object:
Dictionary<string, string> d = new Dictionary<string, string>();
Dictionary<string, string> d2 = d;
(Changing either d or d2 after the above will affect both)
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
Dictionary<string, string> first = new Dictionary<string, string>()
{
{"1", "One"},
{"2", "Two"},
{"3", "Three"},
{"4", "Four"},
{"5", "Five"},
{"6", "Six"},
{"7", "Seven"},
{"8", "Eight"},
{"9", "Nine"},
{"0", "Zero"}
};
Dictionary<string, string> second = new Dictionary<string, string>();
foreach (string key in first.Keys)
{
second.Add(key, first[key]);
}
first["1"] = "newone";
Console.WriteLine(second["1"]);
}
}
A one-line version of Amal's answer:
var second = first.Keys.ToDictionary(_ => _, _ => first[_]);
I need to create a various number of Dictionary<string, string>s in C#; how can I do so?
I've tried using List<Dictionary<string, string>>, but that doesn't work.
See this example:
List<Dictionary<string,string>> list = new List<Dictionary<string,string>>();
Dictionary<string,string> dict1 = new Dictionary<string,string>();
list.Add(dict1);
list.Add(new Dictionary<string,string>());
List<Dictionary<string,string>> does work. Just do it like this:
var d = new List<Dictionary<string,string>>();
var toAdd = new Dictionary<string,string>();
toAdd["Test"] = "Yes";
d.Add(toAdd);
List<Dictionary<string, string>> list = new List<Dictionary<string, string>>();
list.Add(new Dictionary<string, string>());
// list[0].Add(...);
Try
List<Dictionary<string, string>> listDic = new List<Dictionary<string, string>>();
Dictionary<string, string> dic = new Dictionary<string, string>();
Dictionary<string, string> dic1 = new Dictionary<string, string>();
dic.Add("key1", "value1");
dic1.Add("key2", "value2");
listDic.Add(dic);
listDic.Add(dic1);
Try,
List<Dictionary<int, string>> a = new List<Dictionary<int, string>>();
Dictionary<int,string> b = new Dictionary<int,string>();
b.Add(1, "Anwer");
a.Add(b);
for (int i = 0; i < a.Count; i++)
foreach (KeyValuePair<int, string> v in a[i])
Console.WriteLine(v.Key + " ------ " + v.Value);