Getting Hashtable keys in intellisense - visual-studio-2010

I am using hashtable that contains around 60 key,value pairs.
for assigning the value based on key to any control in my page i have to explicitly type the keyname.
For example:
txtName.Text = htData["Name"].ToString();
txtAddress.Text = htData["Address"].ToString();
Doing this same thing for 60 values is time taking and inefficient.
is there a way to directly get the key and then set the value.
Just like intellisense in vs.
For example:
txtName.Text = htData.Name.Value.ToString();
txtAddress.Text = htData.Address.Value.ToString();

If you are stuck with the Hashtable there is not much you can do. I would suggest switching to the Dictionary class.
You could then make the Key an Enumeration:
public enum DataType
{
Name,
Adress,
...
}
Or you could make an Object with 60 Properties, without using a Dictionary.
If you are stuck with the Hashtable, you could still use an Enumeration, with ToString().
A Hashtable will never provide "nice" code though.

Related

Initialize non existing keys to empty array list

I have a class :
class Con {
private List<Ind> inds;
}
I am using Gson in the usual way to convert a JSON string to this class object. so in case, the JSON doesn't have the key inds present this variable inds is assigned a null value. Is there a way to assign inds an empty ArrayList instead?
My Thoughts:
One straightforward way could be once the Gson object is built. Go over all the null objects and assign them to the new ArrayList<>(). Is there a better approach?
public List<Ind> getInds() {
return inds;
}
Currently I am using the above getter in a code like : con.getInds().stream() which is causing NullPointerException.
I am not sure what would be a good way to resolve this. Instead of List Should I return an Optional or Should I modify this getter like
public List<Ind> getInds() {
inds==null?new ArrayList<>():inds;
}
The above will also resolve the NullPointerException. Not sure if there are pros and cons to using this approach. Although now there is no way to identify if the Json has a key with name inds or not. For the current code that I am writing this may not be required. But there is a meaning loss here certainly.
One solution to this would be to assign default values to the fields, for example:
class Con {
private List<Ind> inds = new ArrayList<>();
}
Gson will keep this default value; only if the field is present in the JSON data it will reassign the field value.
There are however a few things to keep in mind:
Your class needs a no-args constructor (implicit or explicit); otherwise Gson might create instances without invoking the initializer blocks of the class, and therefore the field will be null
If the field is present in JSON but has a JSON null value, then Gson will still set that as value
You cannot tell afterwards whether the field was present in JSON but had an empty JSON array as value, or whether it was missing

For/in probem. What is the difference between object.variable and object[variable]?

Why can we use a code like this:
let student = {name:"John", surname:"Doe", index:386754};
let text = "";
let x;
for (x in student) {
text += student[x] + " "; }
And it would preview: John Doe 386754.
But when I formulated it like this:
let student = {name:"John", surname:"Doe", index:386754};
let text = "";
let x;
for (x in student) {
text += student.x + " "; }
, it returnes: undefined undefined undefined.
I suppose it's a pretty basic thing, but I had to ask because I can't find an appropriate answer.
Thank you ahead!
You should check out the data structures. You create a hash table using the variable student. So you can call inner variables (key-value pairs) by using brackets as you did student[name]. The second one student.name means you are calling a method of a class, which you don't have.
I recommend you to check what data structures exist, and how to use them.
The usage of object.something vs object[something] varies in different languages, and JavaScript is particularly loose in this aspect. The big difference here is that in object[something], something must reference a string corresponding to a key in object. So if you had something = 'myKey', and myKey was the name of a key in something (so object = {'myKey': 'value', ...}), you would get value. If you use object.something, you are asking JavaScript to look for a key in object with the name something. Even if you write something = 'myKey', using a dot means that you are looking within the scope of the object, making variables in your program effectively invisible.
So when you write student.x, you get undefined because there is no key 'x': 'value' in student for your program to find. Defining x as a key in your for loop does not change this. On the other hand, writing student[x] means that your program is finding the value x is referencing and plugging it in. When x is 'name', the program is actually looking for student['name'].
Hope that clarifies your issue. Good luck!

Elixir - ETS with custom equals/hash function

In Java, the equals/hash functions can be customised simply by overriding/implementing methods on a class.
This is very useful when you want to customise uniqueness of your class - so that you can check for 'duplicates' in a set easily.
How would you do the same thing in Elixir, specifically with ETS?
One way to do what I need to do is by making a unique hash function (that can return any type). There should only be one unique output of this hash function per unique input.
Then you can store the {hash, val} tuples:
table = :ets.create(:table, [])
:ets.insert(table, {hash(val), val})
:ets.lookup(table, hash(val))

