Skip to main content

Posts

Showing posts from December, 2011

C# Learning: using nullable types

Let’s say your program needs to work with a date and time value. Normally you’d use a DateTime variable. But what if that variable doesn’t always have a value? That’s where nullable types comes in really handy. All you need to do is add a question mark (?) to the end of any value type, and it becomes a nullable type that you can set to null. bool? myNulableInt = null; DateTime? myNullableDate = null; You will find out that the question mark T? is an alias for Nullable<T>.

C# Learning: strut

One of the types in .NET we haven’t talked about much is the struct. struct is short for structure, and structs look a lot like objects. They have fields and properties, just like objects. And you can even pass them into a method that takes an object type parameter: A struct looks like an object… …but isn’t an object public struct Dog { public string Name; public string Breed; public Dog(string name, string breed) { this.Name = name; this.Breed = breed; } public void Speak() { Console.WriteLine(“My name is {0} and I’m a {1}.”, Name, Breed); } } But structs aren’t objects. They can have methods and fields, but they can’t have finalizers. They also can’t inherit from other classes or structs, or have classes or structs inherit from them. Structs are best used for storing data, but the lack of inheritance and references can be a serious limitation. But the thing that sets structs apart from objects more than almost anything else is that you copy them by value

C# Learning: LINQ

C# has a really useful feature called LINQ (which stands for L anguage IN tegrated Q uery). The idea behind LINQ is that it gives you a way to take an array, list, stack, queue, or other collection and work with all the data inside it all at once in a single operation. But what’s really great about LINQ is that you can use the same syntax that works with collections as you can for working with database LINQ makes working with data in collections and databases easy. - to be updated later...

C# Learning : exception handling

In .NET, when an exception occurs, an object is created to represent the problem. It’s called, no surprise here, Exception. When your program throws an exception, .NET generates an Exception object. Exceptions are all about helping you find and fix situations where your code behaves in ways you didn’t expect. In C#, you can basically say, “Try this code, and if an exception occurs, catch it with this other bit of code.”

C# learning: about the serialization

I've been using seriazation for several years, this is the best explaination for me about this theory. Yes, It saves the complete state of the object, so that an identical instance can be brought back to life on the heap later! When an object is serialized, all of the objects it refers to get serialized, too. It is actually very cool.

C# Learning: IEnumerable

When a collection implements IEnumerable<T>, It’s giving you a way to write a loop that goes through its contents in order. E.g. When you write a foreach loop, you’re using IEnumerable, as below, foreach (Duck duck in ducks) { Console.WriteLine(duck); } public Deck(IEnumerable<Card> initialCards) { cards = new List<Card>(initialCards); }

C# Learning: Sort a list

The List.Sort() method knows how to sort any type or class that implements the IComparable<T> interface. That interface has just one member—a method called CompareTo(). Sort() uses an object’s CompareTo() method to compare it with other objects, and uses its return value (an int) to determine which comes first. e.g. class Duck : IComparable<Duck> { public int Size; public KindOfDuck Kind; public int CompareTo(Duck duckToCompare) { if (this.Size > duckToCompare.Size) return 1; else if (this.Size < duckToCompare.Size) return -1; else return 0; } } List<Duck> ducks = new List<Duck>() { new Duck() { Kind = KindOfDuck.Mallard, Size = 17 }, .... } ducks.Sort(); Or you can use IComparer to tell your List how to sort, e.g. class DuckComparerBySize : IComparer<Duck> { public int Compare(Duck x, Duck y) { if (x.Size < y.Size) return -1; if (x.Size > y.Size) return 1; return 0; } } DuckComparerBySize sizeComparer

C# Learning: Dictionary vs Java Map

A list is like a big long page full of names. But what if you also want, for each name, an address? Or for every car in the garage list, you want details about that car? You need a dictionary. A dictionary lets you take a special value—the key—and associate that key with a bunch of data—the value. And one more thing: a specific key can only appear once in any dictionary. Dictionary <Tkey, TValue> kv = new Dictionary <TKey, TValue>() Java uses Map or table(like HashTable) to deal with key/value case. Still, too many options's in Java World.

