Filter dictionary through properties [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a dictionary that I would like to pass externally through a property but I would like to limit some features like the clear method. Here's the code that i've implemented.
// dictionary private declaration inside the class
private Dictionary<int, string> _value = new Dictionary<int, string>();
// here's the property declaration for access the dictionary outside the class
public new Dictionary<int, string> Value
{
get
{
return _value;
}
set
{
// here is where i'd like to avoid some dictionary features like clear() and give only the opportunity to add or change existing values
_value = value;
}
}
It’s possible through the “set” of this property to limit some of the features of the dictionary? Maybe using a switch or an if statement?

There are a ReadonlyDictionary that acts as a wrapper around a dictionary, and a corresponding IReadOnlyDictionary interface.
If you are on .Net core 3 there is also an ImmutableDictionary if the dictionary never needs to be changed.
If this is to coarse grained, you can always create your own wrapper that works however you want it to.

Related

Using var as global in C# unity [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
void Convert(string word)
{
var dictionary = (JObject)JsonConvert.DeserializeObject(dictionaryJson.text);
if(dictionary[word] != null)
{
dictionary[word].ToObject<VocabularyModel>();
// do something;
}
else
{
// do something;
}
}
I am using (JObject)JsonConvert.DeserializeObject to convert json to var dictionary. Everything works fine. However I only have 1 json file and every time a use the function I have to:
var dictionary = (JObject)JsonConvert.DeserializeObject(dictionaryJson.text);
Is there any way to call that line only one and the var dictionary is saved as global variable or something similar that help me call the above command only one time only.
And one other question: does the dictionary[word] works like a loop in an array? Does it go through the dictionary one by one and search for the key "[word]" ?
You can move the declaration of into a class-level scope.
class WhatEverYourClassNameIs
{
private var dictionary;
// Deserializing in constructor, but you can do it elsewhere
WhatEverYourClassNameIs()
{
dictionary = (JObject)JsonConvert.DeserializeObject(dictionaryJson.text);
}
void Convert(string word)
{
// This function and all other functions within your class will have access to dictionary
}
}
Regarding your second question, I don't have a concrete answer since the documentation doesn't say so. A normal .NET dictionary has O(1) indexing complexity, meaning that it doesn't take longer the more elements you have in the dictionary. If you want to be certain you'd have to look up the source code and see how indexing is accomplished in the JObject class

How do you store C# Property in SQL Server database and invoke command? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I would assume it wouldn't be complicated to do the following:
I have a VARCHAR database field that will store a C# property such as "DateTime.Now.Year". I want to pass this value into my ASP.NET application and dynamically return 2017.
How do I read the VARCHAR value as a string and get the C# method to invoke the property?
You could achieve this using System.Reflection, but you really need to store information about the assembly, class and property/field/method you want to invoke.
for example if you stored:
AssemblyPath: "c:\something\someassembly.dll"
ClassFullPath: "SomeAssembly.SomeNameSpace.SomeClass, SomeClass"
MethodName: "someMethodName"
Then in your code you could attempt to load the assembly and instantiate the class:
var assembly = Assembly.Load(assemblyPath);
var clss = Activator.CreateInstance(ClassFullPath);
var method = clss.GetType().GetMethod(MethodName);
var result = method.Invoke();
Now that is a super simplified example, and there are many things to consider like access to the method / property / field etc. (i.e. private vs public .. and static vs instance)
So if you can really reduce your cases and design a set of flags / options you can store in your table, it will be possible.

Access part of the list in c# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have this two list here:
List<KeyValuePair<TextBox, KeyValuePair<string, Type>>> textbox1
So, i need that to get the Textbox i need to write:
textbox1.Key
What should i type to get the KeyValuePair<string, Type>> Type, like textbox1.Value.Value?
The type contains string or int.What i need is to access it's value so i can assign an if operator but i don't know how to. Then i need to modify it but that's my next step and i can arrange that myself.
I suppose textbox1.Key won´t compile since textbox1 is a List and therefor does not have any member called Key. Having said this the following may work for you:
textbox[i].Value.Value
Where i is the index of your KV-pair within the list (rather then the actual key)

best key, value container where key is string and value is type [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
What is the suggested key, value container where the key is always of type string, and the value is a type? The container will not be larger than 100 items. Thanks!
You should definitely go with standard Dictionary<TKey, TValue>. The only improvement you can make is based on your there will be no more than 100 items condition.
Use new Dictionary<string, MyType>(int capacity) constructor when creating dictionary, it will prevent underlying storage from reallocating when new items are added.
var dict = new Dictionary<string, MyType>(100);
I have trouble suggesting anything other than the standard .net dictionary if you need concurrency there is also a ConcurrentDictionary

Object or Tuple? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I was searching for a solution for the problem I'm having in my project.
In the link below I found something usefull:
One Key to multiple values dictionary in C#?
However: isn't it better to use a Object in the dictionary instead of using a Tuple??
Object obj = new Object();
var dict = new Dictionary<KeyType, obj>();
foreach(KeyType kt in dict)
{
obj.X = value1;
obj.Y = value2;
}
Instead of:
var dict = new Dictionary<KeyType, Tuple<string, int, int, int>>()
I hope that there is someone who could help me. Thanks :)
You cannot do obj.X in C#, if obj is of type Object. But you can create a custom class derived from Object, which contains all the field and properties you need and use it inside the dictionary.
This would indeed be superior to the Tuple solution, since myObj.Speed is much more descriptive than myTuple.Item2.
Note, however, that Tuples have some features that your custom class might not have, for example, tuple1.Equals(tuple2) returns true if both have the same elements. With your custom class, Equals will check for reference equality, unless you override it.
No, it is not better to use object - you loose your strong typing.
If you are concerned about the verbosity of your code, you can always do something like this:
At the top of your document:
using MyDictionary = Dictionary<KeyType, Tuple<string, int, int, int>>;
Then your constructors do this:
var dictionary = new MyDictionary();
Instead of using Tuple I prefer to make a simple class with two/three fields. It's much cleaner solution and it's easier to use.

Categories