MWeller.net

Swap Rows and Columns of 2D Array in C#

Author: Markus Weller   IN: Array, C#, Column, Row       Comments: 0

Background

The following methods seem to be very simple, but there are many unanswered questions  in Q&A websites like “Stackoverflow” of people who are searching for a simple and quick way to swap entire columns or rows of 2D arrays.That’s why I decided to publish my methods to do this.

I needed those methods because I often had to swap entire parts of arrays while trying to solve the “Einstein Riddle” in a C# Program.

How the Methods work

The source code should be self explaining but that’s how it is working in general:
  1. Clone the Array
  2. Check for every X value of the array if it matches with the A.
    1. If it matches replace it with the X value of B of the original array.
  3. Check for every X value of the array if it matches with the B.
    1. If it matches replace it with the X value of A of the original array.
Swapping columns works in the same way, but you have to compare and replace the Y values.

How the Methods are used

Parameters are a rectangular 2D Array and the index of the rows/columns which should get swapped.
The indexes may not be out of the Bounds of the Array, otherwise a “IndexOutOfRangeException” is thrown and the array will not change at all. If you remove the following codes no exceptions will be thrown but the array still doesn’t change at all if you pass bad values for A or B.

“throw new IndexOutOfRangeException();”

The methods will return an array where the rows / columns are swapped.

Swap 2 Rows

Swap 2 Columns

 

07-19
2012

Fixed3D BorderStyle in WPF Applications

Author: Markus Weller   IN: C#. WPF, Style       Comments: 0

Windows Presentation Foundation (WPF) Applications have many advantages over normal Windows Forms, but sadly some handy features of the Win Forms got lost like the Build-In NumericUpDown or the Ability to simply add nice Borders to almost everything.

Fixed3D BorderStyle in a WPF Application 

To add a Border with the Fixed3D Style to your Images, Buttons, TextBlocks or whatever, just surround them with four Borders.

The first Border will represent the top left corner, the second Border the inner left and the third one is the white line at the bottom. The last Border has to have the same color as the Background of the Image.

To achieve that the first two Borders only get displayed at the top left and the last two only at the right side the borderthickness has to be set like I did it in my Example. With the Margin property of the first Border you can move the Image.

 

06-15
2012

Chrome window with minimal Interface

Author: Markus Weller   IN: Browser, Chrome       Comments: 0

Sometimes I needed a browser window without the navigation bar and all the other Toolbars, but in Chrome  this is not as simple as in Firefox where you can deactivate the Toolbars by right clicking them and then just untick all the bars you want to remove.

But I needed this feature for designing a Twitter widget which should look well in Columns with a very small width.

I found no other possibility to get such a window but starting a new chrome window with the Parameter

The easiest way to do this is creating a new Shortcut in any folder of Windows with the “target:”

 

minimal chrome window

 

04-15
2012

Chrome User Agent

Author: Markus Weller   IN: Browser, Chrome, User Agent       Comments: 0

If your a Developer or just want to save bandwidth it would be nice to view a specific webpage like smartphones do. In Google Chrome this is very easy and you don’t have to install anything or add a config entry like in Firefox.

Instructions

  1. Press Strg + Shift + J (Windows/Linux) or Command + Option + J (Mac) or the Settings Button in the Top left of Chrome -> Tools -> Developer Tools.
  2. Click the small Setting Button at the bottom. (1)
  3. Check “Override User Agent“ (2)
  4. Then select a User Agent like:Internet Explorer , Firefox, iPhone, iPad or Android 2.3
  5. Or select “Other…” and user a own User Agent String 
Chrome Developer Tools with User Agent Settings.
03-10
2012
loading...