Monday, December 14, 2015

Class Widget: Create Class From Method

Its feature is pretty self-explanatory, creating a class from a method. The new class will be in the same project and within the same namespace, where the type of the method of concern is within. The new class name along with the file name can be specified.

If something like the following is written anywhere in a class,

public bool? IsHelthy(int age, double weight, string type)
{
switch (type)
{
case "cat":
return weight > age * 5;
case "dog":
return weight > age * 10;
default:
return null;
}
}

        the cursor is inside the IsHelthy method, and the Create Class From Method command is called up, and a class/file name is provided properly, e.g. 'Pet', the following class will be created:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CS_Class
{
public class Pet
{
public bool? IsHelthy()
{
switch (type)
{
case "cat":
return weight > age * 5;
case "dog":
return weight > age * 10;
default:
return null;
}
}

private string type;
private double weight;
private int age;
}
}

The namespace of the new class is the same as the type of concern is. In addition, all necessary namespaces are automatically brought in. The code compiles right away. Isn't it terrific?

Enjoy!

FREE Download and Try Visual Smarter 2015

No comments:

Post a Comment