private T GetEnumFromString<T>(string value)
{
try
{
T t;
t = (T)Enum.Parse(typeof(T), value);
return t;
}
catch (Exception E)
{
return (T)((object)0);
}
}
Generic Function to convert string to Enum
The Heartbleed Bug (CVE-2014-0160)
| Neel Metha from Google Security discovered incorrect memory handling in Open TLS Heartbeat extension. By which attacker can access upto 64K of memory of client or server and can expose Private key and other secret data. |
For complete details, please visit references
References
Heartbleed
Ubuntu / Security Notice USN-2165-1
OpenSSL Security Advisory (published 7th of April 2014, ~17:30 UTC)
Categories:
Heartbleed
Dock panel like animated buttons in WPF(XAML)
Like dockpanel buttons(Buttons which grows up as mouse enters on button) in windows, we can have the buttons in WPF without writing any C# or VB code. This can be achieved by using EventTriggers & DoubleAnimations.
As mentioned in above image as mouse enters on Recycle Bin icon, it grows up, similar thing can be achieved in wpf, sample XAML is written below.
Code:
Categories:
EventTriggers,
WPF,
XAML
Deferred Execution in LINQ
Deferred Execution of Query
When we write any LINQ query as mentioned, it doesn't get executed immediately. It actually stores query in the variable and every time we use/enumerate the variable it executes the query and use the result. It is called deferred execution.
So that whenever filter variable(a) changes prior to the use of object(lStr), output will automatically changes.
class Sample
{
public int i;
public String str;
}
static void Main(string[] args)
{
List lst = new List();
lst.Add(new Sample() { i = 1, str = "String1" });
lst.Add(new Sample() { i = 2, str = "String2" });
lst.Add(new Sample() { i = 3, str = "String3" });
lst.Add(new Sample() { i = 4, str = "String4" });
int a = 1;
IEnumerable lStr = (from l in lst
where l.i == a
select l); //It will just store query in lStr variable.
}
When we write any LINQ query as mentioned, it doesn't get executed immediately. It actually stores query in the variable and every time we use/enumerate the variable it executes the query and use the result. It is called deferred execution.
Sample s = lStr.First(); ///Query will be executed here Console.WriteLine(s.str);
So that whenever filter variable(a) changes prior to the use of object(lStr), output will automatically changes.
Factory Design pattern
Factory design pattern provides you an interface creating a single object, but let subclass decide which class to instantiate. This pattern is used most frequently when we are having common functionality with different methods for different objects.
In that scenario, we will create an interface with defining common functionality which will be implemented by the classes. A class will be defined which will be used to identify which class to be instantiated for the object.
For example, We have to create reading utility and we are having 3 different kind of files 1st is pdf, 2nd is doc and 3rd is txt file. On all the files we have to perform open operation but way to open the file will be different. In this case we will create an Interface will contain OpenFile Operation. There will be classes for each file type and the classes will be implementing the factory interface(Example is given in C# Code Example.
C# Code Example:
In that scenario, we will create an interface with defining common functionality which will be implemented by the classes. A class will be defined which will be used to identify which class to be instantiated for the object.
For example, We have to create reading utility and we are having 3 different kind of files 1st is pdf, 2nd is doc and 3rd is txt file. On all the files we have to perform open operation but way to open the file will be different. In this case we will create an Interface will contain OpenFile Operation. There will be classes for each file type and the classes will be implementing the factory interface(Example is given in C# Code Example.
C# Code Example:
Categories:
C#,
Design Patterns,
dot net,
OOP
TargetInvocationException was unhandled "Exception has been thrown by the target of an invocation." issue in WPF
In WPF, most probably the issue occurs when we are accessing null object in the constructor of the code file of the XAML or If you are invoking "PropertyChanged" event without having null check inside the constructor.
Example 1:
Example 1:
"if" condition inside Select Clause of LINQ Query.
In LINQ you can't use "if" statement directly in select clause, but you can achieve this by using ternary operator.
Consider you are querying an Array of object & the object internally contains another object. We have to select some field from inner object. But it may be chance that for some cases inner object is null. So to prevent your query from throwing "Object Reference not set to instance of object exception.", you need to check for null object in select clause.
Code:
LINQ :
Consider you are querying an Array of object & the object internally contains another object. We have to select some field from inner object. But it may be chance that for some cases inner object is null. So to prevent your query from throwing "Object Reference not set to instance of object exception.", you need to check for null object in select clause.
Code:
class Periods
{
public int StartYear;
public int EndYear;
}
class student
{
public int RollNo;
public string name;
public DateTime DOB;
public Periods coursePeriod;
}
//There is an array of student object which internally contains Periods object.
student[] arrStudents= new student[100];
// You have to fetch list of students and time period spend in college
//code to fill arrStudents Array.
//output object
class studentCousePeriod
{
public int Rollno;
public int StartYear;
public int EndYear;
}
LINQ :
C# Null-Coalescing Operator (??)
C# has an operator called Null-Coalescing Operator(??), this operator takes two operands. If left hand side operand is not null then it returns left hand side operand else it returns right hand side operands. It is very effective to assign default value to a variable.
This variable can be used with reference type as well as nullable value types. A sample code for the use of this operator is written below:
This variable can be used with reference type as well as nullable value types. A sample code for the use of this operator is written below:
Categories:
C#,
dot net,
Null-Coalescing Operator
ERROR: Couldn't Find BOOTMGR while booting BackTrack via Bootable pendrive
I was trying to boot my machine with Backtrack via bootable pendrive, but unfortunately I got an error by saying
Couldn't Find BOOTMGR
boot:
No need to worry, It has just not found the boot loader. so we will have load that by the command. Just write the following command and it will be start loading
boot: /casper/vmlinuz boot=casper initrd=/casper/initrd.gz text splash vga=791
now your machine will be start booting with backtrack via your pen drive.
ENJOY!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
How to declare Hashtable or ArrayList in XAML.
To declare Hashtable or ArrayList in xaml, we need to include the System.Collection namespaces in XAML by using the below key
xmlns:col='clr-namespace:System.Collections;assembly=mscorlib'
After declaring the namespace we can use all the class of System.Collection namespace with col: prefix
Code to Declare the HashTable in XAML:
Code to Declare the ArrayList in XAML:
xmlns:col='clr-namespace:System.Collections;assembly=mscorlib'
After declaring the namespace we can use all the class of System.Collection namespace with col: prefix
Code to Declare the HashTable in XAML:
<col:Hashtable x:Key='hash'>
<col:DictionaryEntry x:Key='hashEntry1'
Key='1' Value='a'></col:DictionaryEntry>
<col:DictionaryEntry x:Key='hashEntry2'
Key='2' Value='b'></col:DictionaryEntry>
<col:DictionaryEntry x:Key='hashEntry3'
Key='3' Value='c'></col:DictionaryEntry>
<col:DictionaryEntry x:Key='hashEntry4'
Key='4' Value='d'></col:DictionaryEntry>
</col:Hashtable>Code to Declare the ArrayList in XAML:
Categories:
ArrayList,
LINQ to Hashtable,
WPF,
XAML
Change Style of ListBoxItem when we use ItemTemplate in ListBox.
We will see how can we change background and foreground of any ListBoxItem on MouseOver or on Selection, and for ListBoxItem, ItemTemplate is being used.
Consider a ListBox in XAML, in which, ListBoxItem contains multiple Label via ItemTemplate. Now you want the background color and the text color of the Labels to be changed on Mouse Over or on Selection of the Item. To achieve this we can use following style:
Scenario to Achieve:
To change the Foreground of the text we will use following style on the Labels:
To change the Background of the Item we will write following style for the ListBox:
Consider a ListBox in XAML, in which, ListBoxItem contains multiple Label via ItemTemplate. Now you want the background color and the text color of the Labels to be changed on Mouse Over or on Selection of the Item. To achieve this we can use following style:
Scenario to Achieve:
To change the Foreground of the text we will use following style on the Labels:
<Style TargetType="{x:Type Label}" x:Key="listBoxLabel"> <Setter Property="Foreground" Value="Black"></Setter> <Style.Triggers> <!--Triggers for the IsSelected Property to check selection status--> <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem}, Path=IsSelected}" Value="True"> <Setter Property="Foreground" Value="White"></Setter> </DataTrigger> <!--Triggers for the IsMouseOver Property to check focus status--> <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem}, Path=IsMouseOver}" Value="True"> <Setter Property="Foreground" Value="Red"></Setter> </DataTrigger> </Style.Triggers> </Style>
To change the Background of the Item we will write following style for the ListBox:
Categories:
DataTriggers,
ListBox,
Triggers,
WPF,
XAML
What is Process?
Process: Any
program running in computer is a process. So there can be multiple processes
running in same computer or there can be single process running. Each and every
process will have own flow of control. In sequential execution environment, all
the process are executed in sequentially. In multiprocessor environment all the
process are switched forth and back.
Each running process has own memory range where it has
executable program, data, & stack etc… , is called Address Space/Core Image. Each process is also associated with set
of program registers, counter; stack pointer and Hardware registers etc.
When a process (A) is suspended temporarily by processor and
later when it is resumed, it must be started from the exactly the place from
where it was suspended. To do so, whole state of the process is being stored in
a table called Process Table.
A process can create another process called child processes, these child processes
can further create another child processes, through this we can have process
hierarchy.
Process
Creation:
There are 4 principal events by which a process can be
created
- System Initialization
- A process creates another process
- A user request to create another process
- Initializing batch job.
Process
termination:
There are the following main reason due to which a process
can be terminated
- Normal Exit
- Error Exit
- Fatal Error Exit
- Killed by another Process.
Categories:
Process
ComboBox in WPF DataGrid's Column Header
Here, In this article, we are going to discuss, how can we add framework elements in WPF Datagrid's column header. We will add combo box in the header. To add Combobox in the Header of DataGrid we use [GridColumn].Header tag. In the tag we can add any element directly.
<tools:DataGridTextColumn.Header>
<ComboBox x:Name='txtCbo75P' ItemsSource='{StaticResource StringArrayResource}' SelectedIndex='0'></ComboBox>
</tools:DataGridTextColumn.Header>
Now suppose we have to create Grid as below:
In this case for the forth column rather than giving header text directly, we shall use above code
XAML
Create Nested headers in WPF DataGrid
In WPF many times we may require to render a data grid which contains multilevel headers, for example we have columns FirstName, LastName, Address1, Address2, City, State. Now we have to render a grid in which we have these columns these columns will have group name above the column header. It means there will be another level of header above these. In our case FirstName and LastName will be under Name, and Address1, Address2, City and State Columns will be under the Address.
Sample snapshot is displayed below.
XAML:
Sample snapshot is displayed below.
XAML:
WPF StringFormat example in Binding
In this small article we will see how we can use StringFormat when we use Binding.
In the example we have use one Date Picker and one
TextBlock. The TextBlock isbound with the SelectedDate property of the
DatePicker.And we have used StringFormat property to modify the text shown in
TextBlock.
Sample Code:
Categories:
C#,
dot net,
StringFormat,
WPF,
XAML
Difference between BOSS and LEADER
Have you
ever wondered, what is the difference between a BOSS and a LEADER?
Some may
feel that they both have same role to play whereas others may feel that they
are different.
But we must know that Leadership
cannot be demanded – only earned and deserved.
In this
article we have listed some of the differences which we feel exists between
them and their roles. It is a must read for people, who are in any leadership
position in any organization, and then decide whether they are a BOSS or a
LEADER.
Categories:
Entertainment,
QUOTES
Custom / Partial Projection in LINQ to Hashtable / Dictionary
Like SQL we can project partial or custom data in output. In the output of LINQ Query it will be an enumerable collection of anonymous type. This anonymous type is being created by using new operator in the select clause and member fields are generated dynamically in the select new part of the LINQ.
Consider a data dictionary and structure as followed:
public static Dictionary<int,
marks> stuResult;
public struct marks
{
public int m_hindi;
public int m_english;
public int m_maths;
}
Consider marks is structure datatype which contains marks details of a student. stuResult Dictionary contains RollNo(int), as key and object of marks as its value. Now we have to find out the Total marks obtained by each students. To find this LINQ will be as:
Categories:
LINQ,
LINQ to Hashtable,
LINQ to Object
Where Clause with LINQ to Hashtable / Dictionaries
In this article, we will discuss How can we use where clause in LINQ to Object(Hastable / Dictionaries). Many time while working with the Dictionaries or Hashtables, we may require to find out the values from them which satisfies some specific criteria. In that case we used to write iterations and then find the values which satisfies the criteria. Maybe writing the loop is a easy task for the beginners, but writing code for checking the values and performing the operations are so boring and tedious task.For the checking the values for specific criteria we need to declare intermediate variables etc. But by using LINQ, this can be done so easily with few lines of code even without using iterations.
Categories:
dot net,
LINQ,
LINQ to Hashtable,
LINQ to Object
LINQ: Join operations on Hashtables / Dictionaries
In this example we will discuss, how to perform Join operation on dictionaries / hash tables.
Consider 2 Dictionaries, one dictionary contains Roll No (Int) as key and a Structure (marks) (which contains marks of student) as value. Another dictionary contains Roll No(Int) as key and Name of Student (String) as value. Now we have to find name of students and corresponding marks of the students.
So in this case we will perform Join on the key of both dictionaries and will have enumerable collection of KeyValue pairs (key will be name of student and value will have marks details) in the query result.
var QueryResult = from mrks in stuResult.AsEnumerable<KeyValuePair<int, marks>>()
join pd in
stuPDetails.AsEnumerable<KeyValuePair<int, string>>()
on mrks.Key equals
pd.Key
select
new KeyValuePair<String, marks>(pd.Value,
mrks.Value);
Above mentioned LINQ query will perform join operation on the key values of the dictionaries. Finally selects the new KeyValue pairs having String(Name of Student) as key, marks(Marks details of the student) as value.
Example of JOIN on Dictionaries
Categories:
dot net,
LINQ,
LINQ to Hashtable,
LINQ to Object
Subscribe to:
Posts (Atom)




