IProgrammer<T>
C#, SQL, JavaScript and more
Monday, February 13, 2017
Ending the Great Debate on Enum Flags
This article explains how enumeration and flags work in C#.NET and how to properly use them.
Wednesday, June 03, 2015
How do i create an InstallShield LE project to install a windows service?
Monday, May 11, 2015
Thursday, November 06, 2014
Visual Studio 2012 Crashes at Startup
The solution for famous problem exists here. But my Registry not included all required keys from the article. In the 3rd page of Google search was found very small but very effective advice:
Thank you, Derek Smith!
Full post exists here.You would get above error if ieproxy.dll is not registered properly. Re register Ieproxy.dll.
C:\Program Files\Internet Explorer>regsvr32 /u ieproxy.dll
C:\Program Files\Internet Explorer>regsvr32 ieproxy.dll
C:\Program Files (x86)\Internet Explorer>regsvr32 /u ieproxy.dll
C:\Program Files (x86)\Internet Explorer>regsvr32 ieproxy.dll
Thank you, Derek Smith!
Thursday, October 02, 2014
Web.config and config file for every project in solution
Suppose a given project solution: core, data access layer and several "plugin" modules. In order to prevent changes in the core all configurations was moved to web.config. Also we would like that every module will use a dedicated config file because a "plugin" can be added and/or removed.
First solution:
In web.config file in the section "appSettings" available attribute "file" for moving segment of web config to another file. It's nice but we need more then one config file and therefore required additional configurations.Create "sectionGroup":
Module1 and Module2 this is a names of our plugins. Create configuration section for group and define key for path of module config file in every named section:
Create 2 new config files "App1.config" and "App2.config". In the properties of files change "Copy to Output Directory" to "Copy always". The content of module config file look like this one:
An using code:
Second solution:
Add new section "Plugin". We are use the type DictionarySectionHandler in order to get values as Dictionary:
Add values:
Create 2 new config files "App1.config" and "App2.config". In the properties of files change "Copy to Output Directory" to "Copy always". The content of module config file look like this one:
An using code:
Enjoy!
First solution:
In web.config file in the section "appSettings" available attribute "file" for moving segment of web config to another file. It's nice but we need more then one config file and therefore required additional configurations.Create "sectionGroup":
- <configuration>
- <configSections>
- <sectionGroup name="Plugins">
- <section name="Module1" type="System.Configuration.NameValueFileSectionHandler, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
- <section name="Module2" type="System.Configuration.NameValueFileSectionHandler, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
- </sectionGroup>
- </configSections>
- .....
Module1 and Module2 this is a names of our plugins. Create configuration section for group and define key for path of module config file in every named section:
- <Plugins>
- <Module1>
- <add key="ConfigFile" value="App1.config"/>
- </Module1>
- <Module2>
- <add key="ConfigFile" value="App2.config"/>
- </Module2>
- </Plugins>
Create 2 new config files "App1.config" and "App2.config". In the properties of files change "Copy to Output Directory" to "Copy always". The content of module config file look like this one:
- <?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <appSettings>
- <add key="AnyKey" value="Bla-Bla-Bla"/>
- <add key="AnyKey2" value="Bla-Bla-Bla"/>
- </appSettings>
- </configuration>
An using code:
- class Test
- {
- private static readonly Configuration config = ConfigurationManager.OpenMappedExeConfiguration(
- //Get config file
- new ExeConfigurationFileMap()
- { //Web.config part //File name
- ExeConfigFilename = ((NameValueCollection)ConfigurationManager.GetSection("Plugins/Module1"))["ConfigFile"]
- },
- ConfigurationUserLevel.None);
- private void Foo()
- {
- var value = config.AppSettings.Settings["AnyKey"].Value;
- Console.WriteLine(value);
- }
- }
Second solution:
Add new section "Plugin". We are use the type DictionarySectionHandler in order to get values as Dictionary:
- <configuration>
- <configSections>
- <section name="Plugin" type="System.Configuration.DictionarySectionHandler, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
- </configSections>
Add values:
- <Plugin>
- <add key="ConfigFileOfModule1" value="App1.config"/>
- <add key="ConfigFileOfModule2" value="App2.config"/>
- </Plugin>
Create 2 new config files "App1.config" and "App2.config". In the properties of files change "Copy to Output Directory" to "Copy always". The content of module config file look like this one:
- <?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <appSettings>
- <add key="AnyKey" value="Bla-Bla-Bla"/>
- <add key="AnyKey2" value="Bla-Bla-Bla"/>
- </appSettings>
- </configuration>
An using code:
- private static readonly IDictionary section = (IDictionary)ConfigurationManager.GetSection("Plugin");
- private static readonly System.Configuration.Configuration config =
- ConfigurationManager.OpenMappedExeConfiguration(
- new ExeConfigurationFileMap()
- {
- ExeConfigFilename = section["ConfigFileOfModule1"].ToString()
- }, ConfigurationUserLevel.None);
- private void Foo()
- {
- var value = config.AppSettings.Settings["AnyKey"].Value;
- }
Enjoy!
Tuesday, September 16, 2014
How to include value types AND strings in generic constraint.
I'm very like to use generics in the code. Usually generic implementation is working smoothly but exists one small problem with generic value types: cannot use string typed value because System.String is a class.
The simple solution is creating overwrite method:
Nice, but not perfect. The beautiful idea was offered by KeithS instead to restrict a value to struct change it to IConvertible. Next types implemented an interface IConvertible but now String can be included in the constraint:
Boolean
Byte
Char
DateTime
Decimal
Double
Int16
Int32
Int64
SByte
Single
String
Type
UInt16
UInt32
UInt64
Final:
- Foo<string>("abc");
- private void Foo
(T value) where T : struct - {
- //...
- }
The simple solution is creating overwrite method:
- private void Foo
(T value) where T : string - {
- //...
- }
Nice, but not perfect. The beautiful idea was offered by KeithS instead to restrict a value to struct change it to IConvertible. Next types implemented an interface IConvertible but now String can be included in the constraint:
Boolean
Byte
Char
DateTime
Decimal
Double
Int16
Int32
Int64
SByte
Single
String
Type
UInt16
UInt32
UInt64
Final:
- private void Foo
(T value) where T : IConvertible - {
- //...
- }
Tuesday, August 26, 2014
C# generation: from '?' via '??' to '???'
C# include several usable but not famous operators. Today we'll consider the generation of operator '?'.
The first and known from language C old operator '?' was appeared in C# 1.0 in the Visual Studio 2002. An operator using for condition instead to block if-else:
The next '??' was offered 3 years later in Visual Studio 2005. The ?? operator is called the null-coalescing operator. It returns the left-hand operand if the operand is not null; otherwise it returns the right hand operand:
The last operator '???' was announced in Visual Studio 2012. I'm very like it because he is cutting the validation of object:
In Visual Studio 2014 the operator '?..' did not receive renewal but maybe in next versions we'll see '????' and even '?????' :-).
The first and known from language C old operator '?' was appeared in C# 1.0 in the Visual Studio 2002. An operator using for condition instead to block if-else:
- Random rnd = new Random();
- //Long way
- bool isLessTo50;
- if (rnd.Next(100) < 50)
- {
- isLessTo50 = true;
- }
- else
- {
- isLessTo50 = false;
- }
- //Short way
- bool isLessTo50 = rnd.Next(100) < 50 ? true : false;
The next '??' was offered 3 years later in Visual Studio 2005. The ?? operator is called the null-coalescing operator. It returns the left-hand operand if the operand is not null; otherwise it returns the right hand operand:
- DateTime? dt = GetDate();
- //Regular way
- if (dt != null)
- {
- return dt;
- }
- else
- {
- return DateTime.Now;
- }
- //Short way
- return (dt != null) ? dt : DateTime.Now;
- //New way
- return dt ?? DateTime.Now;
The last operator '???' was announced in Visual Studio 2012. I'm very like it because he is cutting the validation of object:
- class Person
- {
- public string Name { get; set; }
- public int ID { get; set; }
- }
- //The program
- Person person = InitPerson();
- //Old way
- if (person != null)
- {
- return person.ID;
- }
- else
- {
- return default(int);
- }
- //New way
- return person.ID ??? default(int);
In Visual Studio 2014 the operator '?..' did not receive renewal but maybe in next versions we'll see '????' and even '?????' :-).
Subscribe to:
Posts (Atom)