c#

A 3-post collection

Redux for C#.NET 2.0

Refer to : github.com/phizaz/redux-dotnet2.0

I made this piece of code because those redux libraries just don't support .NET 2.0. If you are in the same situation as I was, this piece of code might shed some light on your project.

If you don't know what Redux is ? Educate yourself with http://redux.js.org, Yes! originally it was designed for javascript web development. Many people found this too good to be kept only in the js world, including me.

I don't intend for this to be a full-fledged redux library, I developed this just for a proof of concept that redux is really compatible with .NET 2.0. Feel free tailor the code to suit your need :D

What's included

  1. Libs
    1. class Act, every action should extend this class
    2. class State, every state should extend this class
    3. class Store, every store should extend this class
    4. class Reducer, every reducer should extend this class
      • each methods in classes extending this class, should be Reducer functions in the form of public <StateClass> Reduce(<StateClass> s, <ActClass> act) {..}
    5. class ReduceRunner, create an instance of this class and use it to run reduce functions (keeping the program responsive while reducing).
  2. Examples
    1. Calculator program
    2. GUI Calculator program

Examples

Calucalator

This should be an oversimplified calculator application supporting only a plus operation, and have no UI, just for demonstration purposes.

All the actions for this application are defined in the file calculator/act.cs

public class ActPlus : Act { public int a; public int b; }  
public class ActClear : Act { }  

And now our state calculator/state.cs

public class CalculatorState : State<CalculatorState>  
{
    public bool has_result = false;
    public int result;
}

Reducer functions should be defined in calculator/reducer.cs

public class CalculatorReducer : Reducer<CalculatorState, CalculatorStore&
อ่านต่อ »

C# DateTime without Timezone and Millisecond

Without Millisecond

From http://stackoverflow.com/questions/1004698/how-to-truncate-milliseconds-off-of-a-net-datetime:

dateTime = dateTime.AddTicks( - (dateTime.Ticks % TimeSpan.TicksPerSecond));  
Without Timezone

From http://stackoverflow.com/questions/3188933/prevent-timezone-conversion-on-deserialization-of-datetime-value:

DateTime dateTime = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Unspecified);  
อ่านต่อ »

Convert Two-Digit Year to Four-Digit Year

In C#.NET, From http://stackoverflow.com/questions/2024273/convert-a-two-digit-year-to-a-four-digit-year:

The .NET framework has a method that does exactly what you want:

int fourDigitYear = CultureInfo.CurrentCulture.Calendar.ToFourDigitYear(twoDigitYear)  

Note : Beware of the date setting on each machine can be vastly different, since this CultureInfo is based on the machine setting. The result of this function can be unpredictable, e.g. on machines using Thai locale, BE is a preferred epoch, but it doesn't make any sense with ToFourDigitYear.

Workaround : You can set the current thread's CultureInfo to something you're sure of the CultureInfo behavior and then perform the call. After that return the current thread's CultureInfo back to the old one.

อ่านต่อ »