
Imagine that you have created a complicated part of a 3D game, by example trying to capture the collision of the avatar against a wall. You have created many lines of code to do it.
After some time, at the same program, you need to create a similar logic to capture the collision of the avatar against another object.
When we have many similar actions inside a program it's a good idea the creation of a method, a function that can be reused.
If this method can be useful for other applications, better to write a Class having it inside and to create an assembly-on-library. We will learn how to do this now.
Our exercice is very simple (and useless). It's a method that: when we define a value for the variable name, returns the string: Hi plus the name. A cumpliment...
First of all you need to know that a variable can be (using C#) a field or a property. If possible, it's our sugestion to create a property because you can create consistency tests for the value assigned to it.
When the value of a property changes inside a method, this modification will be valid outside the method. We call this: pass by reference.
When the value of a field changes inside a method, this modification will be NOT valid outside the method. We call this: pass by value.
To use a property you need to create an object of its Class and to use it like a prefix.
To do our exercice, create a folder having the name:Ba and a file having the name: Hi.cs:
namespace Cumpliments {
using System;
using System.Text;
public class Hi {
private String Name;
public String name{
get {return Name;}
set {Name = value;}
}
public Hi(){
Name = null;
}
public String SayHi() {
if(Name != null){
return "Hi "+ Name;
}
else{
return "Hi ";
}
}
}
}
Our Class is a subclass of the root Class: Object but we don't need to specify this.
We are here creating a field: Name and a property: name.
We need also to create a Ba.csproj file:
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<AssemblyName>Ba</AssemblyName>
<DefaultClrNameSpace>Cumpliments</DefaultClrNameSpace>
<TargetType>library</TargetType>
<OutputPath>C:\Ba</OutputPath>
</PropertyGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildBinPath)\Microsoft.WinFX.targets" />
<ItemGroup>
<Compile Include="Hi.cs">
<SubType>Code</SubType>
</Compile>
</ItemGroup>
<ItemGroup>
<Reference Include="System"/>
<Reference Include="System.Xml"/>
<Reference Include="System.Data"/>
<Reference Include="WindowsBase"/>
<Reference Include="PresentationCore"/>
<Reference Include="PresentationFramework"/>
<Reference Include="UIAutomationProvider"/>
<Reference Include="UIAutomationTypes"/>
</ItemGroup>
</Project>
Only this for our "project". We will not have here a .XAML file or a Window... etc. etc. We are not creating an application. We are creating an asssembly-on-library that can be used in many applications (we hope). But we will "compile" it like we did for our "Hello World" application. Open the Visual C# Express etc. etc. like you did in previous lesson.
After the compilation we will have, at the Ba folder, a Ba.dll file that will be used.
The new exercice of this lesson is just to create an application to use it. Create a Ba2 folder and inside it the first file: Window1.xaml:
<Window
xmlns="http://schemas.microsoft.com/winfx/avalon/2005"
xmlns:x="http://schemas.microsoft.com/winfx/xaml/2005"
x:Class="DMU.Window1"
Title="DMU Tutorials Serie"
Top="100"
Left= "100"
Width="400"
Height="400"
>
<Canvas Width="400" Height="400" Background="DeepSkyBlue" >
<TextBlock Canvas.Top="50" Canvas.Left="100" FontSize="14">Type you name and press 'Click' </TextBlock>
<TextBox Name = "Entry1" Canvas.Top="100" Canvas.Left="100" FontSize="14"/>
<Button Canvas.Top="200" Canvas.Left="100" Click = "UseName">Click</Button>
<TextBlock Name="Text1" Canvas.Top="300" Canvas.Left="100" FontSize="14" ></TextBlock>
</Canvas>
</Window>
And the Window1.xaml.cs file:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Shapes;
using Cumpliments;
namespace DMU{
public partial class Window1 : Window{
public Window1(){
InitializeComponent();
}
private void UseName(object sender, RoutedEventArgs e){
String theName =Entry1.Text;
Hi cump = new Hi();
cump.name = theName;
Text1.Text = comp.SayHi() ;
}
}
}
VERY IMPORTANT: Put a copy of the Ba.dll file inside this folder: Ba2.
We can see that we are creating a subclass Window1 having some objects: two TextBlocks, one TextBox and one Button, all inside a Canvas.
Pay attention that we are assigning the value "UseName" to the property "Click" of the object Button. Doing this we are creating/overriding an event that will be defined inside the .XAML.CS file.
We are creating a string theName having what the user types inside the TextBox (whose name is Entry1).
After this, we are creating an object of the Class Hi. Pay attention that we are referencing the namespace Cumpliments. And will refer the Ba.dll at the .CSPROJ file.
Using the object cump we assign theName to the property name.
And the return of the method SayHi goes to the TextBlock whose name is Text1.
Now, look the Ba2.csproj file:
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup>Ba2</AssemblyName> <DefaultClrNameSpace>DMU</DefaultClrNameSpace> <TargetType>winexe</TargetType> <OutputPath>C:\Ba2</OutputPath> </PropertyGroup> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets"/> <Import Project="$(MSBuildBinPath)\Microsoft.WinFX.targets"/> <ItemGroup> <ApplicationDefinition Include="MyApp.xaml"/> <Page Include="Window1.xaml"/> <Compile Include="Window1.xaml.cs"> <DependentUpon>Window1.xaml</DependentUpon> <SubType>Code</SubType> </Compile> <Compile Include="MyApp.xaml.cs"> <DependentUpon>MyApp.xaml</DependentUpon> <SubType>Code</SubType> </Compile> <Reference Include="System"/> <Reference Include="System.Xml"/> <Reference Include="System.Data"/> <Reference Include="WindowsBase"/> <Reference Include="PresentationCore"/> <Reference Include="PresentationFramework"/> <Reference Include="UIAutomationProvider"/> <Reference Include="UIAutomationTypes"/> <Reference Include="Ba, Culture=neutral, processorArchitecture=MSIL"> <HintPath>.\Ba.dll</HintPath> <Name>Ba</Name> <SpecificVersion>False</SpecificVersion> <Aliases>Global</Aliases> </Reference> </ItemGroup> </Project>
What is new here is how we include the Ba.dll in the project - bolded.
TIP: The .DLL file of assemblies-on-libraries will be downloaded to the machine of the user running an application. So: don't create assemblies very generic and very big, having many methods.
Our application needs to have a MyApp.xaml and a MyApp.xaml.cs file that will be the same of the previous "Hello World" application. We will not repeat them here.
Compiling and publishing the application - like we did for the "Hello World" application - we can put a button at a HTML page to run it. Try it clicking here. remember to liberate the firewall.
![]() |
PREVIOUS LESSON NEXT LESSON T.CONTENTS HOMEPAGE
