Showing posts with label Beginners. Show all posts
Showing posts with label Beginners. Show all posts

Tuesday, April 28, 2015

C# - using "params" in method parameter


You can define a method with "params":

void Method(string s, params object[] param)

That means, you can use this code to add parameters to the method 

Customer customer;
Method("bla", customer.CustomerID, customer.CustomerSearchName);

instead of defining this:

void MethodKacke(string s, object[] param)

Then the method parameters had to  be like this:


MethodKacke("bla", new object[] { customer.CustomerID, customer.CustomerSearchName });

So using params is the more easy way. :-)

Monday, November 3, 2014

C# - Null-coalescing Operator



The "??" operator is called the null-coalescing operator and is used to define a default value for a nullable value types as well as reference types. It returns the left-hand operand if it is not null; otherwise it returns the right operand.


Example:

int? a = 123;
int? b = 5;
int? x = a ?? b;

/*Output:x = 123*/



int? a = null;
int? b = 5;
int? x = a ?? b;

/*Output:x = 5*/

Thursday, September 11, 2014

Transact SQL - Creating Temp Tables

This simple example shows you how to create and use temp tables in Microsoft SQL Server 2008.

First we have to create the temp table. The difference between regular tables and temp tables is to use the # before the table name. The syntax is the same:

create table #tempTable(keyword nvarchar(max), productNo nvarchar(30), anotherValue nvarchar(max))

After the temp table is created, we can put some data into it:

insert into #tempTable (keyword, productNo, anotherValue) 
 values ('test', 1234, 'somewhat')

After the data is in the temp table, we can use regular select statements:

select * from #tempTable

The result is:

keyword productNo anotherValue

test 1234 somewhat

Wednesday, April 30, 2014

SQL Server 2008 - Query XML data

Just recognized, that it is easy to query xml data stored in a SQL Server 2008 table (field):

declare @data xml

select @data = <datafield> from <table> (nolock) where <condition>

select @dataselect @data.query('<node>') as result

Here an example with sample xml:

declare @data xml
select @data = '<root><datas><data><add name="a" value="x1" /><add name="b" value="y2" /> <add name="c" value="z3" /></data><data><add name="a" value="e4" /> <add name="b" value="f5" /><add name="c" value="g6" /> </data></datas></root>' 
select @data
select @data.query('(/root/datas/data/add[@name="c"])[1]') as result 

The result is:

<add name="c" value="z3" /> 

Now I need to find out, how to get the value and not the complete node.

Friday, March 14, 2014

C# - Resizing Arrays - Add String to String Array

Guys,
here's a simple way to add a string to a string array (C#):

String[] Sorting;
foreach (String StringObj in StringObjCollection)
{
     Array.Resize(ref Sorting, Sorting.Length + 1);
     Sorting[Sorting.Length - 1] = StringObj;
}

C# - CollectionBase - Sorting a collection

Based on my blog post about creating a collection using the CollectionBase class yesterday, I figured out, how to sort a collection based on the class CollectionBase.

public List<MyClass> SortByColumnSortColumn()
{
     List<MyClass> SortedList = this.List.Cast<MyClass>().ToList();
     SortedList = SortedList.OrderBy(x => x.SortColumn).ToList();
     return SortedList;
}

Thursday, March 13, 2014

C# - Creating a Collection class


Creating a simple Collection class is very easy.
Assuming we have a class like this and want to create a collection class from it...

public class MyClass
{
     public string Name;

     public MyClass()
     {
     }
}

We can easily create a collection of it by using the CollectionBase class in the .NET Framework.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace My_Project.Classes
{
    public class MyClassCollection : System.Collections.CollectionBase
    {
        #region Variables
        #endregion

        #region Properties
        #endregion

        #region Constructors
        #endregion

        #region Methods

        public void Add(My_Project.Classes.MyClass NewMyClassObject)
        {
            List.Add(NewMyClassObject);
        }

        public void Remove(int index)
        {
            // Check to see if there is a MyClass at the supplied index.
            if (index > Count - 1 || index < 0)
            // If no MyClass exists, a messagebox is shown and the operation 
            // is cancelled.
            {
                throw new Exception();
            }
            else
            {
                List.RemoveAt(index);
            }
        }

        public My_Project.Classes.MyClass Item(int Index)
        {
            // The appropriate item is retrieved from the List object and
            // explicitly cast to the MyClass type, then returned to the 
            // caller.
            return (My_Project.Classes.MyClass)List[Index];
        }
        #endregion
    }
}

Now we can use the Collection class:

// create MyClass Object 1:
MyClass MyClassObject1 = new MyClass();

//create MyClass Object 2:
MyClass MyClassObject2 = new MyClass();

//creating the collection object:
MyClassCollection Col = new MyClassCollection();

//adding both objects to the collection
Col.Add(MyClassObject1);
Col.Add(MyClassObject2);

//removing an object from the collection:
Col.Remove(1);

//getting the item object from a collection
MyClass NewObject = Col.Item(0);


Here is a good walkthrough from Microsoft:

http://msdn.microsoft.com/en-us/library/xth2y6ft(v=vs.71).aspx