Tuesday, 17 September 2013

Prism How to bind Module View to app wide property

Prism How to bind Module View to app wide property

I know this may seem silly but, what is the best way to bind to
Application Wide properties using MVVM and Prism?
Problem: ConnectionStatus Status {...} (Implements INotifyPropertyChanged
etc..)
How do I go about accessing this property in specific modules? I want to
be able to use the Status property across different modules?
What is the best way of getting this data across different modules?
Solutions
Currently I am leaning towards having the following view model in the
infrastructure project (shared across modules)
namespace Project.Infrastructure
{
public class AppViewModel
{
public ConnectionStatus Status {...}
}
}
And in IoC define AppViewModel as Singleton, and pass this in the
constructor of ModuleViewModel
namespace Project.ModuleA
{
AppViewModel _appViewModel;
public class ModuleViewModel
{
public ModuleViewModele(AppViewModel appViewModel)
{
....
}
}
}
Question
Is this the best way to do this or is there a better way of doing this?
E.g.
Using EventAggregator and ConnectionStatusChangeEvent to subscribe and
publish to changes in the connection? But in this case if someone
subscribes to the event after it got pubblished (such as online) they will
not get any starting value, so a IStatusService could be used at the
start?
Using RegionContext and binding that to the Status property in the Shell?
But this seems to defeat the purpose of Shell not knowing what the Modules
use etc..
Or is there something that I have completely missed?
I have read the whole Prism(v4) documentation and just not 100% sure of
which is the best way of implementing this.

How do I apply a function in R to certain columns of a data frame grouped by another column?

How do I apply a function in R to certain columns of a data frame grouped
by another column?

I've been looking at the help page for tapply and by and I'm not sure if
they are the right tool for this. For example, if I have a dataframe where
the columns are Name,Value1,Value2 and I want to apply a function, say
function f(x,y) { do_something } to Value1 and Value2 grouped by Name and
get as a result a dataframe with the columns Name,f(Value1,Value2) how
should I go about that?
I can get tapply to work in a simple case like this:
tapply(df$Name, df$value1, mean)
but what if my function takes as input df$value2 as well? and is not as
simple as mean? In other words, pseudo-notation for what I'm trying to do
would be:
tapply(df$Name, c(df$value1,df$value2), function f(x,y) { x+y+bla...})

Unable to understand a c code

Unable to understand a c code

I am studying c programming book by dennis M.Ritchie.I unable to
understand this code. please can any explain me this code?
#include <stdio.h>
main()
{
int c,i,nwhite,nother;
int ndigit[10];
nwhite=nother=0;
for(i=0;i<10;++i)
ndigit[i]=0;
while((c=getchar())!=EOF)
if(c>='0' && c<='9')
++ndigit[c-'0'];
else if(c==' ' || c=='\n' || c=='\t')
++nwhite;
else
++nother;
printf("digits =");
for(i=0;i<10;++i)
printf("%d",ndigit[i]);
printf(",white space=%d, other = %d\n",nwhite,nother);
}

How to fix "Not in GZIP format"?

How to fix "Not in GZIP format"?

I have the following code:
final remote = new HTTPBuilder("http://example.com")
final text = remote.request(GET) { req ->
uri.path = "/path"
requestContentType = TEXT
uri.query = ['name': 'value']
response.success = { resp, text ->
text
}
response.failure = { resp ->
println "Rest failure (${req.getURI()}): ${resp.status}:
${resp.statusLine.reasonPhrase}"
}
}
and it's throwing:
java.io.IOException: Not in GZIP format
at java.util.zip.GZIPInputStream.readHeader(GZIPInputStream.java:143)
at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:58)
at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:67)
at
groovyx.net.http.GZIPEncoding$GZIPDecompressingEntity.getContent(GZIPEncoding.java:73)
at org.apache.http.util.EntityUtils.consume(EntityUtils.java:83)
at
org.apache.http.conn.BasicManagedEntity.ensureConsumed(BasicManagedEntity.java:99)
at
org.apache.http.conn.BasicManagedEntity.consumeContent(BasicManagedEntity.java:112)
at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:515)
at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:441)
at groovyx.net.http.HTTPBuilder.request(HTTPBuilder.java:373)
at groovyx.net.http.HTTPBuilder$request.call(Unknown Source)
at
org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
at
org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at
org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120)
How can this be fixed?

Sunday, 15 September 2013

C++ string/char and accents

C++ string/char and accents

I'm writing a text writer in C++, which I'll have a string of a phrase and
display the appropriate bitmap font for each char value.
For now, it's working for the regular characters, but I'm getting weird
values for accents and other characters such as À, Á, Â, Ã, etc
I'm doing this:
int charToPrint = 'a';
//use this value to figure which bitmap font to display
The bitmap font does have these characters, but on this line I'm not
getting the values I'm supposed to get, such as: 195 for Ã, 199 for Ç,
etc...
I tried changing my project's character set from Multi Byte to Unicode,
but I don't think that does anything for the char->int conversion...
How can I get this conversion with chars?

Confusion about ActiveRecord associations and foreign keys (Ruby/Rails)

Confusion about ActiveRecord associations and foreign keys (Ruby/Rails)

In the below example, do I have to create "employee_id" in the office
model, or is it created automatically by db:migrate?
class Employee < ActiveRecord::Base
has_one :office
end
class Office < ActiveRecord::Base
belongs_to :employee # foreign key - employee_id
end
Sorry, feels like I'm missing something fundamental. I'm trying to get a
basic one to many relationship working, where I can use a dropdown select
of objects from the one side. Are there any good basic tuts explaining how
this works?
I had to create "_id"s in all the models where I wanted this to work, but
it doesn't seem right from examples I've looked at.

Generic class type with dictionary in C#

Generic class type with dictionary in C#

I have a dictionary and I would like the key to be able to be different
classes. Right now I'm using:
public Dictionary<dynamic,int> CbList = new Dictionary<dynamic,int>();
This works but I lose the compile-time checking. Is there a better way?
EDIT:
Basically I was wondering if there was a way to have different classes in
a dictionary without losing compile-time checking....
The key classes are related in that they have the same contructors...