Archive for category .Net

Math.Pow is annoying – so I wrote my own

If you’ve not had to use it yet, Math.Pow() is annoying.

What it does: finds the ‘Power of’ x. So 2^2=4, 2^4=16, etc.

Why it’s annoying:

Well, there are a number of reasons. The first is it’s not the ^ symbol which is widely used by many programming languages (but not .Net).

The second is it requires decimal as it’s input type. Why MS? Why? I wanted to use the ulong datatype in my project but couldn’t get the power of it without converting it to decimal first. With large numbers this has the annoying feature of being expressed with .Nets exponential syntax like so:
1.2345678901230000E-030

I was working on a project that required me to calculate very large whole numbers and then write them to a file, this was an issue.

So, what I did was make it better by re-writing Math.Pow using ulong. I’m sure there are other ways to do this, but re-writing this function proved really simple to do. Feel free to use my method in your projects (though leaving a reference in the code to me would be nice!), also feel free to post alternative ways to solve this problem. I always enjoy your feedback!

//Pow() taken from Anthony's Tech Blog!
//http://anthonystechblog.wordpress.com/2011/04/18/math-pow-is-annoying/
        static ulong Pow (ulong x, ulong y)
        {
            if (y == 0) return 1;
            ulong count = x;
            for (ulong i = 0; i < y-1; i++)
            {
                count = x * count;
            }
            return count;
        }

Leave a Comment

Lambda Expressions

Lambda Expressions are a great way to simplify code that uses delegates.  I won’t bother getting to deep into it as this site does a great job of explaining the concept:

The Blomsma Code: Three steps to understanding Lambda expressions in C#

Leave a Comment

Linq – what it is

Linq (Language Integrated Query) is a great new way of fetching data from a database.  If you’ve ever written a data driven application then you know the pain of writing SQL to interact with your database.  It’s always seemed clunky to me, here you are creating objects with properties but the way those objects get saved to a database and read back out is in a language completely different from what you program in.

Linq is an attempt to change that.  Where a standard query looks like this:

Select col1, col2, col3 from myTable where col1='this'

a Linq query is written in your code and looks like this:

     var vProducts =
        from p in products
        where p.UnitsInStock > 0
        select p;

To view more Linq examples this is a great resource:
101 LINQ Samples: http://msdn.microsoft.com/en-us/vcsharp/aa336746

Leave a Comment

WPF – Working with Listboxes

This is a pretty good article on how to create a list box.  What I like about it is it starts off really simple and works towards more advanced features.  It’s pretty easy to follow allong with:

http://www.c-sharpcorner.com/UploadFile/mahesh/WPFListBox08252008100659AM/WPFListBox.aspx

Leave a Comment

WPF – Windows Presentation Foundation

I’ve been dabbling with WPF on a on again off again basis for a while now.  For those of you that have yet to grasp the concept this is a pretty good read on the concept:

http://jmorrill.hjtcentral.com/Home/tabid/428/EntryId/432/MVVM-for-Tarded-Folks-Like-Me-or-MVVM-and-What-it-Means-to-Me.aspx

Leave a Comment

Crystal Reports for Visual Studio 2010

EDIT:
Note that the latest Crystal reports version is no longer in beta, this is a good video to help you get past the bumps of installing and getting it to work for the first time in .Net 2010:
http://www.youtube.com/watch?v=wud4VtqpetU

You can ignore the instructions on how to get the download from their site as a couple of the steps are wrong (their site has changed since the video was made).
As of March 2012 get Crystal Reports for Visual Studio 2010 Download here
As of (June 22, 2011) it was available here (Still a useful page, especially if the new link goes dead. You know it will) Crystal Reports for Visual Studio 2010 Download here.

These are the same links:
Download Crystal Reports for Visual Studio 2010
Complete Package (EXE)
Click Once
Merge Modules
Redist Installation (32 bit)
Redist Installation (64 bit)
End EDIT:

