Posted by

Problems going from .NET 4.0 to .NET 2.0

I ran into an issue late yesterday. It came to my attention that one of our builds was failing on a new project I submitted. Crap..

The build errors presented themselves as “Missing Assembly Or Reference” for my forms.resx file.

The only reason I can see this failing for is because I’ve set my .NET Framework to 2.0 instead of 4.0 Client Profile. To ensure that was the most likely cause, I switched back and tried to compile. SUCCESS. But that leaves me with the question, what’s going on with my .resx files?

Well after hitting up the internet for some solutions, some of which involved deleting my .resx files, then renaming my form files, then re-opening it, then adding the renamed version to get the .resx to rebuild *Ack*, it sounded like such a process… I decided to go rogue and figure it out myself.

Turns out it’s a really simple solution. Crack open the .resx file in xml mode, (This is easy to do if your builds are failing, just double click on the errors and you’ll be taken in) you’ll notice a lot of sections marked 4.0.0.0. If this is the case, all you need to do is re-write them as 2.0.0.0. This will fix the assembly reference and allow you to build your project properly.

Happy coding.

ImageList Epic Fail

Ran into a rather interesting problem today.

Build configurations can wreak havoc on certain programs with different components thrown on them. Especially third party components.

This morning I was tasked with changing the build configuration on all the applications to x86. The task itself is very easy to do if the agents on your build machine are setup properly to build with the selected configuration.

We went through the hoops of setting up the build agents the other day, and everything seemed A-OK. Multiple projects were sent to build and works flawlessly.

Then came “THE ONE” as I shall refer to it. THE ONE has some serious issues, big time bad issues when trying to build outside of the AnyCPU build configuration. Issues so big that even an Undo Pending Changes and Get New couldn’t fix. The build kept failing with the same reason, over and over and over.

An attempt was made to load a program with an incorrect format

I checked all the references, twice. All of them were exactly how they were supposed to be, but this error was damn persistent. Since we couldn’t go back to a previous version this issue had to be solved.

Through the debugger we found that the error was actually occurring the .resx file for the project. After poking around and commenting out some code, we discovered that it was actually and ImageList that was causing all the problems.

After some online sleuthing, it was concluded that the bug was actually from the VS2010 and the .NET framework itself. Our projects all target .NET 2.0, this seems to cause a problem with serialization on the ImageList component. It assumes that you are always using .NET 4.0.

The solution to the problem was easy, we just couldn’t add the component at design time, everything would have to be done dynamically. Presto, it works. Stupid ImageList.

Could not load file or assembly

First off, if you’re encountering this error, you may need to look farther than just here for a solution, especially regarding ASP.NET technology.

I spent a good 3 hours of my work-day scratching my head over how the hell I was going to fix this bug. First thought was to check my assembly references… well, everything checks out there… the program even runs properly in debug mode. The program even runs in damn release mode… but when I move the files to a new location, bam, I get the following error.

“Unhandled exception has occurred in your application…. Could not load file or assembly ‘xxx’ or one of its dependencies. The module was expected to contain an assembly manifest.”

Good god. What does it all mean? Well, before you jump to the assumption that

heh, stupid chump didn’t copy a file

.. I did, multiple times, after multiple rebuilds, cleans, rebuilds, cleans, changing on configuration settings etc. etc. etc. Turns out after the entire ordeal there was an extremely simple solution to the problem.

You see, the directory that I was copying my executable to also had a dll under the exact same name. bar.exe, bar.dll. This is a PROBLEM for my application.

The call structure that I created goes as so : foo.exe -> bar.exe. Extremely simple, done it a thousand times. But my application got the great idea to try and access bar.dll instead, thus creating a gigantic confusing problem that could have easily been avoided by using different assembly names. Oops.

After a quick rename of bar.dll, everything ran smooth as silk.

If you’re reading this and you have a similar problem, hopefully some light has been shed.

Until next time.

C# Comments

There are two different types of comments in C#.

The first of which is a single line comment that as its name states, can only be used on a single line at a time.

//this is a single line comment

If you want to add comments that may take more than one line you have the option of using multiple single line comments.

//you can use
//multiple single line
//comments to make
//a block 

You also have the option of using a block style comment.

/*this
* is 
* a 
* multi-line comment */ 

