See The Answers

I want to use a track-bar to change a form's opacity.

This is my code:

decimal trans = trackBar1.Value / 5000;
this.Opacity = trans;

When I try to build it, I get this error:

Cannot implicitly convert type 'decimal' to 'double'.

I tried making trans to double, but then the control doesn't work. This code has worked fine for me in VB.NET in the past.

See The Answers

I have an absolutely positioned div containing several children, one of which is a relatively positioned div. When I use a percentage-based width on the child div, it collapses to 0 width on IE7, but not on Firefox or Safari.

If I use pixel width, it works. If the parent is relatively positioned, the percentage width on the child works.

Is there something I'm missing here? Is there an easy fix for this besides the pixel-based width on the child? Is there an area of the CSS specification that covers this?

See The Answers

Are there any conversion tools for porting from Visual J# code to C#?

See The Answers

Given a DateTime representing a person's birthday, how do I calculate their age?

See The Answers

Given a specific DateTime value, how do I display relative time, like:

  • 2 hours ago
  • 3 days ago
  • a month ago

Et cetera?

See The Answers

Is there a standard way for a Web Server to determine what time zone offset a user is in?

From a HTTP header, or part of the user-agent description perhaps?

See The Answers

How do you expose a LINQ query as an ASMX web service? Usually, from the business tier, I can return a typed DataSet or DataTable which can be serialized for transport over ASMX.

How can I do the same for a LINQ query? Is there a way to populate a typed DataSet or DataTable via a LINQ query?:

public static MyDataTable CallMySproc()    
    string conn = ...;

    MyDatabaseDataContext db = new MyDatabaseDataContext(conn);    
    MyDataTable dt = new MyDataTable();

    // execute a sproc via LINQ
    var query = from dr in db.MySproc().AsEnumerable
    select dr;

    // copy LINQ query resultset into a DataTable -this does not work !    
    dt = query.CopyToDataTable();

    return dt;

How can I get the resultset of a LINQ query into a DataSet or DataTable? Alternatively, is the LINQ query serializeable so that I can expose it as an ASMX web service?

See The Answers

Solutions are welcome in any language. :-) I'm looking for the fastest way to obtain the value of π, as a personal challenge. More specifically I'm using ways that don't involve using #defined constants like M_PI, or hard-coding the number in.

The program below tests the various ways I know of. The inline assembly version is, in theory, the fastest option, though clearly not portable; I've included it as a baseline to compare the other versions against. In my tests, with built-ins, the 4 * atan(1) version is fastest on GCC 4.2, because it auto-folds the atan(1) into a constant. With -fno-builtin specified, the atan2(0, -1) version is fastest.

Here's the main testing program (pitimes.c):

#include <math.h>
#include <stdio.h>
#include <time.h>

