C# string to var object - c#

I have a string "hello", and a integer 1.
I want to convert them into
new { hello= 1 }
dynamically, and without using any condition like
switch(p1){
case "hello":
return new {hello=p2};
}
as there is many different string and I need to put many items into a super object set like
var emotion = {smile=1,angry=2,worry=3}
the problem is smile, angry and worry was string. but after added to emotion, they are not string, but just an index (like dictionary, however dictionary's index also has dataType, which is not my expected result)
Is it possible?
--- Updated --- i have added a function to specify the expected output.
private void Question_1()
{
//i have
string a = "hello";
int b = 1;
// i want to convert a and b to new {a = b} programmatically, for example i can convert a and b to a Tuple like
Tuple<string, int> x = new Tuple<string, int>(a,b);
//but i dont know how to to convert it to new {a = b}, as i need to put the string "hello" as key to new {a=b}
var result = new { hello = b }; //you can see i can put b after =, but i can never put the string hello at the left
}
private void Question_2()
{
//and the final should be like this
List<Tuple<string, int>> list = new List<Tuple<string, int>>() {
new Tuple<string,int>("smile",1),
new Tuple<string,int>("cry",2),
new Tuple<string,int>("worry",3)
};
foreach (Tuple<string, int> item in list)
{
//adding item's string and int into result and finally the result is
}
//the final result
var finalResult = new { smile = 1, cry = 2, worry = 3 };
}

Use .NET naming conventions for enums: They should be Pascal Notation.
enum Emotion
{
Smile = 1, Angry = 2, Worry = 3
}
var l = new List<Emotion> { Emotion.Angry, Emotion.Smile, Emotion.Worry };
You can also use a friendly name for your enum with the DesriptionAttribute like this:
enum Emotion
{
[Description("Smiling")]
Smile = 1,
[Description("Angry Type")]
Angry = 2,
[Description("Worry Type")]
Worry = 3
}

Any reason you can't just use a dictionary?
var hi = new Dictionary<string,int>();
hi[p1] = p2;
return hi; // Would serialize the same way as your anonymous object
If not, then you could use the expando object to dynamically set properties at runtime.
var hi = new ExpandoObject() as IDictionary<string, object>;
hi.Add(p1, p2);
var p2Value = (int)((dynamic)hi).hello;

You can use ExpandoObject.
class Program
{
static dynamic obj = new ExpandoObject();
static void Main(string[] args)
{
AddProperty("City", "Sydney");
AddProperty("Country", "Australia");
AddProperty("hello", 1);
Console.WriteLine(obj.City);
Console.WriteLine(obj.Country);
Console.WriteLine(obj.hello);
//We can even use dynamic property names ( e.g. cityProp in below example )
IDictionary<string, object> dic = obj as IDictionary<string, object>;
Console.WriteLine("City is : " + dic[cityProp]);
}
public static void AddProperty(string propertyName, object value)
{
IDictionary<string, object> a = obj as IDictionary<string, object>;
a[propertyName] = value;
}
}

dynamic emotion = new { smile = 1, angry = 2, worry = 3 };
Console.WriteLine(emotion.smile);
Like this?
Edit: Based on your comment on another answer:
it is not worked, i need it able to be accepted by
Url.Action("action","controller", x), where x is thing that i'm trying
to create dynamically . i don't know if dynamic too complex or what.
but Url.Action dont know how to read it
There's obviously more to the question than just C#, clearly this an MVC question. You should really add as much information as you can about what you need.
Your answer is probably here:
https://stackoverflow.com/a/15112223/1685167
The #Url.Action() method is proccess on the server side, so you cannot
pass a client side value to this function as a parameter.

Related

separating strings by some text within listbox

I have a ListBox where table names are written like this:
Staging_Section_01_2019_03_19_01
Staging_Section_01_2019_03_20_01
Staging_Section_23_2019_03_21_01
Staging_Section_52_2019_03_23_01
Staging_Section_52_2019_03_24_01
What I am trying to do is to separate them by Section Number, so I want all Section_01 in one List object and Section_23 in another List object, so on and so forth. The dynamic nature is whats making it difficult for me.
So far, I have the following:
foreach (var it in uploadBox.Items)
{
if (it.ToString().Contains("Section"))
{
section = it.ToString().Substring(0, 18);
found = it.ToString().IndexOf("_");
section = section.Substring(found + 1);
sectionNum = section.Substring(8, 2);
}
}
I have gotten the sectionNum which would just be the number and section, which is the string like Section_01.
Any idea on how to approach this?
The expected output would be something like this:
List 1
Staging_Section_01_2019_03_19_01
Staging_Section_01_2019_03_20_01
List 2
Staging_Section_23_2019_03_21_01
List 3
Staging_Section_52_2019_03_23_01
Staging_Section_52_2019_03_24_01
I would use a Dictionary<string, List<string>> for this. Each 'section' that is parsed would be a key, and the remaining portion would the the value.
Dictionary<string, List<string>> myDict = new Dictionary<string, List<string>>();
foreach (var it in uploadBox.Items)
{
if (it.ToString().Contains("Section"))
{
section = it.ToString().Substring(0, 18);
found = it.ToString().IndexOf("_");
section = section.Substring(found + 1);
sectionNum = section.Substring(8, 2);
if(!myDict.ContainsKey(sectionNum))
{
myDict.Add(sectionNum, new List<string> { someOtherValue });
}
else
{
myDict[sectionNum].Add(someOtherValue);
}
}
}
Unless I have completely misinterpreted your question, I think this is a potential solution to your dynamic objects.
you could do something like this:
var sections = new Dictionary<string, List<string>>();
foreach(var it in uploadBox.Items)
{
var item = it.ToString();
if(item.Contains("Section"))
{
var section = GetSection(item);
if(!sections.ContainsKey(section))
{
sections.Add(section, new List<string>());
}
sections[section].Add(item);
}
}
private string GetSection(string item)
{
var split = item.Split("_");
return $"{split[1]}_{split[2]}";
}
It is best to regex for this kind of task:
uploadBox.Items
.GroupBy(x => Regex.Match(x.ToString(), #"^\w+_Section_(?<section>\d+)").Groups["section"].Value)

List of objects with a list as a property of the object

Declaring a list of objects:
List<object> result = new List<object>();
and a list of int to store the ids:
List<int> ids = new List<int>();
I want to store in result objects containing the pair (string, list of int).
It works fine for the pair (string, int) but I want that when there are 2 identical strings to have only one object and the int values to be stored in a list.
ex: {pars = "xxx", id = 1} , {pars = "xxx", id = 2} becomes {pars = "xxx", id = (1,2 )}
For doing the initial functionality, I use a foreach through an object from which I take the string(pars) and the id:
foreach (dataBinding in myData)
{
var x = string.Join(" ", dataBinding.Params.Select(p => p.myDescription));
result.Add(new { pars = x, id = dataBinding.Id });
}
there could be more strings in Params, that's why I use the join.
As it is here it works by creating objects having the form (string, int). But my aim is to make it (string, list of int) and if there are two objects with same string to combine them as I wrote before.
I tried to add ids list as the second property of the object but probably I'm not doing it correctly.
result.Add(new { pars = x, ids = dataBinding.Id });
You can use LINQ, especially GroupBy:
Dictionary<string, List<int>> descriptionIDs = myData
.GroupBy(x => x.myDescription)
.ToDictionary(g => g.Key, g => g.Select(x => x.Id).ToList());
Now you have even a dictionary, not just a strange List<object> that contains anonymous types.
As someone mentioned, you can also use ToLookup which i'd also prefer:
var descriptionLookup = myData.ToLookup(x => x.myDescription);
Now you can get the ID-List easily:
var result = descriptionLookup.Select(g => new { pars = g.Key, ids = g.Select(x=> x.Id).ToList() }).ToList():
Perhaps I am not understanding the scenario fully but I suspect using the following would server your purpose.
List<Dictionary<string, List<int>>>
When the key doesn't exist you add it and when it does you just add to the List.
Below program depicts the current generic collection type, also allow to add a new value if Key Already exists.
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
MyProgram p = new MyProgram();
p.Add("First" , 5);
p.Add("Second" , 8);
p.Add("Third" , 9);
p.Add("First" , 6);
p.Add("First" , 7);
p.PrintDictionary();
}
}
public class MyProgram
{
private Dictionary<string, List<int>> dict = new Dictionary<string, List<int>>();
public void Add(string key, int value)
{
if (dict.ContainsKey(key))
{
dict[key].Add(value);
}
else
{
dict.Add(key, new List<int>() {value});
}
}
public void PrintDictionary()
{
foreach(var keyValue in dict)
{
Console.WriteLine("Key : " + keyValue.Key);
foreach(var val in keyValue.Value)
{
Console.WriteLine(string.Format("\t Value : {0}", val));
}
}
}
}
Output :
Key : First
Value : 5
Value : 6
Value : 7
Key : Second
Value : 8
Key : Third
Value : 9
Check this Live Fiddle.

Is there any way to emulate Javascript Intrinsic Objects in C#

In JavaScript, I can call a function like so:
x = myFunction({"meetingID": 2, "meetingRoom": "A103"});
myFunction() then receives an object which I can parse with JSON, or just by referencing the object's properties:
function myFunction( args ) {
var x = args.meetingID;
}
Is there any such similar construct in C#? Are there such things as "inline objects", or "on the fly objects"?
You can use anonymous type in conjunction with dynamic keyword.
void MyFunction(dynamic args)
{
var x = args.MeetingId;
}
MyFunction(new { MeetingId = 2, MeetingRoom = "A103" });
Beware - this works different from javascript. If object passed to MyFunction doesn't contain property MeetingId, you'll get exception at runtime.
You could use a dictionary
public static void Main()
{
myFunction(new Dictionary<string, object>()
{
{ "meetingID", 2},
{ "meetingRoom", "A103"}
}
);
}
public static void myFunction(Dictionary<string,object> args)
{
var x = args["meetingID"];
}
Just a small suggestion .. if possible, it will be safer to use optional parameters instead:
string myFunction( int meetingID = 0, string meetingRoom = null ) {
var x = meetingID;
}
then you can use it like :
var x = myFunction( meetingID: 2, meetingRoom: "A103" );
var y = myFunction( meetingRoom:"A103" ); // order doesn't matter if you specify the parameter name
var z = myFunction( 3, "B109" );
var _ = myFunction();

Creating objects dynamically in loop

I have an array of strings that I am looping through. I would like to loop through the array and on each iteration, create a new object with a name that matches the string value.
For example;
string[] array = new string[] { "one", "two", "three" };
class myClass(){
public myClass(){
}
}
foreach (string name in array)
{
myClass *value of name here* = new myClass();
}
Would result in three objects being instantiated, with the names "one", "two" and "three".
Is this possible or is there are better solution?
What are you trying to do is not possible in statically-typed language. IIRC, that's possible on PHP, and it's not advisable though.
Use dictionary instead: http://ideone.com/vChWD
using System;
using System.Collections.Generic;
class myClass{
public string Name { get; set; }
public myClass(){
}
}
class MainClass
{
public static void Main()
{
string[] array = new string[] { "one", "two", "three" };
IDictionary<string,myClass> col= new Dictionary<string,myClass>();
foreach (string name in array)
{
col[name] = new myClass { Name = "hahah " + name + "!"};
}
foreach(var x in col.Values)
{
Console.WriteLine(x.Name);
}
Console.WriteLine("Test");
Console.WriteLine(col["two"].Name);
}
}
Output:
hahah one!
hahah two!
hahah three!
Test
hahah two!
While others have given you an alternate but no one is telling why do they recommend you that.
That's because You cannot access object with dynamic names.
(Food for thought: Just think for a moment if you could do so, how will you access them before they are even coded/named.)
Instead create a Dictionary<string, myClass> as others mentioned.
Use a Dictionary<String, myClass> instead:
var dict= new Dictionary<String, myClass>();
foreach (string name in array)
{
dict.Add(name, new myClass());
}
Now you can access the myClass instances by your names:
var one = dict["one"];
or in a loop:
foreach (string name in array)
{
myClass m = dict[ name ];
}
You can use this approach:
var array = [srt1, srt2, str3];
var other_array = [];
for(var i = 0; i < array.length; i++){
other_array.push({
name: array[i]
})
}
And for lookup it is easy to find the instance you need by filtering:
var instance1 = other_array.filter(function(result) {
return result.name == 'str1';
});
Looks like you need a list of dictionary of your objects:
var myClassDictionary = new Dictionary<string,myClass>();
foreach (string name in array)
{
myClassDicationry.Add(name, new myClass());
}
// usage:
// myClass["one"] <- an instance of myClass
There are no programming languages that I know of that let you define variable names in runtime.
You could do something like this -
Dictionary<string, myClass> classes = new Dictionary<string, myClass>();
foreach(string name in array)
{
classes[name] = new myClass();
}
Then you can refer to the named instances later
classes[name].MyClassMethod();
You can use the following code.
string[] array = new string[] { "one", "two", "three" };
Dictionary<String, myClass> list;
class myClass(){
public myClass(){
list = new Dictionary<String, myClass>();
}
}
foreach (string name in array)
{
list.Add(name, new myClass())
}
You can use lists to store the objects so you can access them
list<myClass> myClasses = new List<myClass>();
foreach (myClass object in myClasses)
{
//preform interaction with your classes here
}
Not applicable to C#, or any statically-typed language for that matter.
For curiosity, I tried if what I remembered in PHP(creating variables on-the-fly) is still correct.
It's still the same PHP, last I used it was year 2000. You can generate variables on-the-fly, not saying it's advisable though, it pollutes the global variables, it can corrupt some existing variable or object with same name.
https://ideone.com/nJDiou
<?php
class MyClass
{
private $v;
function __construct($x) {
$this->v = $x;
}
public function getValue() {
return $this->v;
}
}
$one = new MyClass("I'm tough!");
echo "The one: " . $one->getValue() . "\n";
$i = 0;
foreach(array("one","two","three") as $h) {
$$h = new MyClass("Says who? " . ++$i);
}
echo "The one: " . $one->getValue() . "\n";
echo $two->getValue() . "\n";
echo $three->getValue() . "\n";
echo "loop\n";
foreach(array("three","one","two") as $h) {
echo $$h->getValue() . "\n";
}
?>
Outputs:
The one: I'm tough!
The one: Says who? 1
Says who? 2
Says who? 3
loop
Says who? 3
Says who? 1
Says who? 2

C#: Adding data to dictionary

I have a list like
List<string> TempList = new List<string> { "[66,X,X]", "[67,X,2]", "[x,x,x]" };
I need to add data to the dictionary from the above list
Dictionary<int, int> Dict = new Dictionary<int, int>();
so the Dict should contain
Key --> 66 value --> 67
i need to take 66(first value) from first string([66,X,X]) and 67(first value) from second string( [67,X,X]) and add it as a key value pair into the dictionary.
Now i'm following string replacing and looping methodology to do this .
Is there any way to do this in LINQ or Regular expression.
After your comment that you're starting from a list of lists, I understood what you were after. I'm reusing Jaroslav's 'GetNumber' function here. Wrote my sample with array of array of string, but should work just the same. The code below will throw if you have duplicate keys, which I presume is what you want if you're using a dictionary.
var input = new []
{
new [] { "[66,X,X]", "[67,X,2]", "[x,x,x]" },
new [] { "[5,X,X]", "[8,X,2]", "[x,x,x]" }
};
var query = from l in input
select new
{
Key = GetNumber(l.ElementAt(0)),
Value = GetNumber(l.ElementAt(1))
};
var dictionary = query.ToDictionary(x => x.Key, x => x.Value);
Here is an example using both string.Split() and a Regex:
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<string> data = new List<string>() { "[66,X,X]", "[67,X,2]", "[x,x,x]" };
addToDict(data);
Console.ReadKey();
}
private static void addToDict(List<string> items)
{
string key = items[0].Split('[', ',')[1];
string val = items[1].Split('[', ',')[1];
string pattern = #"(?:^\[)(\d+)";
Match m = Regex.Match(items[0], pattern);
key = m.Groups[1].Value;
m = Regex.Match(items[1], pattern);
val = m.Groups[1].Value;
_dict.Add(key, val);
}
static Dictionary<string, string> _dict = new Dictionary<string, string>();
}
}
i suspect that your example is quite contrived though, so there may be a better solution especially if you need to process large numbers of strings into key/value pairs (i deliberately hardcoded index values because your example was quite simple and i didn't want to over complicate the answer). If the input data is consistent in format then you can make assumptions like using fixed indexes, but if there is a possibility of some variance then there may need to be more code to check the validity of it.
You can use a regular expression to extract the value from each item in the list, and if you want, use LINQ to select out two lists and zip them together (in C# 4.0):
var regex = new Regex(#"\d+");
var allValues = TempList.Select(x =>int.Parse(regex.Match(x).Value));
var dictKeys = allValues.Where((x,index)=> index % 2 == 0); //even-numbered
var dictValues = allValues.Where((x,index)=> index % 2 > 0); //odd numbered
var dict = dictKeys.Zip(dictValues, (key,value) => new{key,value})
.ToDictionary(x=>x.key,x=>x.value);
If you're using C# 3.5, you can use Eric Lippert's implementation of Zip().
IF I understand correctly: you want to create linked nodes like 66 -> 67, 67 -> 68, ... n -> n+1?
I would not use LINQ:
private static int GetNumber(string s)
{
int endPos = s.IndexOf(',');
return Int32.Parse(s.Substring(1, endPos-1));
}
And in code:
int first, second;
for (int i = 1; i < TempList.Count; i++)
{
first = GetNumber(TempList[i - 1]);
second = GetNumber(TempList[i]);
Dict.Add(first, second);
}
You should also perform checking, etc.
The sample assumes a list with at least 2 items.
List<List<string>> source = GetSource();
Dictionary<int, int> result = source.ToDictionary(
tempList => GetNumber(tempList[0]),
tempList => GetNumber(tempList[1])
);

Categories

Resources