It is considered good practice to use multiple comments throughout your code. It not only helps other developers understand what you were trying to accomplish, it’s a good reference for you to look back on after putting down a project for a couple of months.

*note* Comments are not taken into consideration by the compiler, multiple comments will not bulk up your end executable.

Hello World C#

Alright, lets get started!

First thing we’ll need is the Visual Studio 2010 Express Edition provided by Microsoft. Don’t worry, you can get the Express Edition for free. The setup is fairly straight forward but may take a few minutes to complete.

Once this is installed, launch the application. If you haven’t run the application before, you might be prompted to configure your workspace environment. You will be given a few options, feel free to choose whichever option feels the most comfortable.

After the workspace has been configured you will see your start page. The start page can be disabled through the tools menu, but for these tutorials we’ll leave it on. On the top left side of the Start Page you will see a “New Project” button, click it. You will be prompted with a new project wizard. Make sure you have C# selected on the left column, and the “Console Application” type selected in the main column. Name the project “Hello_World”. Where you choose to save these tutorials is up to you, as with all things USE MEANINGFUL NAMES and no spaces, underscores are preferred over spaces.

When you click okay the editor will be displayed and you will see.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Hello_World
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

Now, you’re probably thinking “holy crap, what does it all mean”, don’t worry, we’ll work through this. As of right now, this program is pretty much completely useless, so lets add some functionality to it and get things rockin. We will be editing the “Main function during this tutorial, don’t worry about the details of functions just yet, that will come in a later tutorial. The Main Function looks like this:

static void Main(string[] args)
        {
        }

Now, we’ll write our own program!

Between the curly-braces”{ }” of the function add

Console.WriteLine("Hello World");

remember to end the line with a semi colon “;”

your entire program should look like this when you’re done:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Hello_World
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World");
        }
    }
}

When you were typing you probably witnessed a little window popping up. This is the Intellisense technology that was bundled with Visual Studio. You will learn to love Intellisense! We’ll go into that more in a later tutorial.

This program can now be built and tested.  To test your program, press ctrl-f5, or click debug>start without debugging.  If everything worked out properly you should be prompted with a command prompt window displaying the text “Hello World”.

Alright! now that the main program has been created, lets discuss all the little bits and pieces from the top down.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

these four lines are the Namespaces that are utilized by the program. A Namespace is essentially a grouping of common functionality. We’ll dive deeper into these in later tutorials. The next section of code is:

namespace Hello_World
{
  somestuff
}

this is the namespace that our program is contained within. The { and } brackets are used to bunch logical blocks of code together, curly braces and semi-colons are a major part of the C# programming language.

Next up is the class keyword

class Program
    {
        somestuff
    }

a class is an essential mainstay of object oriented programming(OOP), the functionality of a program is contained within one or more classes. We will explore classes in detail later on.

        static void Main(string[] args)
        {
            Console.WriteLine("Hello World");
        }

The last section of code we will discuss is the function, or “method” is C# terms. The Main function is the first method called when the program is run.

The static keyword attached to the function allows for the function to be called without an instance of the class being created, this may sound complicated right now, but we’ll discuss it in detail later.

The void keyword attached to the function indicated that the function does not return a specific value type, eg. int, string, float, double, etc… “Main” is the name of the function, attached to it are parameters that can be passed to the method, these are surrounded by parenthesis ( ). We won’t really discuss the details of the parameters right now, as this is just an simple introduction to the language.

The final bit to discuss is

Console.WriteLine("Hello World");

This is the line responsible for the output on screen. WriteLine is a method contained within the Console object. You are passing a string literal parameter for the method to display. As shown before, the semi-colon marks the end of a statement.

That about it for the Hello World tutorial. Experiment using the WriteLine method until you are comfortable then move onto the next tutorial when you are ready. We will be discussing the concept of variables and comments next.

Introduction:

During the last few weeks I’ve been trying to learn the basics of the C# language.  So far my journey has taken me deep into a fundamental understanding.  My goals for this blog are to share the knowledge I have gained as well as provide custom examples of some sections of code I’ve been working on.  With that being said, keep in mind that I am still learning this craft and have a long way to go before becoming an expert of the material.  If you see anything in this blog that raises and eyebrow please don’t hesitate to comment, I am eager to learn just as much as I am to teach.

Follow

Get every new post delivered to your Inbox.