Intersection function in C# (updated)

C# lacks math functions. Recently I needed to use intersection of strings in my project. It looks like a fairly common and simple task but quick Google research didn’t bring any valuable results. Just to refresh your memory, Intersection is a common set of elements from two or more collections. Wiki: http://en.wikipedia.org/wiki/Intersection_%28set_theory%29

So, here are two versions of Intersection function written in C#. First one takes two string arrays as parameters and returns an intersection of them.

public static string[] Intersection(string[] arrayA, string[] arrayB)
{
        ArrayList outArray = new ArrayList();
        for (int i = 0; i < arrayA.Length; i++)
        {
                for (int j = 0; j < arrayB.Length; j++)
                {
                        if (arrayA[i] == arrayB[j] && outArray.IndexOf(arrayB[j]) < 0)
                                outArray.Add(arrayB[j]);
                }
        }
        return (String[])outArray.ToArray(typeof(string));
}

Second one takes a Jagged Array of strings as a parameter and returns an intersection of them.

public static string[] Intersection(string[][] jArray)
{
        string[] resArray = new string[] { };
        int nArrays = jArray.Length;
       
        if (nArrays > 0)
        {
                //We are testing against the first array
                string[] testArray = jArray[0];
                if (nArrays > 1)                       
                {
                        ArrayList outArray = new ArrayList();

                        //Loop thru elemets in test array
                        for (int n = 0; n < testArray.Length; n++)
                        {
                                //Init counter
                                int resFound = 0; 

                                //Loop thru all elements in jagged array 
                                for (int i = 1; i < nArrays; i++)
                                {
                                        for (int j = 0; j < jArray[i].Length; j++)
                                        {
                                                if (jArray[i][j] == testArray[n])
                                                {
                                                        //Match found - increment the counter
                                                        //and break to the next array
                                                        resFound++;
                                                        break;
                                                }
                                        }
                                }

                                //Check if number of matches is equal or greater than number of arrays
                                if (resFound >= nArrays - 1 && outArray.IndexOf(testArray[n]) < 0)
                                        outArray.Add(testArray[n]);
                        }

                        resArray = (String[])outArray.ToArray(typeof(string));
                }
                else                
                        //Use whole test array if it is the only one
                        resArray = testArray;
               
        }
        return resArray;
}

 

 

posted by alexark with 0 Comments

Finally! Design tools from Microsoft hit Beta.

A quote from http://www.betanews.com/article/New_Microsoft_Web_Design_Tool_in_Beta/1157562798

Microsoft late Tuesday released the first beta of its new Web design tool that will compete with the likes of Adobe Dreamweaver. The program is part of Microsoft's forthcoming Expression lineup, which also includes a graphical design tool and an interface designer.

Expression Web Beta 1 is the second public preview of the tool following a Community Technology Preview issued in May. Features of the application include the ability to build sophisticated CSS-based layouts, standards compliance and built-in support for ASP.NET 2.0 server technology.

Download it here: http://www.microsoft.com/products/expression/en/default.mspx

posted by alexark with 0 Comments

Geeky Fortune Cookie

This one is by far the best I’ve ever gotten. Right next to: “The greatest danger could be your stupidity” and “It must be home-grown”.

What is interesting they all came from the same restaurant in Point Richmond, CA called “Red Pepper”. They are using “Super K” fortune cookies from Kari-Out Co., NY.

Some web resources on Fortune Cookies:

- Random Fortune Cookie Generator: http://fortunecookie.rleeden.no-ip.com/
- http://www.weirdfortunecookies.com (also mentioned the one Steve has about stupidity)
- “Cookie Master” – article in New Yorker: http://www.newyorker.com/printables/talk/050606ta_talk_olshan

posted by alexark with 0 Comments

ASP.NET 2.0 Internals Explained

Interesting article explaining ASP.NET 2.0 internals. What actually happens under the hood, compared to ASP.NET 1.1

"For professional ASP.NET developers, the big questions about ASP.NET 2.0 relate to what has changed on the inside. New features are fun and interesting to learn about, but changes to the core structure of ASP.NET speak louder to developers who really want to master the technology. In this white paper, we will cover how the internal structure of ASP.NET 2.0 has changed since version 1.x."

http://msdn.microsoft.com/asp.net/default.aspx?pull=/library/en-us/dnvs05/html/Internals.asp

posted by alexark with 0 Comments

Microsoft Best Practice Analyzer for .NET 2.0

Microsoft just released the Best Practice Analyzer for .NET 2.0 web applications. It's still beta, but works.

Here is the quote from Microsoft:

"The Best Practice Analyzer ASP.NET (alpha release) is a tool that scans the configuration of an ASP.NET 2.0 application. The tool can scan against three mainline scenarios (hosted environment, production environment, or development environment) and identify problematic configuration settings in the machine.config or web.config files associated with your ASP.NET application. This is an alpha release intended to gain feedback on the tool and the configuration rules included with it"

Free download here: http://go.microsoft.com/?linkid=5150083

posted by alexark with 0 Comments

Insurance rip-off in Louisiana

Some people in Vertigo already know that I’m fighting against Progressive Insurance because they paid only half of the repair estimate they made.

Today I saw an interesting report on KRON4 TV stationPhil Matier interviewed a lawyer (did not catch his name), specializing in cases against insurance companies. He told about tricks insurance companies playing with victims of Catrina/Rita in flooded areas of Louisiana. Some of the common scams:

  • Insurance company rejects the water damage claims if owner only had hurricane insurance.
  • Insurance company pays only for the first floor in a flooded house, saying that second floor was above the water level.
  • If a house has the fire damage, insurance company says that the fire could not be caused by flood and hurricane

