class SingletonClass
{
private:
static bool SingletonIsstanceFlag;
static SingletonClass *single;
SingletonClass()
{
//private constructor
}
public:
static SingletonClass* getInstance();
void method();
~SingletonClass()
{
SingletonIsstanceFlag = false;
}
};
Wednesday, June 29, 2011
Singleton Pattern & its implementation with C++
#include
#include "Singleton.h"
using namespace std;
void useSingleton(){
Singleton* singletoneg= Singleton::getInstance();
singleton->doSummut();
// this shouldn't be allowed
Singleton* doubleton;
doubleton->doSummutElse();
}
int main(int argc, char** argv) {
useSingleton();
return (EXIT_SUCCESS);
}
#include "Singleton.h"
using namespace std;
void useSingleton(){
Singleton* singletoneg= Singleton::getInstance();
singleton->doSummut();
// this shouldn't be allowed
Singleton* doubleton;
doubleton->doSummutElse();
}
int main(int argc, char** argv) {
useSingleton();
return (EXIT_SUCCESS);
}
Tuesday, June 28, 2011
Delegates (C# Programming)
Delegates have the following properties:
- Delegates are like C++ function pointers but are type safe.
- Delegates allow methods to be passed as parameters.
- Delegates can be used to define callback methods.
- Delegates can be chained together; for example, multiple methods can be called on a single event.
- Methods do not have to match the delegate signature exactly. For more information, see Using Variance in Delegates (C# and Visual Basic).
- C# version 2.0 introduced the concept of Anonymous Methods, which allow code blocks to be passed as parameters in place of a separately defined method. C# 3.0 introduced lambda expressions as a more concise way of writing inline code blocks. Both anonymous methods and lambda expressions (in certain contexts) are compiled to delegate types. Together, these features are now known as anonymous functions. For more information about lambda expressions, see Anonymous Functions (C# Programming Guide).
public delegate int PerformCalculation(int x, int y);
A delegate is a type that defines a method signature. When you instantiate a delegate, you can associate its instance with any method with a compatible signature. You can invoke (or call) the method through the delegate instance.
Delegates are used to pass methods as arguments to other methods. Event handlers are nothing more than methods that are invoked through delegates. You create a custom method, and a class such as a windows control can call your method when a certain event occurs delegate void Delegate1(); delegate void Delegate2(); static void method(Delegate1 d, Delegate2 e, System.Delegate f) { // Compile-time error. //Console.WriteLine(d == e); // OK at compile-time. False if the run-time type of f // is not the same as that of d. System.Console.WriteLine(d == f); }
Interfaces (C# Programming)
Interfaces consist of methods, properties, events, indexers, or any combination of those four member types. An interface cannot contain constants, fields, operators, instance constructors, destructors, or types. It cannot contain static members. Interfaces members are automatically public, and they cannot include any access modifiers.
When a class or struct implements an interface, the class or struct provides an implementation for all of the members defined by the interface. The interface itself provides no functionality that a class or struct can inherit in the way that base class functionality can be inherited. However, if a base class implements an interface, the derived class inherits that implementation. The derived class is said to implement the interface implicitly.
Classes and structs implement interfaces in a manner similar to how classes inherit a base class or struct, with two exceptions:
When a class or struct implements an interface, the class or struct provides an implementation for all of the members defined by the interface. The interface itself provides no functionality that a class or struct can inherit in the way that base class functionality can be inherited. However, if a base class implements an interface, the derived class inherits that implementation. The derived class is said to implement the interface implicitly.
Classes and structs implement interfaces in a manner similar to how classes inherit a base class or struct, with two exceptions:
- A class or struct can implement more than one interface.
- When a class or struct implements an interface, it receives only the method names and signatures, because the interface itself contains no implementations
public class Car : IEquatable<Car> { public string Make {get; set;} public string Model { get; set; } public string Year { get; set; } // Implementation of IEquatable<T> interface public bool Equals(Car car) { if (this.Make == car.Make && this.Model == car.Model && this.Year == car.Year) { return true; } else return false; } }
An interface has the following properties:
- An interface is like an abstract base class: any non-abstract type that implements the interface must implement all its members.
- An interface cannot be instantiated directly.
- Interfaces can contain events, indexers, methods, and properties.
- Interfaces contain no implementation of methods.
- Classes and structs can implement more than one interface.
- An interface itself can inherit from multiple interfaces.
Namespaces (C# Programming)
Namespaces have the following properties:
- They organize large code projects.
- They are delimited by using the . operator.
- The using directive obviates the requirement to specify the name of the namespace for every class.
- The global namespace is the "root" namespace: global::System will always refer to the .NET Framework namespace System.
namespace SampleNamespace { class SampleClass { public void SampleMethod() { System.Console.WriteLine( "SampleMethod inside SampleNamespace"); } } }
Using Structs (C# Programming)
public struct CoOrds { public int x, y; public CoOrds(int p1, int p2) { x = p1; y = p2; } }
Classes (C# Programming)
public class Person { // Field public string name; // Constructor public Person() { name = "unknown"; } // Method public void SetName(string newName) { name = newName; } } class TestPerson { static void Main() { Person person = new Person(); Console.WriteLine(person.name); person.SetName("John Smith"); Console.WriteLine(person.name); // Keep the console window open in debug mode. Console.WriteLine("Press any key to exit."); Console.ReadKey(); } }
C# Preprocessor Directives
This section discusses the C# language's preprocessor directives:
#if
#else
#elif
#endif
#define
#undef
#warning
#error
#line
#region
#endregion
#pragma
#pragma warning
#pragma checksum
While the compiler does not have a separate preprocessor, the directives described in this section are processed as if there was one; these directives are used to aid in conditional compilation. Unlike C and C++ directives, you cannot use these directives to create macros.
A preprocessor directive must be the only instruction on a line.
#if
#else
#elif
#endif
#define
#undef
#warning
#error
#line
#region
#endregion
#pragma
#pragma warning
#pragma checksum
While the compiler does not have a separate preprocessor, the directives described in this section are processed as if there was one; these directives are used to aid in conditional compilation. Unlike C and C++ directives, you cannot use these directives to create macros.
A preprocessor directive must be the only instruction on a line.
List Current Keyboard Shortcuts
To list current keyboard shortcut mappings
- On the Tools menu, point to Macros, and then click Macros IDE.
- In Project Explorer, double-click MyMacros.
- Right-click Module1 and then click Rename.
- Type KeyboardShortcuts as the new name for the module.
- Double-click KeyboardShortcuts to open the file in the editor.
- Paste the following code in the file after Public Module KeyboardShortcuts:
Sub GetAllCommands() Dim cmd As Command Dim ow As OutputWindow = DTE.Windows.Item(Constants.vsWindowKindOutput).Object Dim owp As OutputWindowPane Dim exists As Boolean Dim i As Integer Dim sArray() As String sArray = New String() {} i = 1 exists = False For Each owp In ow.OutputWindowPanes If owp.Name = "Macro Output" Then exists = True Exit For End If i = i + 1 Next If exists Then owp = ow.OutputWindowPanes.Item(i) Else owp = ow.OutputWindowPanes.Add("Macro Output") End If owp.Clear() ' Output 1 line per command For Each cmd In DTE.Commands Dim binding As Object Dim shortcuts As String shortcuts = "" For Each binding In cmd.Bindings Dim b As String b = binding If Not shortcuts = "" Then shortcuts += "--OR-- " End If shortcuts = shortcuts + b + " " Next shortcuts = shortcuts.Trim() If Not cmd.Name.Trim().Equals("") And Not shortcuts.Equals("") Then sArray.Resize(sArray, sArray.Length + 1) sArray(sArray.Length - 1) = cmd.Name + vbTab + shortcuts End If Next Array.Sort(sArray) owp.OutputString(String.Join(vbCrLf, sArray)) End Sub - On the File menu, click Save MyMacros.
- Switch back to Visual Studio.
- On the Tools menu, point to Macros and then click Macro Explorer.
- Expand MyMacros and then expand KeyboardShortcuts.
- Right-click GetAllCommands and then click Run.
The macro generates a list of all possible commands in the IDE and any keyboard shortcut mappings these commands have in the current keyboard mapping scheme. - On the View menu, click Output.
Commands and their shortcut key combinations appear in the Output window. You can copy this information and paste it into another application, such as Microsoft Office Excel, for additional formatting and printing options.
The event handler will simply copy a newly added list item’s title in to another list.
public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdded(properties);
SPWeb web = properties.OpenWeb();
SPList list = web.Lists["contacts"];
string alladdress=properties.ListItem["To"] + ";" + properties.ListItem["CC"];
string[] addresses = alladdress.Split(';');
foreach (string emails in addresses)
{
SPListItem item = list.Items.Add();
string[] nameid = emails.Split('@');
item["Email"] = emails;
item["FirstName"] = nameid[0];
item["Title"] = " ";
item.Update();
}
list.Update();
web.Close();
}
Subscribe to:
Posts (Atom)
Featured Posts
Learn how to install Apache, MySQL and PHP on Ubuntu. Complete LAMP stack installation guide with one-shot commands, PHP testing and php-opcache troubleshooting
How to Install LAMP Stack on Ubuntu | Complete Guide UBUNTU • LINUX • SERVER • PHP ...
-
public struct CoOrds { public int x, y; public CoOrds( int p1, int p2) { x = p1; y = p2; } }
-
LM Studio Overview LM Studio is a desktop application designed for developing and experimenting with large language models (LLMs)...