Archive for June, 2008

when to use static

Posted by rameshch on June 30, 2008

Static keyword can be applied to
Class, field, method, properties, operator, event and constructors.
Static member belongs to the class and not to any object of the class.
They can be used without creating the instance of the class.

For e.g. Static Void Main()
It is called by the operating system on program execution
To access the static member we’ll use

ClassName.staticmember

When a variable is declared as static internally what happens is that all the instances of the class share the same static variable. A static variable is initialized when its class is loaded. If not
initialized explicitly then
it is initialized to zero for numeric variable
null in case of object references
false for boolean

StaticMethod- they can only contain static member and call other static method.
If we need to access them than it can be done through the object of that class

class Game
{
void string getGameName()
{
……………….
}
public static void getNameThroughStatic(Game g)
{
g.getGameName(); // accessing static method
}
}

When to use them?
Well we can use them when we need to maintain information applicable to the entire class

suppose we have a class Employees there we can have a static count variable to keep track of no of employees.

class Employees
{
static in count=0;
public Employees()
{
count++;
}

~Employees
{
count–;
}

}

What are Static Constructor?
They can be used to initialize the static variables.
They are called automatically and before the instance constructor (if called any).

for above class
static Employees() // no other access modifiers for them
{}

What are static Classes?
A class whose objects can’t be created and which can only have static members. They can’t be inherited as well.
They can have static constructor

Why use static Classes?
It can be used to group related static method.

 

Static modifier used with the class, field, method, properties, operator, event and constructors. We can not use static modifier with indexers, destructors.

When we use static modifier with the members then they are no more part of instance of a class. Static members are a part of class itself.

 

For example:

public class MyClass

{

    public struct MyStruct

    {

        public static int x = 100;

    }

}

 

//To refer to the static member x, use the fully qualified name

MyClass.MyStruct.x

 

 

Consider a class that represents a student. Assume that the class contains a method to count student. Both the method does not belong to any instance student. Instead they belong to the College class. Therefore, they should be declared as static members of the college class.

 

Static Class

If static modifier is apply to the class then all member of class must be static.

Classes, including static classes, may have static constructors.

It is not possible to make an instance of static class with “new” keyword.

Features of static class:

1.      They can only contain static members.

2.      They are sealed means we cannot inherit the class.

3.      They can not  contain non static constructors

Static Members and Constructor:

Static members are initialized before the static member is accessed for the first time.

A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only. It is called automatically before the first instance is created or any static members are referenced.

A static constructor does not take access modifiers or have parameters.

A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.

 

Posted in C# | Leave a Comment »

Customizing Form Web Part

Posted by rameshch on June 4, 2008

Here is an outline of the steps:

1.    Open up your site in SharePoint Designer.

2.    In the Data Source Library section (on the right), expand SharePointListis and click on custom List (it’s not mandatory we can use share point List  or documents)

3.    Click on the down arrow next to the custom List and choose Show Data

As shown Below from list click on Show Data

4) From the  Inserted Selected Fields select Multiple Item View

4) Insert a Form Web Part (Insert -> SharePoint Controls -> Form Web Part)

5) From Design Phase click on Parameter  create new parameter (Param1)

Fig3

6)choose on WebPart Connection From the actions drop down, choose “Get Parameters From” and click Next.

 

 

7)  Choose Connect to Web Part on this Page and on the next screen choose the Form  Web Part  Match the T1 (or the variable you specified) to the  Param1 variable.

8)Choose Connect to Web Part on this Page and on the next screen choose the Form Web part
9) Match the T1 (or the variable you specified) to the Param1 variable.

10) From Fig3  choose Filters  add the Fields to match the requirement

10) Now we’re done!   There are so many other options explore them.

Posted in Uncategorized | Leave a Comment »