Swift - Working with Dictionaries - Add multiple values

I've been looking for answers to this problem, but unfortunately without success.
I'm developing a mathematical app (Swift-based), which keeps data of every function the user enters.
(I then need to draw the functions on an NSView using a Parser)
The data structure is saved into a Dictionary but I'm not able to add values and keys.
The Dictionary is initialized like:
var functions = [String : [[String : NSBezierPath], [String : NSColor], [String : CGFloat], [String : Bool]]]();
//1A.The String key of the main Dictionary is the value of the function, such as "sin(x)"
//1B.The value of the `Dictionary` is an `Array` od `Dictionaries`
//2.The first value is a dictionary, whose key is a String and value NSBezierPath()
//3.The second value is a dictionary, whose key is a String and value NSColor()
//4.The third value is a dictionary, whose key is a String and value CGFloat()
//5.The first value is a dictionary, whose key is a String and value Bool()
To add the functions, I have implemented a method (I will report a part of) :
...
//Build the sub-dictionaries
let path : [String:NSBezierPath] = ["path" : thePath];
let color : [String:NSColor] = ["color" : theColor];
let line : [String:CGFloat] = ["lineWidth" : theLine];
let visible : [String:Bool] = ["visible" : theVisibility];
//Note that I'm 100% sure that the relative values are compatible with the relative types.
//Therefore I'm pretty sure there is a syntax error.
//Add the element (note: theFunction is a string, and I want it to be the key of the `Dictionary`)
functions[theFunction] = [path, color, line, visible]; //Error here
...
I'm given the following error:
'#|value $T10' is not identical to '(String,[([String:NSbezierPath],[String : NSColor],[String : CGFloat],[String : Bool])])'
I hope the question was enough clear and complete.
In case I will immediately add any kind of information you will need.
Best regards and happy holidays.
Dictionaries map from a specific key type to a specific value type. For example, you could make your key type String and your value type Int.
In your case, you’ve declared quite a strange dictionary: a mapping from Strings (fair enough), to an array of 4-tuples of 4 different dictionary types (each one from strings to a different type).
(It’s a new one on me, but it looks like this:
var thingy = [String,String]()
is shorthand for this:
var thingy = [(String,String)]()
Huh. Strange but it works. Your dictionary is using a variant of this trick)
This means to make your assignment work you need to create an array of a 4-tuple (note additional brackets):
functions[theFunction] = [(path, color, line, visible)]
I’m guessing you didn’t mean to do this though. Did you actually want an array of these 4 different dictionary types? If so, you’re out of luck – you can’t store different types (dictionaries that have different types for their values) in the same array.
(Well, you could if you made the values of the dictionary Any – but that’s a terrible idea and would be nightmare to use)
Probably the result you wanted was this (i.e. make the functions dictionary map from a string to a 4-tuple of dictionaries of different types):
var functions = [String : ([String : NSBezierPath], [String : NSColor], [String : CGFloat], [String : Bool])]()
You’d assign values to the dictionary like this (note, no square brackets on the rhs):
functions[theFunction] = (path, color, line, visible)
This will work but it will be pretty unpleasant to work with. But do you really want to store your structured data in dictionaries and arrays? This isn’t JavaScript ;-) You’ll tie yourself in knots navigating that multi-level dictionary. Declare a struct! It’ll be so much easier to work with in your code.
struct Functions {
var beziers: [String:NSBezierPath]
var color: [String:NSColor]
var line: [String:NSColor]
var floats: [String:CGFloat]
var bools: [String:Bool]
}
var functions: [String:Functions] = [:]
Even better, if all the beziers, colors etc are supposed to be references with the same key, declare a dictionary that contains all of them or similar.

How to get keys from hash - ruby c extension

I am looking for a function which can get me all the keys from hash or I can loop through the hash to retrieve single key at a time.
Currently I am hardcoding key
VALUE option = rb_hash_aref(options, rb_str_new2("some_key"));
You can iterate over the key/value pairs with a callback function using rb_hash_foreach (blog post w/an example):
void rb_hash_foreach(VALUE, int (*)(ANYARGS), VALUE);
There is an rb_hash_keys in MRI, but it's not in any header files it seems, so using it may be risky.
You could always make a call to the Ruby method itself:
VALUE keys = rb_funcall(hash, rb_intern("keys"), 0)

Resources