#define ITERS 10000000
#define TESTWITH(x) {                                                       \
    diff = 0.0;                                                             \
    time1 = clock();                                                        \
    for (i = 0; i < ITERS; ++i)                                             \
        diff += (x) - M_PI;                                                 \
    time2 = clock();                                                        \
    printf("%s\t=> %e, time => %f\n", #x, diff, diffclock(time2, time1));   \

static inline double
diffclock(clock_t time1, clock_t time0)
    return (double) (time1 - time0) / CLOCKS_PER_SEC;

    int i;
    clock_t time1, time2;
    double diff;

    /* Warmup. The atan2 case catches GCC's atan folding (which would
     * optimise the ``4 * atan(1) - M_PI'' to a no-op), if -fno-builtin
     * is not used. */
    TESTWITH(4 * atan(1))
    TESTWITH(4 * atan2(1, 1))

#if defined(__GNUC__) && (defined(__i386__) || defined(__amd64__))
    extern double fldpi();

    /* Actual tests start here. */
    TESTWITH(atan2(0, -1))
    TESTWITH(2 * asin(1))
    TESTWITH(4 * atan2(1, 1))
    TESTWITH(4 * atan(1))

    return 0;

And the inline assembly stuff (fldpi.c), noting that it will only work for x86 and x64 systems:

    double pi;
    asm("fldpi" : "=t" (pi));
    return pi;

And a build script that builds all the configurations I'm testing (

gcc -O3 -Wall -c           -m32 -o fldpi-32.o fldpi.c
gcc -O3 -Wall -c           -m64 -o fldpi-64.o fldpi.c

gcc -O3 -Wall -ffast-math  -m32 -o pitimes1-32 pitimes.c fldpi-32.o
gcc -O3 -Wall              -m32 -o pitimes2-32 pitimes.c fldpi-32.o -lm
gcc -O3 -Wall -fno-builtin -m32 -o pitimes3-32 pitimes.c fldpi-32.o -lm
gcc -O3 -Wall -ffast-math  -m64 -o pitimes1-64 pitimes.c fldpi-64.o -lm
gcc -O3 -Wall              -m64 -o pitimes2-64 pitimes.c fldpi-64.o -lm
gcc -O3 -Wall -fno-builtin -m64 -o pitimes3-64 pitimes.c fldpi-64.o -lm

Apart from testing between various compiler flags (I've compared 32-bit against 64-bit too, because the optimisations are different), I've also tried switching the order of the tests around. The atan2(0, -1) version still comes out top every time, though.

See The Answers

If I have a trigger before the update on a table, how can I throw an error that prevents the update on that table?

See The Answers

I've been having issues getting the C sockets API to work properly in C++. Specifically, although I am including sys/socket.h, I still get compile time errors telling me that AF_INET is not defined. Am I missing something obvious, or could this be related to the fact that I'm doing this coding on z/OS and my problems are much more complicated? ;)

Oh dear. Upon further investigation, I discovered that there is an #ifdef that I'm hitting. Apparently z/OS isn't happy unless I define which type of sockets I'm using with a little

#define _OE_SOCKETS

action. Now, I personally have no idea what this OE SOCKETS thingy is, and if any z/OS sockets programmers are out there (all 3 of you), perhaps you could give me a rundown of how this all works?

See The Answers

How can I monitor an SQL Server database for changes to a table without using triggers or modifying the structure of the database in any way? My preferred programming environment is .NET and C#.

I'd like to be able to support any SQL Server 2000 SP4 or newer. My application is a bolt-on data visualization for another company's product. Our customer base is in the thousands, so I don't want to have to put in requirements that we modify the third-party vendor's table at every installation.

By "changes to a table" I mean changes to table data, not changes to table structure.

Ultimately, I would like the change to trigger an event in my application, instead of having to check for changes at an interval.

The best course of action given my requirements (no triggers or schema modification, SQL Server 2000 and 2005) seems to be to use the BINARY_CHECKSUM function in T-SQL. The way I plan to implement is this:

Every X seconds run the following query:


and compare that against the stored value. If the value has changed, go through the table row by row using the query

select row_id,BINARY_CHECKSUM(*) from sample_table WITH (NOLOCK);

and compare the returned checksums against stored values.

See The Answers

I am aware that in .NET there are three timer types (see Comparing the Timer Classes in the .NET Framework Class Library). I have chosen a threaded timer as the other types can drift if the main thread is busy, and I need this to be reliable.

The way this timer works in the control of the timer is put on another thread so it can always tick along with the work begin completed on the parent thread when it is not busy.

The issue with this timer in a console application is that while the timer is ticking along on another thread the main thread is not doing anything so the application closes.

I tried adding a while true loop, but then the main thread is too busy when the timer does go off.

See The Answers

I am starting a new web application in PHP and this time around I want to create something that people can extend by using a plugin interface. How does one go about writing 'hooks' into their code so that plugins can attach to specific events?

See The Answers

Let's say you create a Wizard in an HTML form. One button goes back and one goes forward. Since the "back" button appears first in the markup, when you press Enter it will use that button to submit the form.


   <input type="text" name="field1" /> <!-- put your cursor in this field and press Enter -->

   <input type="submit" name="prev" value="Previous Page" /> <!-- This is the button that will submit -->
   <input type="submit" name="next" value="Next Page" /> <!-- But this is the button that I WANT to submit -->

What I would like to do, is get to decide which button is used to submit the form when a user presses Enter. That way, when you press Enter the Wizard will move to the next page, not the previous. Do you have to use tabindex to do this?

See The Answers

Let's say I have a DataTable with a Name column. I want to have a collection of the unique names ordered alphabetically. The following query ignores the order by clause.

var names =
    (from DataRow dr in dataTable.Rows
    orderby (string)dr["Name"]
    select (string)dr["Name"]).Distinct();

Why does the orderby not get enforced?

See The Answers

How do you page through a collection in LINQ given that you have a startIndex and a count?

See The Answers

I've got all these comments that I want to make into 'RDoc comments', so they can be formatted appropriately and viewed using ri. Can anyone get me started on understanding how to use RDoc?

See The Answers

The version of Subclipse (1.2.4) currently available through Aptana's automagic Plugins Manager does not work with the newest version of Subversion.

I see on the Subclipse website however that they have 1.4.2 out for Eclipse. So I added a new remote update site to my Update manager. When I tried to install it, it told me I needed Mylyn 3.0.0. So I after much searching I found Mylyn 3.0.0 and added another new remote update site to my update manager. Then when I tried to install that, it told me I needed org.eclipse.ui 3.3.0 or equivalent.

Looking at the configuration details for Aptana, it looks like it is built against eclipse 3.2.2.

Does anyone know if there is a way to upgrade the version of Eclipse Aptana is built against to 3.3.0? Or if there is some other way to get Subclipse to work with the very newest version of Subversion?

I know this isn't necessarily a "programming" question, but I hope it's ok since it's highly relevant to the programming experience...