C# Learning: List and for

List<Shoe> shoeCloset = new List<Shoe>(); shoeCloset.Add(new Shoe() { Style = Style.Sneakers, Color = “Black” }); .... foreach (Shoe shoe in shoeCloset) { ... } shoeCloset.RemoveAt(4); if (shoeCloset.Contains(secondShoe)).. Not like java, having many kinds of List, C# only have one, which is way more easier for developers like me to use. Thanks.

C# Learning: interface and abstract class

Interface names start with I Whenever you create an interface, you should make its name start with an uppercase I. There’s no rule that says you need to do it, but it makes your code a lot easier to understand. You can see for yourself just how much easier that can make your life. Just go into the IDE to any blank line inside any method and type “I”—IntelliSense shows .NET interfaces. interface IStingPatrol { int AlertLevel { get;} int StingerLength { get; set;} bool LookForEnemies(); int SharpenStinger(int length); } When you mark a class abstract, C# won’t let you write code to instantiate it. It’s a lot like an interface—it acts like a template for the classes that inherit from it. abstract class abc{}

C# Learning: subclass

You extend a class by adding a colon to the end of class declaration, followed by the base class to inherit from. class Bird { public void Fly() { // here’s the code to make the bird fly } public void LayEggs() { ... }; public void PreenFeathers() { ... }; } class Pigeon : Bird { public void Coo() { ... } } A subclass can override methods to change or replace methods it inherited Sometimes you’ve got a subclass that you’d like to inherit most of the behaviors from the base class, but not all of them. When you want to change the behaviors that a class has inherited, you can override the methods. A subclass can only override a method if it’s marked with the virtual keyword, which tells C# to allow the subclass to override methods. class Bird { public virtual void Fly() { // code to make the bird fly } } Add a method with the same name to the derived class You’ll need to have exactly the same signature—meaning the same return value and parameters—and you’ll

C# Learning: get/set accessors and modifiers vs Java

public const int CostOfFoodPerPerson = 25; public int NumberOfPeople { get { return numberOfPeople; } set { numberOfPeople = value;  // I think value is a hidden object in the calss. } } Java: I can only implement the const by define the variable like below, public final static  int CostOfFoodPerPerson = 25; Access Levels Modifier Class Package Subclass World public Y Y Y Y protected Y Y Y N no modifier Y Y N N private Y N N N

C# Learning: References

When your code needs to work with an object in memory, it uses a reference, which is a variable whose type is a class of the object it’s going to point to. A reference is like a label that your code uses to talk about a specific object. This is also apply in Java World. May I say C# is the subset of Java? I guess it's yes in some way.

C# Learning: variable's types vs Java

int can store any whole number from –2,147,483,648 to 2,147,483,647. string can hold text of any length (including the empty string “”). bool is a Boolean value—it’s either true or false. double can store real numbers from ±5.0 × 10−324 to ±1.7 × 10308 with up to 16 significant figures. That range looks weird and complicated, but it’s actually pretty simple. The “significant figures” part means the precision of the number: 35,048,410,000,000, 1,743,059, 14.43857, and 0.00004374155 all have seven significant figures. byte can store any whole number between 0 and 255. sbyte can store any whole number from –128 to 127 short can store any whole number from –32,768 to 32,767. ushort can store any whole number from 0 to 65,535. uint can store any whole number from 0 to 4,294,967,295. long can store any whole number between minus and plus 9 billion billion. ulong can store any whole number between 0 and about 18 billion billion. Java's counterpart, Primitive Type Siz

C# Learning: NameSpace, Class vs java

You do something like below in C#, File 1: MoreClasses.cs using System; using System.Linq; using System.Collections.Generic namespace PetFiler2 { class Fish { public void Swim() { // statements } } partial class Cat { public void Purr() { // statements } } } File 2: SomeClasses.cs namespace PetFiler2 { class Dog { public void Bark() { // statements go here } } partial class Cat { public void Meow() { // more statements } } } In java: import is the counterpart of using, and package is the other for namespace. In my knowledge, I can not find the implementation for partial class before JDK 7 (I don't have too much idea about JDK7)

C# Learning: variables vs java

If you write code that uses a variable that hasn’t been assigned a value, your code won’t compile. It’s easy to avoid that error by combining your variable declaration and assignment into a single  statement. However, in java, you can do something like int index; and it will set default value 0.

Some QAs about SiteCore - Part 2

Q.What is an Item?  An item is addressable unit of content. Addressable means that the item has a path in the Sitecore Content Tree. Q.Editing text on the page is called? Inline Editing with Page Editor. Q.Why do designers use Page Editor? Designers use Page Editor to make design changes. Page Editor features Design Mode. Q. Describe the anatomy of the Ribbon in the Content Editor. The Content Editor Ribbon consists of Tabs. In each Tab are Groups. In each Group are commands. The Content Editor Ribbon is customizable. Users can hide Tabs as well as create their own tab featuring various commands. Q. Name two text editors. Rich Text and Word. There is also just plain text for multiline and single-line text fields. Q. Name a few Sitecore interfaces for Developers. Developers create data templates with Template Manager. Developers edit sublayouts and layouts with Grid Designer. Developers use the Control Panel to install packages. Q.What is Grid Designer? The Grid Des

Some QAs about SiteCore - Part 1

Q: What is the difference between the Content Management Server and the Content Delivery Server? The Content Management Server is where content editors create content. It features both the Master and the Core databases. The Content Delivery Server is for web site visitors. It features the Web database (and core database asp.net membership model by default). Q: How SiteCore deals with data? Sitecore reads data from the database and represents it in memory as XML. Developers *never* write SQL statements. Instead, they use the API to access the XML representation. Q: What are the three Databases used by SiteCore? A standard Sitecore installation uses three databases: Master All versions of content, all languages, published and unpublished in any workflow state. This is the work-in-progress database. Web Latest version of published content that has completed workflow. This is the live database. Core Configuration information for the Sitecore user interfaces. ASP.NET memb

Microsoft C# and me

It seems no more Java development project in the near future in the company.  Time for me to learn other great language to satisfy my career and help company working on other great projects. In the following days, I will learn some books about C# , asp.net and share my C# travel with you. I used to be a C++ programmer in the very beginning of my development career(from 2000 - 2002) using tool like Visual Studio C++ 6.0 and Borland C++ Builder 4.0. From my inner heart, C++ is not bad, it made me control everything like memory, arrays, pointer, it's fantastic. I attended/passed the tests for Visual Studio C++, Sql Sever 7.0 and I am really glad to come back to Microsoft's hug for a while or longer. Cheers, my C#

Java's Date Classes Must Die.

Java's Date Classes Must Die . Problem Joda Databases Solution Examples Jar Javadoc Source BSD WEB4J DATE4J But, soft! what code in yonder program breaks? It is the Date, and dim grows the Sun! Arise, fair Sun, and kill the egregious Date, Who is already sick and pale with grief, That we, perchance, art more fair than she. This site offers a Java tool called date4j . It's an alternative to Date , Calendar , and related Java classes. The JDK's treatment of dates is likely the single most suctorial aspect of the Java core libraries. It needs improvement. The main goals of date4j are : easy manipulation of dates/times in the Gregorian calendar (the civil calendar used in almost all countries). easy storage and retrieval of such dates/times from a relational database. a simplified model of civil timekeeping, similar to the model used by many databases. Problem For reference, here are the JDK classes related to

How to solve QuerySyntaxException (table is not mapped) in hibetnate?

Got following exception in my console, org.hibernate.hql.ast.QuerySyntaxException: ResultType is not mapped [from ResultType r WHERE r.eventType.eventTypeCode = :eventType AND r.resultTypeCode.resultTypeCode= :resultType] at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:181) at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:110) at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:93) at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:277) at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3056) at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2945) at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:688) at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:544) at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalke