{
value = 100 - value;
continue; // Why???
}
else
{
value = value + 100;
}
!!!FAIL!!!
!!!FAIL!!!
Let's update all the views, when there is no need to update.. As we can see high quality comments describe what this piece of shit does.
!!!FAIL!!!
!!!FAIL!!!
Without any comment, let’s go to the hell:
Nice combination of using goto and switch statement together. I hope I will never work with the author.
!!!FAIL!!!
The question is only that: Why???
Why the hell he doesn’t go and play football instead of programming?
I use the great example from Gotreportviewer since about six month that uses Microsoft Reportviewer and my biggest problem is giving columns titles that different to field names.
There is my solution that has an additional property: List<string> Headers that contains column titles with the following restriction:
Number of headers must be equal to number of columns or TableRdlGenerator throws the following exception:
Error creating report: header and field columns must be set and have to be the same count of elements both of them!
The entire solution can be downloaded from my public folder on skydrive.
After I have been started to learn jquery I decided to replace my Ajax Control Toolkit controls for a jQuery-based solution.
My control for replace in that week is the CollapsiblePanelExtender. I need a solution that works on partial page postbacks and full page postbacks too and stores collapsed state during postbacks, and, of course I have to be able to change the collapse state on server side so I had to mix server and client side code for the solution.
Note: at the end of the post, I share the zipped full Visual Studio 2010 solution.
The extender needs the following informations:
The code behind of the web user control:
In the markup we need to include our javascript files. For getting this work on partial page and full page postbacks and any other cases we rendering script block in InlineScripts as shown in my previous post before. We store collapsed state in a simple asp.net hiddenfield because we can reach it’s value on server and client side too.
Based on these the markup for the web user control is:
We have to register our control in the web.config before using:
A simple example page for testing:
And the code behind of the page:
And at last the full source code of the solution can be downloaded my public folder on skydrive.
There are many ways to add script block to our asp.net pages. This is a very simple problem until we had got a complex project with custom controls, partial page rendering, updatepanels and dynamically loaded controls.
We have to add script blocks different ways if we asynchrony post backs or full page post backs.
The other problem is when the script block don’t have to load at the first page load, it depends on some logic dynamically.
After many trying I use this solution since about half year and it works at any case.
The simple and easy to use source code is:
After registering into web.config, we can use it easily everywhere on our program:
There is a really great add-on for Visual Studio named Productivity Power Tools. It helps a lot during my work but hides some information that so important for me: the list of throwable exceptions.
At the university I didn’t have a choice. I had to learn Java. Java is a great language and has a restriction that I like so: you must specify the list of the throwable exceptions at the specification of each method. I work with C# nowadays. It doesn’t has this rule but Visual Studio tries to help us on a simple way: it shows us this list when we hover on the name of the given method with the mouse like this:
As we can se, we now a bit closer to the Java restrictions. We can find out fast the given list but the language still don’t force us into handle all exceptions. It smart or not – I don’t know. But in fact I’m happy to see this list.
There is a bit problem with the power tools: It gives us a new window for our Visual Studio, named Solution Navigator. It has a lot of functions among others coloring the hints that I shown before:
Ops! As we can see, the list of throwable exceptions has disappeared. We have an other function instead of this, every can try this out by clicking on the triangle at the right side of the popup.
At this time we have two choice. We can use our Visual Studio with these settings or we can disable this function by setting of on the Visual Studio menu –> Tools –> Options –> Productivity Power Tools –> Solution Navigator. We got the original function after restarting our development environment.
Remarks: Solution Navigator is so slow. Sometimes useful but the most part of my times it only slows down my work without any benefits.
// Using object initializer Person demoP = new Person() { ID = 0, Name = "Happy", Age = 1 };
Person oldDemoP = new Person(); oldDemoP.ID = 0; oldDemoP.Name = "Happy"; oldDemoP.Age = 1;
public class Person { public int ID { get; set; } public string Name { get; set; } public int Age { get; set; } }
public class PersonDS { public object ID { get; set; } public object Name { get; set; } public object Age { get; set; } }
List<PersonDS> database = new List<PersonDS>(); database.Add(new PersonDS() { ID = 1, Name = "Peter", Age = 18 }); database.Add(new PersonDS() { ID = 2, Name = "Liza", Age = null }); database.Add(new PersonDS() { ID = 3, Name = "Cloe", Age = 16 });
// Old way to read datas try { List<Person> oldPersons = new List<Person>(); foreach (var datarow in database) { Person p = new Person(); p.ID = (datarow.ID as int?).Value; p.Name = datarow.Name as string; p.Age = (datarow.Age as int?).Value;// Throws Exception at value assignment } } catch (Exception exc) { Console.WriteLine(string.Format("Message: {1}\nStackTrace: {1}"
, exc.Message, exc.StackTrace)); }
// The new way to read datas try { List<Person> newPersons = (from p in database select new Person() {// Throws Exception at object initialize ID = (p.ID as int?).Value, Name = p.Name as string, Age = (p.Age as int?).Value }).ToList(); } catch (Exception exc) { Console.WriteLine(string.Format("Message: {1}\nStackTrace: {1}"
, exc.Message, exc.StackTrace)); } }
As we can see, we get our exceptions in both cases. But at the old way, we can see that we have a missing Age value at the first look. With object initializers, we see only that we got an error at the initialize. And we have to find out what was the error exactly. We can find many problems with this example: at the real life, I never used this form to read data from database: p.Age = (datarow.Age as int?).Value;// Throws Exception at value assignment
There are many ways to do this properly but the target is showing a simple problem with object initializers: error messages are not enough well detailed. In real life we usually don't have objects with only three properties. And we can get data with many ways: databases, xmls, streams, user input, ...
The conclusion is: let's type a bit more but make error safe code. When we should bet our month salary for that property and want to say: 'Oh, it must be filled', don't be lazy and handle the possibility when it's not and we can sit down and open a beer.
Morzel
The entire code example:using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ObjectInitializersVSOldWays { public class Person { public int ID { get; set; } public string Name { get; set; } public int Age { get; set; } } public class PersonDS { public object ID { get; set; } public object Name { get; set; } public object Age { get; set; } } class Program { static void Main(string[] args) { // Using object initializer Person demoP = new Person() { ID = 0, Name = "Happy", Age = 1 }; Person oldDemoP = new Person(); oldDemoP.ID = 0; oldDemoP.Name = "Happy"; oldDemoP.Age = 1; List<PersonDS> database = new List<PersonDS>(); database.Add(new PersonDS() { ID = 1, Name = "Peter", Age = 18 }); database.Add(new PersonDS() { ID = 2, Name = "Liza", Age = null }); database.Add(new PersonDS() { ID = 3, Name = "Cloe", Age = 16 }); // Old way to read datas try { List<Person> oldPersons = new List<Person>(); foreach (var datarow in database) { Person p = new Person(); p.ID = (datarow.ID as int?).Value; p.Name = datarow.Name as string; p.Age = (datarow.Age as int?).Value;// Throws Exception at value assignment } } catch (Exception exc) { Console.WriteLine(string.Format("Message: {1}\nStackTrace: {1}", exc.Message, exc.StackTrace)); } // The new way to read datas try { List<Person> newPersons = (from p in database select new Person() { // Throws Exception at object initialize ID = (p.ID as int?).Value, Name = p.Name as string, Age = (p.Age as int?).Value }).ToList(); } catch (Exception exc) { Console.WriteLine(string.Format("Message: {1}\nStackTrace: {1}", exc.Message, exc.StackTrace)); } } } }And the output is:Message: at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource) at System.Nullable`1.get_Value() at ObjectInitializersVSOldWays.Program.Main(String[] args) in D:\--- Souce ---\Blog\ObjectInitializersVSOldWays\ObjectInitializersVSOldWays\Program.cs:line 41 StackTrace: at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource) at System.Nullable`1.get_Value() at ObjectInitializersVSOldWays.Program.Main(String[] args) in D:\--- Souce ---\Blog\ObjectInitializersVSOldWays\ObjectInitializersVSOldWays\Program.cs:line 41 Message: at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource) at System.Nullable`1.get_Value() at ObjectInitializersVSOldWays.Program.<Main>b__4(PersonDS p) in D:\--- Souce ---\Blog\ObjectInitializersVSOldWays\ObjectInitializersVSOldWays\Program.cs:line 54 at System.Linq.Enumerable.WhereSelectListIterator`2.MoveNext() at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) at ObjectInitializersVSOldWays.Program.Main(String[] args) in D:\--- Souce ---\Blog\ObjectInitializersVSOldWays\ObjectInitializersVSOldWays\Program.cs:line 52 StackTrace: at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource) at System.Nullable`1.get_Value() at ObjectInitializersVSOldWays.Program.<Main>b__4(PersonDS p) in D:\--- Souce ---\Blog\ObjectInitializersVSOldWays\ObjectInitializersVSOldWays\Program.cs:line 54 at System.Linq.Enumerable.WhereSelectListIterator`2.MoveNext() at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) at ObjectInitializersVSOldWays.Program.Main(String[] args) in D:\--- Souce ---\Blog\ObjectInitializersVSOldWays\ObjectInitializersVSOldWays\Program.cs:line 52
using System; using System.Linq; using DotNetNuke.Entities.Controllers; using DotNetNuke.Entities.Modules; using DotNetNuke.Services.Exceptions; using DotNetNuke.Data;
<asp:Label runat="server" ID="lblSetting" BackColor="AntiqueWhite"/>
var dp = DataProvider.Instance();// -2 Not an existing user
// Signature: AddHostSetting(
// string SettingKey// , string SettingValue// , bool SettingIsSecure// , int createdByUserID);dp.AddHostSetting("TestKey", "TestValue", false, -2);
lblSetting.Text = HostController.Instance.GetString("TestValue");
4. Open a beer and be happy :)