Well it seems the good people at Crystal have been dragging their feet a bit in releasing a new version for Visual Studio 2010.  I recently downloaded the beta version of Crystal for Visual Studio 2010, if you’re interested you can get it here:
http://www.sdn.sap.com/irj/scn/weblogs?blog=/pub/wlg/20322

I installed it on my machine and was having some good success, that is until I tried to run it the first time.  It seems there is a bug in it that requires this attribute be added to your app.config / web.config file:
<startup useLegacyV2RuntimeActivationPolicy=”true”>

</startup>
Which has something to do with a change Microsoft made in .Net ver 4.0 that Crystal failed to handle before their release.

So after that one little hiccup I completed development of my project and all was well, which I thought wasn’t too bad for a beta version of software, that is, until I tried to deploy my change.

It seems the Crystal runtime has bloated a bit, it’s now 1gig+ and takes forever to install (over an hour on my clients’ machine).  Finally I had it and my app installed I then went to run my new app annnnddd.. crash.  Dang it.  It turns out that the beta version of the Crystal install does not support being installed on a machine that already has a previous version of Crystal on it, no matter what version.

So I tried uninstalling versions, re-installing versions, uninstalling versions on my machine, re-installing only the 2010 version and re-building.  No matter what I tried I could not get my report to run on my clients machine.

At this point I have no idea what the issue was, whether I was deploying it wrong, if the runtime installer for Crystal is broken, or if it’s something else entirely, but at this point if you’re interested in trying out Microsoft.Reporting.WinForms.ReportViewer I highly recommend it!

Ultimately I wound up using Microsoft’s Reporting engine, which I have to say was a delight to work with.  It has its own little oddities, but the first time I ran it, the FIRST time, it worked like a charm.

Thanks MS, sorry Crystal, you just lost a customer.

Leave a Comment

Windows code samples – shell integration

There’s a interesting site out there with a number of windows example code samples.  They are ‘shell integration’ examples, it looks interesting.

http://code.msdn.microsoft.com/shellintegration

Leave a Comment

How to set the timeout for a webservice call .Net

There are a number of things that can time out in a webservice so this question has a few parts to it.  Among the things that can time out the main ones are:

1) The client making the call (your web app for example)
2) The webservice itself (by default a web method has a max run time set by the web.config / IIS)
3) Generally timeouts are related to database calls that take too long.  Your data call can time out getting data from the database.

Now generally you shouldn’t be extending timeouts if your app is taking too long to run.  If your app is timing out chances are your app is doing something wrong and is simply too slow.  Perhaps you should put the long running process on another thread?  Or improve your SQL?  Or maybe it actually is a hardware / network bottle neck.  The best reason I can think of for wanted to extend the timeout on a web-service is when you are in debug mode and having the service timeout during debugging is annoying and time wasting.

In any case, if you do genuinely need to extend the timeout for a web service add this to your web.config:
<httpRuntime executionTimeout=”600″ />
This node is a child of <system.web />

Leave a Comment

Datatypes for .Net, Oracle, etc

Here’s a topic that’s integral to programming, but I doubt there’s a developer out there that has them all memorized, datatypes.  Different languages / data storage applications have different datatypes.  Often they’re not even compatible with each other so marshaling data from database to user interface and back again can be a challenge.

I’m going to keep a list of links to datatype definitions here, if you have one you would like me to add be sure to leave a message!

Visual Studio .Net 2003′s datatypes:
http://msdn.microsoft.com/en-us/library/47zceaw7%28v=vs.71%29.aspx

Visual Studio .Net 2010′s datatypes:
http://msdn.microsoft.com/en-us/library/47zceaw7(v=VS.100).aspx

Oracle Datatypes:
http://ss64.com/ora/syntax-datatypes.html

Leave a Comment

Datagrids – how to filter / sort

Datagrids – how to filter / sort
This topic has been done over and over and over again.  The thing is, it always seems like I’m trying to make the datagrid do something I’ve never done before on every project I write.

Here’s a good link on some basic filtering / sorting using datagrids:
http://www.akadia.com/services/dotnet_filter_sort.html

Leave a Comment

Follow

Get every new post delivered to your Inbox.