This confirms my belief how evil insurance companies are...

posted by alexark with 0 Comments

New/Old Web Project model for VS 2005

MSFT just released a first preview of new Web Application Project Model. It looks very much like old-good VS 2003 Web Project model, but without requirements of IIS and Front Page Extensions.

Supposedly it should help migrating web apps from ASP.NET 1.1 to 2.0. Also it might be helpful for complex solutions with multiple web projects and elaborate deployment strategy.

Here is Scott Guthrie's blog: http://weblogs.asp.net/scottgu/archive/2005/12/16.aspx

posted by alexark with 0 Comments

Problems using paging in GridView

Given: You have a page with GridView control, bound to ObjectDataSource control. GridView control has paging option enabled.

 

Task: When there is an ItemId in QueryString, switch to appropriate page and make a GridViewRow with the same DataKey selected.

 

Solution: So far I couldn’t find a solution to this task. To have an item selected – piece of cake, but when it comes to the page selection, it appears that we have an access only to the DataKeys collection for the page, currently rendered in GridView. It is some sort of the catch 22. To have an Item selected, we need to know the page this Item is on. To have the page, we need an access to all the DataKeys collection. We can only access to the DataKeys for the current page. L

posted by alexark with 4 Comments

ASP.NET 2.0 page lifecyle

ASP.NET 2.0 event sequence changed a lot since 1.1. Here is the order:

 

Application: BeginRequest

Application: PreAuthenticateRequest

Application: AuthenticateRequest

Application: PostAuthenticateRequest

Application: PreAuthorizeRequest

Application: AuthorizeRequest

Application: PostAuthorizeRequest

Application: PreResolveRequestCache

Application: ResolveRequestCache

Application: PostResolveRequestCache

Application: PreMapRequestHandler

Page: Construct

Application: PostMapRequestHandler

Application: PreAcquireRequestState

Application: AcquireRequestState

Application: PostAcquireRequestState

Application: PreRequestHandlerExecute

Page: AddParsedSubObject

Page: CreateControlCollection

Page: AddedControl

Page: AddParsedSubObject

Page: AddedControl

Page: ResolveAdapter

Page: DeterminePostBackMode

Page: PreInit

Control: ResolveAdapter

Control: Init

Control: TrackViewState

Page: Init

Page: TrackViewState

Page: InitComplete

Page: LoadPageStateFromPersistenceMedium

Control: LoadViewState

Page: EnsureChildControls

Page: CreateChildControls

Page: PreLoad

Page: Load

Control: DataBind

Control: Load

Page: EnsureChildControls

Page: LoadComplete

Page: EnsureChildControls

Page: PreRender

Control: EnsureChildControls

Control: PreRender

Page: PreRenderComplete

Page: SaveViewState

Control: SaveViewState

Page: SaveViewState

Control: SaveViewState

Page: SavePageStateToPersistenceMedium

Page: SaveStateComplete

Page: CreateHtmlTextWriter

Page: RenderControl

Page: Render

Page: RenderChildren

Control: RenderControl

Page: VerifyRenderingInServerForm

Page: CreateHtmlTextWriter

Control: Unload

Control: Dispose

Page: Unload

Page: Dispose

Application: PostRequestHandlerExecute

Application: PreReleaseRequestState

Application: ReleaseRequestState

Application: PostReleaseRequestState

Application: PreUpdateRequestCache

Application: UpdateRequestCache

Application: PostUpdateRequestCache

Application: EndRequest

Application: PreSendRequestHeaders

Application: PreSendRequestContent

 

(Tnaks to http://weblogs.asp.net/jeff/)

 

Here: http://hydrate.typepad.com/leo/2004/08/the_aspnet_v20_.html  you can find a nice colorful lifecycle chart.

 

posted by alexark with 1 Comments

Profile issues in ASP.NET 2.0

Profile is a great feature of the ASP.NET 2.0. It allows you to store a lot of information in Sql Server without crating all the plumbing. There is a little glitch we just discovered. It Pet Shop 4 we allow user to have a shopping cart before sign in. Therefore when you sign in, we transfer the anonymous cart to the user cart:

Global.asax code:

 

void Profile_MigrateAnonymous(Object sender, ProfileMigrateEventArgs e) {

        ProfileCommon anonProfile = Profile.GetProfile(e.AnonymousID);

        Profile.ShoppingCart = anonProfile.ShoppingCart;

    }

It works just fine. Now, when user checked out, we have to clear his/her cart, so we have to clear the cart in anonymous profile. We decided to destroy it on MigrateAnonymous event. Here’s the code:

void Profile_MigrateAnonymous(Object sender, ProfileMigrateEventArgs e) {

        ProfileCommon anonProfile = Profile.GetProfile(e.AnonymousID);

        Profile.ShoppingCart = anonProfile.ShoppingCart;       

      

        anonProfile.ShoppingCart = null;     

    }

Ouch – doesn’t work! User put an item in the cart, sign in, check out, cart is back!

Lets try to clear it upon check out:

Profile.ShoppingCart.Clear();

ProfileCommon anonProfile = Profile.GetProfile(HttpContext.Current.Request.AnonymousID);

anonProfile.ShoppingCart.Clear();

 

Doesn’t work as well…

 

It looks like a bug. Theoretically we should be able to destroy or recreate an anonymous profile on the fly.

posted by alexark with 1 Comments