- Details
- Written by: Stanko Milosev
- Category: C#
- Hits: 4510
Create new console project, and create base class like this:
public class BaseClass { public BaseClass(int value) { Console.WriteLine("BaseClass constructor with value: " + value.ToString()); } }
Now, let's derive class from our BaseClass:
class DerivedClass: BaseClass { public DerivedClass(int value) : base(value) { Console.WriteLine("DerivedClass constructor with value: " + value.ToString()); } }
If we create our Main method like this:
class Program { static void Main(string[] args) { BaseClass a = new BaseClass(10); DerivedClass b = new DerivedClass(20); Console.ReadKey(); } }
Result will be like this:
BaseClass constructor with value: 10 BaseClass constructor with value: 20 DerivedClass constructor with value: 20
Example project you can download from here.
- Details
- Written by: Stanko Milosev
- Category: C#
- Hits: 4659
Start new console and create one class like this:
public class IndexerClass { private string[] myData; public string this[string myTest] { get { return "Test get"; } } }
Now if Main method looks like this:
class Program { static void Main(string[] args) { IndexerClass myInd = new IndexerClass(); Console.WriteLine(myInd["A test"]); Console.ReadKey(); } }
Result would be something like this:
Test get
Example you can download from here
- Details
- Written by: Stanko Milosev
- Category: C#
- Hits: 4800
To use "using" you need to have implementation IDisposable interface, so here is how my ViewModel look like:
namespace UsingExample.ViewModel { using System; using System.Windows; public class UsingExampleViewModel { public string UsingExampleTest { get; set; } public UsingExampleViewModel() { using (UsingExampleClassTest aaa = new UsingExampleClassTest()) { aaa.myTest = "rrr"; } UsingExampleTest = "test"; } } public class UsingExampleClassTest: IDisposable { public string myTest { get; set; } public void Dispose() { MessageBox.Show("Die object!"); } } }
XAML:
<Window x:Class="UsingExample.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:viewModel="clr-namespace:UsingExample.ViewModel" Title="MainWindow" Height="350" Width="525"> <Grid> <Grid.Resources> <viewModel:UsingExampleViewModel x:Key="UsingExampleViewModel" /> </Grid.Resources> <TextBox DataContext="{StaticResource UsingExampleViewModel}" Text="{Binding UsingExampleTest}"/> </Grid> </Window>
Example project you can download from here.
- Details
- Written by: Stanko Milosev
- Category: C#
- Hits: 7605
Let's say we want to create xml like this:
<?xml version="1.0" encoding="utf-8"?> <project> <menu bg_img=""> <mnu_item txt="Menu item 1" anotherAttribute="another attribute 1"> <sub_item txt="Sub item 1" articleId="1" /> </mnu_item> <mnu_item txt="Menu item 2" anotherAttribute="another attribute 2"> <sub_item txt="Sub item 2" articleId="2" /> </mnu_item> </menu> </project>
Serialization
First we need model:
[XmlRoot("project")] public class Project { [XmlElement(ElementName = "menu")] public Menu Menu { get; set; } } public class Menu { [XmlAttribute("back_image")] public string backImage {get; set;} [XmlElement(ElementName = "mnu_item")] public List<MenuItem> MenuItem { get; set; } } public class MenuItem { [XmlAttribute("txt")] public string Txt { get; set; } [XmlAttribute("anotherAttribute")] public string AnotherAttribute { get; set; } [XmlElement("sub_item")] public SubItem SubItem { get; set; } } public class SubItem { [XmlAttribute("txt")] public string Txt { get; set; } [XmlAttribute("articleId")] public string ArticleId { get; set; } }
ViewModel looks like this:
Project serializedProject = new Project(); serializedProject.Menu = new Menu(); serializedProject.Menu.backImage = string.Empty; MenuItem menuItem = new MenuItem(); menuItem.Txt = "Menu item 1"; menuItem.AnotherAttribute = "another attribute 1"; menuItem.SubItem = new SubItem(); menuItem.SubItem.Txt = "Sub item 1"; menuItem.SubItem.ArticleId = "1"; serializedProject.Menu.MenuItem = new List<MenuItem>(); serializedProject.Menu.MenuItem.Add(menuItem); menuItem = new MenuItem(); menuItem.Txt = "Menu item 2"; menuItem.AnotherAttribute = "another attribute 2"; menuItem.SubItem = new SubItem(); menuItem.SubItem.Txt = "Sub item 2"; menuItem.SubItem.ArticleId = "2"; serializedProject.Menu.MenuItem.Add(menuItem); TextWriter txtWriter = new StreamWriter(Path.ChangeExtension(System.Reflection.Assembly.GetEntryAssembly().Location, ".xml")); XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); XmlSerializer xmlSerializer = new XmlSerializer(typeof(Project), ""); xmlSerializer.Serialize(txtWriter, serializedProject, ns); txtWriter.Close();
Notice lines:
XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); ... xmlSerializer.Serialize(txtWriter, serializedProject, ns);
Without these lines our XML would look like this:
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
Then in model, line, for example:
[XmlAttribute("back_image")]
Means that our attribute will be named as "back_image" in our XML.
Path.ChangeExtension(System.Reflection.Assembly.GetEntryAssembly().Location, ".xml")
With
System.Reflection.Assembly.GetEntryAssembly().Location
We are geting location of our exe file, and with method Path.ChangeExtension we are changing our file extension to XML.
Linq to XML
For linq to XML we don't need model, only XElement, so my code looks like this:
XElement myLinq2XML = new XElement( "project", new XElement( "menu", new XAttribute("back_image", string.Empty), new XElement( "mnu_item", new XAttribute("txt", "Linq2XML item 1"), new XAttribute("anotherAttribute", "another attribute 1"), new XElement("sub_item", new XAttribute("txt", "Sub item 1"), new XAttribute("articleId", "1") ) ), new XElement( "mnu_item", new XAttribute("txt", "Linq2XML item 2"), new XAttribute("anotherAttribute", "another attribute 2"), new XElement("sub_item", new XAttribute("txt", "Sub item 2"), new XAttribute("articleId", "2") ) ) ) );
Example project you can download from here.