since we had a chance to go through Computer Programming history and had a sight view on how computer language is essential to Revit API. Today we will touch the concept of writing a program, but first let me explain some sort of things.
![](https://images.freeimages.com/images/large-previews/595/law-education-series-3-1467430.jpg)
Dll is an abbreviation for a Dynamic Link Library. you can say it is like a book where you give your computer the required knowledge to compute and gives back a result. A very good Explanation is here The Tech Terms Dictionary We will mention about it again in this post.
Start your Visual Studio and click on create new Project
![](https://tutorials.visualstudio.com/vs-get-started/img/window-overview.png)
and Finally you will reach to the below image. Up to here we are now ready to write our program. lets understand this interface first:
Before Writing any Program, there are some concepts you need to be aware of these concepts:
Namespace, Class, EntryPoint.
there are a lot of useful information in different websites, for the Namespaces, and classes. already linked above, but I will write it in my own way, may be it deliver a better perspective.
Let me tale you a story which will get things and idea more clearer to you.
using System.Drawing;
namespace LAI01_BallVolume
{
//this is a class that contains information about a typical Ball.
public class Ball
{
public double Radius;
public Color BallColor;
public double GetVolume()
{
return (4 / 3) * Math.PI * Math.Pow(Radius, 3);
}
}
public class Program
{
private static void Main(string[] args)
{
//this is the entry Point of the Program
var myBall = new Ball();
myBall.Radius = 4;
myBall.BallColor = Color.Red;
Console.WriteLine($"Your Ball {myBall.BallColor} has a Volume of {myBall.GetVolume()} CubicMeter.");
Console.ReadLine();
}
}
}```
What is Additional Library:
Like we mentioned before, Computer, as a digital device, doesn't know anything unless you started feeding it with information. Under the scope of this application, computer doesn't know anything about:
Methods:
are containers of Code Statements. Methods must have a name. Name must not start with a digit or Hyphen. for example the Get Ball Method. Read more about Methods here.
Methods optionally returns something and parameters. like the example above GetVolume() returns double, but has no Parameter
```public double GetVolume()
{
return (4 / 3) * Math.PI * Math.Pow(Radius, 3);
}```
Method Signatures:
You also noticed `static` and `public` signatures. `public` is used to allow calling outside its boundary. in this program you have a Ball Class and Program Class. in order to allow classes call methods/properties of other classes, public signature must be provided.
we will discuss static in another post.
![Wyvern-programming-languages-in-one](https://upload.wikimedia.org/wikipedia/commons/2/24/Wyvern-programming-languages-in-one.jpg)
What skills I do need to Start Revit API?
In fact, I as well have asked this question long time back. Some of the answers I received was “NO you cannot. You need to be a computer programmer specialist.” others said “Pooh! I think it will take too long time for you to study something out of your career.” - Well if you received the same answers, then drop them all behind. Nothing is impossible, just keep finding your path and you will definitely succeed. Well, I advise beginners to touch the basic concepts of Programming and try to write a very small typical application such as “Calculator”. Once you are happy with that then start crunching your head with Revit API. in this post we will take you to write your first program.
For now I need you to install an IDE please visit our Post for installation guideline.
Project Types
There are many applications around the world, that we are using daily. Web, Windows, Library, Android... etc applications. For example the webpage you are reading now is a web application. in order to create your first program you need to know what type of application you will write as well as what language. A quick helpful definitions
Console Application:
It is a application that only deal with text. input from user, and output from the computer. we will use this type in our today's tutorial.
Defined in techopedia as:
A console application, in the context of C#, is an application that takes input and displays output at a command line console with access to three basic data streams: standard input, standard output and standard error.
A console application facilitates the reading and writing of characters from a console - either individually or as an entire line. It is the simplest form of a C# program and is typically invoked from the Windows command prompt. A console application usually exists in the form of a stand-alone executable file with minimal or no graphical user interface (GUI).
Windows Application:
is any application that uses the Window `GUI`
A program that is written to run under the Microsoft Windows operating system, also called a "Windows app." All 32-bit Windows applications run in the 32-bit and 64-bit versions of Windows. All 64-bit applications require 64-bit Windows, which is the standard on new Windows computers and tablets.~PCMag
Dll Library:
![](https://images.freeimages.com/images/large-previews/595/law-education-series-3-1467430.jpg)
Let's setup our First Program:
![]() |
Set your Project name, and Folder Location |
![](https://tutorials.visualstudio.com/vs-get-started/img/window-overview.png)
and Finally you will reach to the below image. Up to here we are now ready to write our program. lets understand this interface first:
Visual Studio User Interface:
The Code editor is where you write your code.for more information about `VSUI`, see here
The Solution explorer shows the files you’re working with.
The Properties pane gives additional information and context about selected parts of your project.
The Output window displays debugging and error messages, compiler warnings, status messages, and other output.
Before Writing any Program, there are some concepts you need to be aware of these concepts:
Namespace, Class, EntryPoint.
there are a lot of useful information in different websites, for the Namespaces, and classes. already linked above, but I will write it in my own way, may be it deliver a better perspective.
![](https://images.freeimages.com/images/large-previews/99f/book-1309944.jpg)
I decided to write a book about my first Architecture project. My book will require papers of size A3 and A4. I went to my desk, and picked 5 A4 papers and 2 A3 papers, then I started writing. During writing, I realized that i need to Reference more information about projects done by others. Since, these references are not mine, I add a reference at the footnote. To make my book understandable, I added a note to readers "You need to read the first page before reading the rest of the book". Finally, I did finish writing and used all the papers. I then bound all the papers with a hard Cover and title my book as "My First Project".now lets translate that to Computer World:
Story argument | C# argument |
---|---|
Hardcover | Namespace |
book Title | Namespace Name |
My book will require papers of size A3 and A4 | 2 class types A4 and A3 |
picked 5 A4 papers and 2 A3 papers | Create 5 instances of A4 class and 2 instances of A3 class |
read the first page before reading the rest | Entry Point |
add a reference at the footnote | Link/Add Libraries to your Project |
Namespace
is a boundary to your program, that contains all the required classes to run the program. They can be nested, meaning a namespace under namespace. They also must have a name and the name must not start with digits or hyphen
```namespace MyBook
{
//some classes will be defined here
}
```
{
//some classes will be defined here
}
```
Class
is a template that you can instantiate objects from. like A4 paper is a class, but we instantiated page 01,02----05. basically, a class must have a name and the name must not start with digits or hyphen. class is a container of properties that describe an instantiated object. for example, a typical A4 paper has the following properties width, height, back Color, paper type ...etc you can add properties as much as your program needs.
```public class A4Paper
{
public double Width=21;
public double Height=29;
public Color BackColor=Colors.White;
}
```
{
public double Width=21;
public double Height=29;
public Color BackColor=Colors.White;
}
```
Entry Point:
Any computer program must have a starting point and end point. it is like pressing the power button to run your PC and closing it when you are done. Entry Points differs depending on the type of the Application. since our discussion today is about Console Application then the entry point is
```static void Main(string[] args)
{
}
```
{
}
```
You can't change the entry Point to something else, otherwise, the application will keep looking for the `Main(string[] args)` method, and then crash.
Application startup occurs when the execution environment calls a designated method, which is referred to as the application's entry point. This entry point method is always named Main...
~C# specification
Now lets start writing our first Program.
We need an application that calculate the volume of a sphere when we advise it with a radius.
let's break the above:
Does the computer know anything about what a ball is? NO
Does the computer know anything about calculation in specific Volume? NO
Your application knows nothing because you feed it with nothing. so we have to do the following:
- Create a class Name: `Ball`
- Add a double property Name: `Radius`
- Add a double property Name: `BallColor`
- Add a method `double GetVolume()`
- Go to entry Point.
- Instantiate your a ball class.
- Assign the new ball variables
- Call `myball.GetVolume();`
And your Program should be like this:
```using System;using System.Drawing;
namespace LAI01_BallVolume
{
//this is a class that contains information about a typical Ball.
public class Ball
{
public double Radius;
public Color BallColor;
public double GetVolume()
{
return (4 / 3) * Math.PI * Math.Pow(Radius, 3);
}
}
public class Program
{
private static void Main(string[] args)
{
//this is the entry Point of the Program
var myBall = new Ball();
myBall.Radius = 4;
myBall.BallColor = Color.Red;
Console.WriteLine($"Your Ball {myBall.BallColor} has a Volume of {myBall.GetVolume()} CubicMeter.");
Console.ReadLine();
}
}
}```
Points to Attention:
This program structure would look like this:
Like we mentioned before, Computer, as a digital device, doesn't know anything unless you started feeding it with information. Under the scope of this application, computer doesn't know anything about:
- Math.PI
- Math.POW(..,..)
- Color.Red
- Console.WriteLine(....)
- System.dll and
- System.Drawing.dll...
Methods:
are containers of Code Statements. Methods must have a name. Name must not start with a digit or Hyphen. for example the Get Ball Method. Read more about Methods here.
Methods optionally returns something and parameters. like the example above GetVolume() returns double, but has no Parameter
```public double GetVolume()
{
return (4 / 3) * Math.PI * Math.Pow(Radius, 3);
}```
Method Signatures:
You also noticed `static` and `public` signatures. `public` is used to allow calling outside its boundary. in this program you have a Ball Class and Program Class. in order to allow classes call methods/properties of other classes, public signature must be provided.
we will discuss static in another post.
No comments:
Post a Comment