Dienstag, 10. April 2012

Using .NET App.Config / Settings.settings

To use Settings.settings / app.config in your .NET (C#) windows forms app:

1. In the Solution Explorer right-click on your project, select "Add" > "New Item"
2. Select "Settings File", specify a meaningful name and click "Add". The File is created, added to your project and displayed.
3. Enter some setting 'fields' and their types you wish to maintain as application settings, much like a ressources file. A correspoinding app.config file will be generated / extended (this is where the actual values will be kept).

4. To access those app settings (e.g. when loading your application)

[NameOfTheSettingsFile] lastConfiguration = [NameOfTheSettingsFile].Default;
this.SomePropertyA = lastConfiguration.[SomeFieldYouHaveDefinedInTheSettingsFile];
this.SomePropertyB = lastConfiguration.[SomeOtherFieldYouHaveDefinedInTheSettingsFile]

5. To save/update app configuration (e.g. upon exiting or when options have been edited)

[NameOfTheSettingsFile] lastConfiguration = [NameOfTheSettingsFile].Default;
lastConfiguration.[SomeFieldYouHaveDefinedInTheSettingsFile] = this.SomePropertyA;
lastConfiguration.[SomeOtherFieldYouHaveDefinedInTheSettingsFile] = this.SomePropertyB;

lastConfiguration.Save();

Mittwoch, 29. Juni 2011

What happened to my changes in this damn Excel file?

About 30 minutes ago, I was raging to find out that for some reason, MS Excel 2010 had not saved all the changes I had made and saved yesterday. I can vaguely remember that the program once crashed and 'auto-recovered' my last changes. After that I saved those results to another file, which I also overwrote.

Soo.. now today when I opened that overwritten file, all my changes were gone, boo. The built-in recovery features (unsaved files) of MS Office did not help at all - basically no back-ups were to be found anywhere.

However, I was able to recover the file through the Win7 feature 'previous Versions' .. that is, right-click on the file in the Explorer > Properties > Previous Versions. There you find a list of backups made by Win7 every time you change/overwrite the file. :D

I'm not entirely sure whether it was exactly this feature which caused the problems or whether this was caused by Excel itself. Anyway: annoyance.

Donnerstag, 8. April 2010

Can not use an object as parameter to a method.

A nice example how XCode's Objective-C compiler gives greatly inscrutable compilation error descriptions:

error: can not use an object as parameter to a method.
Which means:
Objects, i.e. instances of classes, can only given to a function via a pointer (id). This is because in contrast to structs (e.g. CGRect) and basic data types (int, float, etc.), classes may have custom constructors and deconstructors which the compiler is not able to call for you when a copy shall be given to the function. So basically:

- (NSString) sayYeah { NSLog(@"YEAH"); }; // won't work

- (NSString*) sayMaan { NSLog(@"MAAN"); };  // will work, notice the *

Freitag, 19. März 2010

RSQLite Hints

I needed to convert a shapefile into a SQLite data base, which should be easy using R's rgdal/maptools and RSQLite packages, no? If you're new to R like me, you may run into some troubles, so here are some issues:

1. Basic RSQLite database interaction
The package RSQLite is (unfortunately) merely a driver for R's stanardized data base interface DBI, which is rather poorly documented and kind of unintuitive. This means that we need to use functions from the DBI package to interact with our database, which makes things unnecessarily complicated. But whatever, here is a basic interaction:

library(RSQLite)
library(DBI)


# set up SQLite DB connection
db <- dbConnect(dbDriver("SQLite"), dbname = "mydatabase.sqlite")

# query
addresses <- dbGetQuery(db, SELECT * FROM addresses")

# close
dbDisconnect(db)


If you google around a little, you'll see that we basically have to basic functions for those kinds of queries: dbGetQuery and dbSendQuery .. the difference here is that with the latter, you will have to catch the result and use the 'fetch' command to grab the data and then cleanup using the dbClearResult, i.e. you need three commands. This must be done after every SQL query, even if you don't care about the response like after a "INSERT INTO" command. In a nutshell: use dbGetQuery which does this all for you.


2. "CREATE DATABASE does not work!!"
I'm not sure if this is a SQLite thing, but at least you can not/don't need to do that in R's RSQLite interface. In other words, you can only have one single database per SQLite-file. So, no big thing, just start off with "CREATE TABLE". :I



3. Large Amounts of Data
When one uses a database, normally there is quite a few data in the game. What to do if you need to get a lot of data into or out of the data base? As soon as you start using for loops to add rows from your data base, you'll probably run into performance problems since apparently setting up the connection for each query takes considerable time. Two work arounds:


a. dbGetPreparedQuery with manual transaction:

dbBeginTransaction(db)
dbGetPreparedQuery(db, "INSERT INTO addresses (name, phone, address) VALUES (?,?,?)", bind.data=new_empolyee_data);
dbCommit(db)

This does not excecute the query until dbCommit, in other words, you fill something like a buffer. The dbGetPreparedQuery automatically fills out the "?"-fields in your query string and submit those strings to the database. Again, there is a dbSendPreparedQuery alternative, where you'll have to clear the result manually after the command.
The bind.data parameter requires a data frame (what is that? boo.). So if you get an error like:
Error in function (classes, fdef, mtable) :
unable to find an inherited method for function "dbGetPreparedQuery", for signature "SQLiteConnection", "character", "matrix"
.. convert it to a data frame using the as.data.frame(..) function, see example below.


b. write or read tables as a whole:

You can conveniently add a full table to the data base using the dbWriteTable command. The field names in SQL are taken from the column names of your matrix. Numeric values are stored in a 'REAL' field, strings in a general TEXT field. I don't know if you can customize this, it's rather quick and dirty, so to say.
# give the data matrix appropriate column names
colnames(addr) <- c("key", "empolyee", "phone", "address")
# add the table as data.frame to the db
dbWriteTable(db, "new_addresses", as.data.frame(addr))

Here again, this must be a data frame to work correctly.
If you want to insert something into an already existing data base you can use SQL to do that on the database itself:
INSERT INTO addresses (name, phone, address)
SELECT * FROM new_addresses

Montag, 1. März 2010

R maptools: Shapefile Polygons: Hole attribute

Problem: When I import a Shape File ( readShapePoly ) with the R package maptools, it does not seem to import that 'hole' attribute correctly. All the boolean values are set to 'FALSE'.

Solution: This attribute is not present in the shapefile itself, thus it cannot be imported. There is a function checkPolygonsHoles in the maptools package to fill out the attribute fields in your imported shapefile structure. I.e. you need to caluculate the value of that hole-field yourself, like so:

shp <- readShapePoly(...);
shp@polygons <- lapply(shp@polygons, checkPolygonsHoles);

R Search and Replace

How can one replace certain characters or sequences of characters in a string?

I searched for a function that does that for quite a while but to no avail. But, using the strsplit & paste functions, one can do it like this:

streplace <- function(string, searchterm, replacestring) {
    string <- paste(strsplit(string,paste("[",searchterm,"]", sep=""))[[1]], collapse=replacestring);
    return(string);
}

With the usage for example: htmlstring = streplace(mytext, "ä", "&auml;")

Montag, 1. Februar 2010

Tinn-R Error At Startup

(on WinXP)
When trying to connect to R or RGui with Tinn-R, one may get the following error: 

Die Anweisung in "0x00000000" verweist auf Speicher in "0x00000000". Der Vorgang "read" konnt nicht auf dem Speicher durchgeführt werden.

(Basically: The instruction in "0x0" referes to memory segment "0x0". The process "read" could not be executet on the memory.)

In my case, for some odd reason, this was resolved selecting the compatibility option "deactivate visual desings" ("Visuelle Designs deaktivieren) . I.e. right click the application executable or shortcut and selecting Properties. The setting can then be found in the "Compatibility" Tab.