Contents tagged with C#

  • Trivial implementation of File uploader

    Hello everybody,

    today I want to share with you fragment of code of html extension.

    Imagine that you edit your cshtml file, and see following code:

    @Html.EditorFor(model => model.SomeField

    Then you need to add some file upload and notice, that there is not file upload! 

    How can you fix this situation?

    I propose to use HtmlExtension code. For example like this:

    public static MvcHtmlString FileUpload(this HtmlHelper htmlHelper, string cssClass, object htmlAttrs = null) {

    var tb = new TagBuilder("input");

    tb.MergeAttribute("type", "file");

    tb.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttrs));

    if (!string.IsNullOrWhiteSpace(cssClass)) {

    tb. … more

  • Usage of Factor in R

    Hello everybody,

    today I want to make post about usage of factor in R.

    So, first of all, what is factor at all? 

    I was surprised to see that factor is very overloaded term and can have a lot of meanings in many areas.  So, for what factors can be used in R?

    First of all they can be used for storing categorical or nominal values. For example it can be convenient for saving gender, blood group, car type, etc. 

    So, let's say you wrote something like this: 

    cars.classes <- c("Coupe", "Cabriolet", "Sport", "Hatchback")

    For now cars.classes is not a factor, but just character vector. 

    If you execute this code in RStudio, and then execute in RStudio following line:

    cars.classes … more

  • Coercion in programming

    Hello everybody,

    just one short notice. Sometime in programming you can read word coercion. 

    You may be wondering what it means. 

    If to put simply, it is converting of one type into the other. 

    In C# coercion can be of two types: 

    implicit and explicit. 

    double a,b;

    a = 25; // implicit coercion

    b = (double) 7; //explicit coercion ( casting)

    more

  • The Adapter Pattern

    Hello everybody,

    today I want to make post about one of design patterns which is called Adapter. There are many articles over the web about this topic, but I hope to bring some clarity in this area.

    Before I proceed any further, let me describe situation, when you can need it. One of the cases for consideration can be option, if you have your own class, let's name it A. And then let's say that there is some other class B, which has some needed function for you. For example like this:

    How you can deal with this? One of the common ways which many people do, is to use class be directly in your code. For example like this:

    Aggregate class B from class A in some kind of function, and do … more

  • Moq and unit test

    Hello everybody,

    today I want to write few words about Moq and unit testing. 

    First of all you can have a question, why do I need Moq at all?

    Moq is especially useful for cases if we don't have desire to code interface, then code implementation of interface, and then code fake implementation. As usually those simple implementation are relatively simple so somebody ( as Moq team ) can organize code generation for your simple interface implementations.

    So, unit test is something that should follow 3 A:

    Arrange

    Act

    Assert

    General schema of Unit test implementation can be like this:

    [Test]

    public void SomeTest()

    {

    //Arrange

    var … more

  • Override Equals method of value types

    Hello everybody.

    Today I want to give some demo.

    using System;

    using System.Diagnostics;

    namespace StackOverflowQuest

    {

    class Program

    {

    struct StructTest

    {

    public string TestString { get; set; }

    //public override bool Equals(object obj)

    //{

    // var ct = (StructTest)obj;

    // return ct.TestString == this.TestString;

    //}

    }

    class ClassTest

    {

    public string TestString { get; set; }

    public override bool Equals(object obj)

    {

    var ct = obj as ClassTest;

    if (ct == null) … more

  • Search for duplicates

    Hello everybody,

    Here is just shorp version of application which searches for duplicates in folder.

    In  downloadable archive you can find my implementation of searcher with Task, ContinueWith and some other new features of C# 4.5 more