when programming .NET. Delegates are often described as the 'backbone of the
event model' in VB.NET. In this we are going to introduce you to delegates
(Sytem.Delegate class) and show you how powerful of an event mechanism
through delegates.
Delegates are implemented using the delegate class found in the System
namespace. A delegate is nothing more than a class that derives from
System.MulticastDelegate. A delegate is defined as a data structure that refers to
a static method or to a class instance and an instance method of that class. In
other words, a delegate can be used like a type safe pointer. You can use a
delegate to point to a method, much like a callback or the 'AddressOf' operator in
VB.NET. Delegates are often used for asynchronous programming and are the
ideal method for generically defining events.
Before you get into the actual delegate class let us see some code which
simulates the delegate functionality through simple class code for the sake of
understanding. We have a simple class called 'VBDelegate' with two static
methods named 'CallDelegate' and 'DisplayMessage' as shown below. When the
CallDelegate method is called,(when the program is run) to display the message.
Now normally, if we had a class called 'VBDelegate' with a method named
'DisplayMessage'
VB.NET
Public Class VBDelegate
'Static method to call displaymessage function
Public Shared Sub CallDelegate()
DisplayMessage("Some Text")
End Sub
'Static Function to display the message
Private Shared Function DisplayMessage(ByVal strTextOutput As String)
MsgBox(strTextOutput)
End Function
End Class
C#
Public Class CSDelegate
{
'Static method to call displaymessage function
Public static void CallDelegate()
{
DisplayMessage("Some Text")
}
'Static Function to display the message
Private static void DisplayMessage(String strTextOutput)
{
MessageBox.Show(strTextOutput)
}
}
There is nothing wrong with this approach at all. In fact, it is probably more
commonly used than delegates. However, it does not provide much in terms of
flexibility or a dynamic event model. With delegates, you can pass a method
along to something else, and let it execute the method instead. Perhaps you do
not know which method you want to call until runtime, in which case, delegates
also come in handy.
Now we will implement the same functionality as before, using a actual delegate
class of .NET.
Delegates in VB.NET are declared like:
Delegate [Function/Sub] methodname(arg1,arg2..argN)
The declared delegate methodname will have the same method signature as the
methods they want to be a delegate for. This example is calling a shared
method..
VB.NET
Class VBDelegate
'Declaration of delegate variable with arguments
Delegate Function MyDelegate(ByVal strOutput As String)
'Function to call the delegates
Public Shared Sub CallDelegates()
'Declare variables of type Mydelegate points to the
'function MessageDisplay with AddressOf operator
Dim d1 As New MyDelegate(AddressOf MesssageDisplay)
Dim d2 As New MyDelegate(AddressOf MesssageDisplay)
'Pass the arguments to the function through delegates
d1("First Delegation ")
d2("Second Delegation ")
End Sub
'Function to display the message
Private Shared Function MesssageDisplay(ByVal strTextOutput As String)
MsgBox(strTextOutput)
End Function
End Class
C#
Class CSDelegate
{
'Declaration of delegate variable with arguments
delegate void MyDelegate(String strOutput);
'Function to call the delegates
Public static void CallDelegates()
{
'Declare variables of type Mydelegate points to the
'function MessageDisplay
MyDelegate d1 = New MyDelegate(MesssageDisplay);
MyDelegate d2 = New MyDelegate(MesssageDisplay);
'Pass the arguments to the function through delegates
d1("First Delegation ");
d2("Second Delegation ");
}
'Function to display the message
Private static void MesssageDisplay(String strTextOutput)
{
MessgeBox.Show(strTextOutput);
}
}
The Output to the display window is:
First Delegation in one message window
And
Second Delegation in the second message window
What has happened in the above code? Let's take a look
First, we defined a delegate. Remember, when defining a delegate it is very
similar to stating the signature of a method. We have said we want a delegate
that can accept a string as an argument so basically, this delegate can work with
any method that takes the same argument(s).
In our CallDelagates method, we create two instances of our 'MyDelegate'. Then,
we pass into MyDelegate's constructor the address of our 'DisplayMessage'
method. This means that the method we pass into the delegate's constructor (in
this case it's 'DisplayMessage' method) must have a method signature that
accepts a string object as an input argument, just like our delegate does. Now,
you might be thinking, "why are we passing in the address of a method when we
defined our delegate to accept a string object as it's input argument?" In the code
above, we are telling the delegate which method to call, not which string we're
passing in. Carefully understand this concept.
Then finally we have our 'DisplayMessage' method, which takes the string
passed in by the delegate and tells it what string is to be displayed.
The same approach is used while handling the events in the .NET. This topic is
just to understand how delegates works in .NET.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using System.Threading;
using SpeechLib;
using MediaPlayer;
using WMPLib;
using System.Diagnostics;
private SpeechLib.SpSharedRecoContext objRecoContext;
private SpeechLib.ISpeechRecoGrammar grammar = null;
private SpeechLib.ISpeechGrammarRule menuRule = null;
SpVoice spo = new SpVoice();
bool saved = false;
bool enable_speach = false;
bool speach_diabletime_out = false;
private void Form1_Load(object sender, EventArgs e)
{
try
{
objRecoContext = new SpSharedRecoContext();
spo.Speak("Welcome", SpeechVoiceSpeakFlags.SVSFlagsAsync);
spo.WaitUntilDone(1000);
enable_speach = false;
objRecoContext.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(Reco_Event);
}
catch (Exception exp)
{
MessageBox.Show("Install speech Before Statring Akku", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
Application.Exit();
}
}
string result = "";
private void Reco_Event(int StreamNumber, object StreamPosition, SpeechRecognitionType RecognitionType, ISpeechRecoResult Result)
{
result = Result.PhraseInfo.GetText(0, -1, true);
if (result == Properties.Settings.Default.start)
{
System.Diagnostics.Process p = new System.Diagnostics.Process();
//p.StartInfo.FileName = @"C:\Program Files\Mozilla Firefox\firefox.exe";
p.StartInfo.FileName = @"C:\Program Files\Internet Explorer\IEXPLORE.EXE";
p.Start();
p.StartInfo.FileName = @"C:\\Windows";
p.Start();
//System.Diagnostics.Process("C:\\Windows");
}
if (result == Properties.Settings.Default.explorer)
{
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = @"C:\Program Files\Internet Explorer\IEXPLORE.EXE";
p.Start();
}
if (result == Properties.Settings.Default.player)
{
result = "";
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = @"C:\Program Files\Windows Media Player\wmplayer.exe" ;
p.Start();
System.Diagnostics.Process.Start(@"D:\virtulamouse\virtulamouse\01 - Kya Mujhe Pyaar Hai (Kay Kay).mp3");
}
if (result == Properties.Settings.Default.notepad)
{
result = "";
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = @"notepad.exe";
p.Start();
//System.Diagnostics.Process.Start(@"%SystemRoot%\system32\notepad.exe");
}
if (result == Properties.Settings.Default.keyboard)
{
result = "";
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = @"osk.exe";
p.Start();
//System.Diagnostics.Process.Start(@"%SystemRoot%\system32\notepad.exe");
}
serialPort1.Close();
//System.Windows.Forms.SendKeys.SendWait("This text was entered ");
//Thread.Sleep(1000);
//System.Windows.Forms.SendKeys.SendWait("{TAB}");
//Thread.Sleep(1000);
//System.Windows.Forms.SendKeys.SendWait("using the System.Windows.");
//Thread.Sleep(1000);
}
private void btnenablespeech_Click(object sender, EventArgs e)
{
try
{
grammar = objRecoContext.CreateGrammar(0);
menuRule = grammar.Rules.Add("MenuCommands", SpeechRuleAttributes.SRATopLevel | SpeechRuleAttributes.SRADynamic, 1);
object PropValue = "";
menuRule.InitialState.AddWordTransition(null, Properties.Settings.Default.start, " ", SpeechGrammarWordType.SGLexical, Properties.Settings.Default.start, 2, ref PropValue, 1.0F);
menuRule.InitialState.AddWordTransition(null, Properties.Settings.Default.explorer, " ", SpeechGrammarWordType.SGLexical, Properties.Settings.Default.explorer, 3, ref PropValue, 1.0F);
menuRule.InitialState.AddWordTransition(null, Properties.Settings.Default.player, " ", SpeechGrammarWordType.SGLexical, Properties.Settings.Default.player, 4, ref PropValue, 1.0F);
menuRule.InitialState.AddWordTransition(null, Properties.Settings.Default.notepad, " ", SpeechGrammarWordType.SGLexical, Properties.Settings.Default.notepad, 5, ref PropValue, 1.0F);
menuRule.InitialState.AddWordTransition(null, Properties.Settings.Default.keyboard, " ", SpeechGrammarWordType.SGLexical, Properties.Settings.Default.keyboard, 5, ref PropValue, 1.0F);
//menuRule.InitialState.AddWordTransition(null, Properties.Settings.Default.start, " ", SpeechGrammarWordType.SGLexical, Properties.Settings.Default.start, 2, ref PropValue, 1.0F);
//menuRule.InitialState.AddWordTransition(null, Properties.Settings.Default.explorer, " ", SpeechGrammarWordType.SGLexical, Properties.Settings.Default.explorer, 3, ref PropValue, 1.0F);
//menuRule.InitialState.AddWordTransition(null, Properties.Settings.Default.player, " ", SpeechGrammarWordType.SGLexical, Properties.Settings.Default.player, 4, ref PropValue, 1.0F);
//menuRule.InitialState.AddWordTransition(null, Properties.Settings.Default.notepad, " ", SpeechGrammarWordType.SGLexical, Properties.Settings.Default.notepad, 5, ref PropValue, 1.0F);
//menuRule.InitialState.AddWordTransition(null, Properties.Settings.Default.keyboard, " ", SpeechGrammarWordType.SGLexical, Properties.Settings.Default.keyboard, 5, ref PropValue, 1.0F);
grammar.Rules.Commit();
grammar.CmdSetRuleState("MenuCommands", SpeechRuleState.SGDSActive);
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
if (serialPort1.IsOpen == true)
{
serialPort1.Close();
}
return;
}
spo.Speak("start", SpeechVoiceSpeakFlags.SVSFlagsAsync);
spo.WaitUntilDone(1000);
if (button2.Text == "Enable Speech")
{
button2.Text = "Disable Speech";
}
else if (button2.Text == "Disable Speech")
{
objRecoContext.EndStream += new _ISpeechRecoContextEvents_EndStreamEventHandler(wavRecoContext_EndStream);
button2.Text = "Enable Speech";
}
}
01 00 00 00 c0 71 01 00 01 00 50 70 01 00 3f 00 30 60 : AUTO SCAN
01 00 06 00 60 72 01 00 06 1a 50 47 4d 2d 54 30 37 32 34 20 56 31 2e 30 52 34 20 28 30 36 30 35 31 38 29 00 6c 2a 01 00 06 01 01 75 20 : RESPONSE
01 00 3f 01 00 b8 31 : OK
01 00 06 01 00 b5 e1 : RESPONSE
AUTO SCAN AND OK ARE FOR INITIALISATION OF READER.
SO THESE QUERIES SHOULD BE SEND BEFORE CARD INSERT AND SHOULD CHECK POSITIVE RESPONSE ('06' THIRD BYTE OF RESPONSE)FOR PROCEEDING.
QUERIES AND RESPONSE AFTER CARD INSERT
01 00 20 00 00 68 : REQUEST
01 00 06 02 04 00 88 87 : RESPONSE
ALWAYS SEND REQUEST QUERY AND CHECK +VE RESPONSE FOR PROCEEDING
01 00 21 00 90 69 : CARD ID
01 00 06 04 3a 6f ce 42 32 02 : RESPONSE
01 00 22 04 3a 6f ce 42 d6 04 :SELECT
01 00 06 01 08 73 e0 : RESPONSE
01 00 23 02 60 00 44 a6 : AUTHENTICATE WITH SECTOR 0 AND KEYA
01 00 06 01 00 b5 e1 : RESPONSE
01 00 28 05 01 00 00 00 00 9c d5 01 00 24 01 01 7f 80 : VALUE FORMAT
01 00 06 01 00 b5 e1 01 00 06 10 00 00 00 00 ff ff ff ff 00 00 00 00 01 fe 01 fe 71 3d : RESPONSE
01 00 26 06 01 c1 00 00 00 64 29 d3
01 00 24 01 01 7f 80 : VALUE INCREMENT FOR 100
01 00 06 01 00 b5 e1 01 00 06 10 64 00 00 00 9b ff ff ff 64 00 00 00 01 fe 01 fe 0e 61 : RESPONSE
01 00 27 01 01 7f 70 : READ VALUE
01 00 06 04 00 00 00 64 4d eb : RESPONSE WITH 100
01 00 26 06 01 c0 00 00 00 64 e9 ee
01 00 24 01 01 7f 80 : VALUE DECREMENT 100
01 00 06 01 00 b5 e1 01 00 06 10 00 00 00 00 ff ff ff ff 00 00 00 00 01 fe 01 fe 71 3d : RESPONSE
01 00 25 15 01 73 61 6e 6f 6f 6e 00 00 00 00 00 00 00 00 00 00 00 00 00 00 76 c5 : WRITE BLOCK 'xxxx' IN BLOCK 1
01 00 06 01 00 b5 e1 : RESPONSE
01 00 24 01 01 7f 80 : READ FROM BLOCK 1
01 00 06 10 73 61 6e 6f 6f 6e 00 00 00 00 00 00 00 00 00 00 29 a9 : RESPONSE FOR READ (xxxxx)
01 00 2a 00 a0 6e : HALT
01 00 06 01 00 b5 e1 : RESPONSE
user clicks the button. The following example shows how the event can be
defined, raised in the class and used by the object user.
Declaration:- The event member will be declared with Event keyword. Basically
the event member should always be public. Because these are the members will
be used by the external users.
'Declare a class which operates on the Employee collection database
'This class is used to find some summarised operation on the Employee
'collction database, which means finding the relavent employee ‘information, 'getting the
total no. of employees in the collection and ‘others - Its just 'an example to explain how
event works
Public Class EmployeeCollection
'Declare an event which will be raised after finding the data
'Keyword ‘Event’ is used the declare the events
Public Event FindResult(ByVal blnFound As Boolean)
'This method is to find the data from employee colletion database and 'raise the findresult
event to return the result
Public Sub FindData(ByVal Name As String)
'find the Employee with name and return the result as boolean, if
'the data is found then raise FindResult with True else with
'False
Dim found As Boolean
found = FineEmployee(Name)
If found Then
'Raise the event with parameter
RaiseEvent FindResult(True)
Else
'Raise the event with parameter
RaiseEvent FindResult(False)
End If
End Sub
End Class
Usage:- In order to access the events of the objects, the object should be
declared with withevents clause. This is shown in the following example with form
load event.
'Declare the object with WithEvents clause to create an instance
Dim WithEvents objEmpColl As EmployeeCollection = New EmployeeCollection()
Public Sub load()
'Find the Employee with name Rama in the Employee collection
objEmpColl.FindData("Rama")
End Sub
'The following event will be raised after the search operation
Private Sub objObject_FindResult(ByValue blnFound as Boolean) Handles
objObject.FindResult
If blnFound Then
MsgBox("The given Employee is Found in the collection")
Else
MsgBox("The given Employee is not Found")
End If
End Sub
earlier versions of VB we use TYPE and END TYPE to define it). Here it is
defined with STRUCTURE and END STRUCTURE keyword.
It supports allmost all the features of OOPS, like
• Implementing interfaces
• Constructors, methods, properties, fields, constants and events
• Shared constructors
Ex: -
Defining the structure:-
VB.NET
Structure Person
Public FirstName As String
Public LastName As String
Public Address As String
Public Pincode As String
Public DateOFBirth As DateTime
Public Sub DisplayInfo()
Dim msg As String
msg = FirstName & " " & LastName & vbCrLf
msg = msg & Address & vbCrLf
msg = msg & "PIN – " & Pincode
msg = msg & "Date of Birth : " & DateOFBirth.ToString
End Sub
End Structure
C#
struct Person
{
Public String FirstName;
Public String LastName ;
Public String Address ;
Public String Pincode ;
Public DateTime DateOFBirth;
Public void DisplayInfo()
{
String msg=new String();
msg = FirstName + " " + LastName ;
msg = msg + Address ;
msg = msg + "PIN – " + Pincode;
msg = msg + "Date of Birth : " + DateOFBirth.ToString;
}
}
In the example a Person structure is declared to hold the a person’s details like
name, address, pincode etc., we have already seen this person details in terms
of a class object. Basically the structures are used to hold the set of values like
array. Theoritically arrays holds a set of single datatype values, but structures
holds a set of different datatype values. Due to the OOPS feature of .NET we can
implement the methods and properties in the structures too. This is shown in the
person structure. Here the Firstname,Lastname,Address,Pincode,Dateofbirth are
all the variables of person structure with different datatype declarations, where
DisplayInfo is a method to disputably the information.
Variables holds the data and methods operates on the data stored in it as in the
normal class object.
Usage of the structure:-
VB.Net
'Creating an instance of the structure like a normal class variable
Dim varPerson As Person = New Person()
'Setting the structure variables with the values
varPerson.firstname = “Rama”
varPerson.lastname = “Lakan”
varPerson.address = “Mysore”
varPerson.pincode = “570002”
varPerson.dateofbirth = “25/06/1977”
'Calling the strcuture method to manipulate and display the person address
varPerson.displayinfo()
C#
Person varPerson =New Person();
'Setting the structure variables with the values
varPerson.firstname = “Rama”;
varPerson.lastname = “Lakan”;
varPerson.address = “Mysore”;
varPerson.pincode = “570002”;
varPerson.dateofbirth = “25/06/1977”;
'Calling the strcuture method to manipulate and display the person address
varPerson.displayinfo();
cmm.CommandType = CommandType.StoredProcedure
cmm.CommandText = "sp_facility_addupdate"
cmm.Parameters.Add("@facility_id", SqlDbType.BigInt, 0).Value = Convert.ToInt64(hdnFac_ID.Value)
cmm.Parameters("@facility_id").Direction = ParameterDirection.InputOutput
cmm.Parameters.Add("@location_id", SqlDbType.BigInt, 0).Value = Convert.ToInt64(Session("Location_ID"))
cmm.Parameters.Add("@facility_name", SqlDbType.VarChar, 50).Value = txtFacNm.Text
If ddl_FacilityType.SelectedValue = 1 Then
cmm.Parameters.Add("@facility_type", SqlDbType.VarChar, 50).Value = "Production Facility"
ElseIf ddl_FacilityType.SelectedValue = 2 Then
cmm.Parameters.Add("@facility_type", SqlDbType.VarChar, 50).Value = "Repair Facility"
ElseIf ddl_FacilityType.SelectedValue = 3 Then
cmm.Parameters.Add("@facility_type", SqlDbType.VarChar, 50).Value = "Warehouse"
ElseIf ddl_FacilityType.SelectedValue = 4 Then
cmm.Parameters.Add("@facility_type", SqlDbType.VarChar, 50).Value = "Consolidation Center"
End If
cmm.Parameters.Add("@address_1", SqlDbType.VarChar, 50).Value = txtAddr1.Text
cmm.Parameters.Add("@address_2", SqlDbType.VarChar, 50).Value = txtAddr2.Text
cmm.Parameters.Add("@city", SqlDbType.VarChar, 50).Value = txtCity.Text
cmm.Parameters.Add("@state", SqlDbType.VarChar, 50).Value = txtState.Text
cmm.Parameters.Add("@country", SqlDbType.VarChar, 50).Value = ddl_country.SelectedValue
cmm.Parameters.Add("@zip", SqlDbType.VarChar, 50).Value = txtZip.Text
cmm.Parameters.Add("@phone", SqlDbType.VarChar, 50).Value = txtPhone.Text
cmm.Parameters.Add("@isdcode", SqlDbType.VarChar, 20).Value = txtisdcode.Text
cmm.Parameters.Add("@created_id", SqlDbType.BigInt, 0).Value = Convert.ToInt64(lblCreator_ID_Fac.Text)
cmm.Parameters.Add("@last_update_id", SqlDbType.BigInt, 0).Value = Convert.ToInt64(dt.Rows(0)("user_id").ToString())
DataConnect.GetInstance.ExecuteNonQuery(cmm)
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a value");
int a = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("Enter a value");
int b = Convert.ToInt16(Console.ReadLine());
Console.WriteLine(Convert.ToString(b + a));
Console.WriteLine("end");
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter limit");
int limit = Convert.ToInt16(Console.ReadLine());
int sum = 0;
for (int i = 0; i <= limit; i++)
{
sum = sum + i;
}
Console.WriteLine("The sum is :" + sum.ToString());
Console.WriteLine("end");
Console.ReadLine();
}
}
}
SELECTION OF OPERATIONS.....
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the first number.");
int a = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("Enter the second number.");
int b = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("enter an operation to be performed (+,-,*,/)");
string o = Console.ReadLine();
using if condition
if (o.Trim() == "+")
{
Console.WriteLine(Convert.ToString(a+b));
}
else if (o.Trim() == "-")
{
Console.WriteLine(Convert.ToString(a - b));
}
else if (o.Trim() == "*")
{
Console.WriteLine(Convert.ToString(a * b));
}
else if (o.Trim() == "/")
{
Console.WriteLine(Convert.ToString(a / b));
}
Console.WriteLine("end");
Console.ReadLine();
}
}
}
Using switch condition
switch (o.Trim())
{
case "+":
Console.WriteLine(Convert.ToString(a + b));
break;
case "-":
Console.WriteLine(Convert.ToString(a- b));
break;
case "*":
Console.WriteLine(Convert.ToString(a * b));
break;
case "/":
Console.WriteLine(Convert.ToString(a / b));
break;
}
Console.WriteLine("end");
Console.ReadLine();
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter limit");
int limit = Convert.ToInt16(Console.ReadLine());
int PRODUCT = 1;
for (int i = 1; i <= limit; i++)
{
PRODUCT = PRODUCT * i;
}
Console.WriteLine("The product is :" + PRODUCT.ToString());
Console.WriteLine("end");
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter limit");
int limit = Convert.ToInt16(Console.ReadLine());
int i=0;
while (i < limit)
{
Console.WriteLine(i.ToString());
i++;
}
Console.WriteLine("end");
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a value");
int a = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("Enter limit");
int limit = Convert.ToInt16(Console.ReadLine());
int i = 0;
while (i < limit)
{
Console.WriteLine((a*i).ToString());
i++;
}
Console.WriteLine("end");
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a value");
int a = Convert.ToInt16(Console.ReadLine());
int rev = 0, rem = 0;
do
{
rem = a % 10;
rev = rev * 10 + rem;
a = a / 10;
}
while (a > 0);
Console.WriteLine("the reversed no. is "+rev.ToString());
Console.WriteLine("end");
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
class student
{
int rno;
string name, address;
public void getdata(int r, string n, string add)
{
rno = r;
name = n;
address = add;
}
public void display()
{
Console.WriteLine("ROLL NUMBER: "+rno.ToString());
Console.WriteLine("Name: " + name);
Console.WriteLine("Address: " + address);
}
}
static void Main(string[] args)
{
student sd = new student();
Console.WriteLine("enter no,name,address");
sd.getdata(int.Parse(Console.ReadLine()), Console.ReadLine(), Console.ReadLine());
sd.display();
Console.WriteLine("end");
Console.ReadLine();
}
}
}
There is a class & a child class. A class can access the result from child class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
class student
{
int rno;
string name, address;
public void getdata(int r, string n, string add)
{
rno = r;
name = n;
address = add;
}
public void display()
{
Console.WriteLine("ROLL NUMBER: "+rno.ToString());
Console.WriteLine("Name: " + name);
Console.WriteLine("Address: " + address);
}
}
class accadamic : student
{
int rollno, mark1, mark2, total;
public void getmarks(int r, int m1, int m2)
{
rollno = r;
mark1 = m1;
mark2 = m2;
}
public void result()
{
Console.WriteLine("Mark1: " + mark1.ToString());
Console.WriteLine("Mark2: " + mark2.ToString());
total = mark1 + mark2;
Console.WriteLine("Total marks: " + total.ToString());
}
}
static void Main(string[] args)
{
accadamic acd = new accadamic();
Console.WriteLine("enter rollno,name,address");
acd.getdata(Convert.ToInt16(Console.ReadLine()), Console.ReadLine(), Console.ReadLine());
Console.WriteLine("enter marks");
acd.getmarks(Convert.ToInt16(Console.ReadLine()),Convert.ToInt16(Console.ReadLine()),Convert.ToInt16(Console.ReadLine()));
acd.display();
acd.result();
Console.WriteLine("end");
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
String s = textBox1.Text;
MessageBox.Show(s);
String da = dateTimePicker1.Value.ToString("dd/MM/yy");
MessageBox.Show(da);
string nu = numericUpDown1.Value.ToString();
MessageBox.Show(nu);
string ad = textBox2.Text;
MessageBox.Show(ad);
string ph = maskedTextBox1.Text;
MessageBox.Show(ph);
if (radioButton1.Checked == true)
{
MessageBox.Show(radioButton1.Text);
}
else
{
MessageBox.Show(radioButton2.Text);
}
MessageBox.Show(comboBox1.Text);
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.Add("Bsc");
}
}
}
System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
if (checkedListBox1.GetItemChecked(i) == true)
{
comboBox1.Items.Add(checkedListBox1.Items[i].ToString());
}
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
int i = 0;
private void timer1_Tick(object sender, EventArgs e)
{
if (i < imageList1.Images.Count)
{
pictureBox1.Image = imageList1.Images[i];
i++;
progressBar1.Value += (100 / imageList1.Images.Count);
}
else
{
timer1.Stop();
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class calculator : Form
{
public calculator()
{
InitializeComponent();
}
string var1, var2, op;
private void btn1_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text+((Button)sender).Text;
}
private void btn2_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text+((Button)sender).Text;
}
private void btn3_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + ((Button)sender).Text;
}
private void btn4_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + ((Button)sender).Text;
}
private void btn5_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + ((Button)sender).Text;
}
private void btn6_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + ((Button)sender).Text;
}
private void btn7_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + ((Button)sender).Text;
}
private void btn8_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + ((Button)sender).Text;
}
private void btn9_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + ((Button)sender).Text;
}
private void btn0_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + ((Button)sender).Text;
}
private void btnplus_Click(object sender, EventArgs e)
{
var1 = textBox1.Text;
op = "+";
textBox1.Text = "";
}
private void btnminus_Click(object sender, EventArgs e)
{
var1 = textBox1.Text;
op = "-";
textBox1.Text = "";
}
private void btnmul_Click(object sender, EventArgs e)
{
var1 = textBox1.Text;
op = "*";
textBox1.Text = "";
}
private void btndiv_Click(object sender, EventArgs e)
{
var1 = textBox1.Text;
op = "/";
textBox1.Text = "";
}
private void btnequal_Click(object sender, EventArgs e)
{
var2 = textBox1.Text;
if (op == "+")
{
textBox1.Text = Convert.ToString(decimal.Parse(var1) + decimal.Parse(var2));
}
if (op == "-")
{
textBox1.Text = Convert.ToString(decimal.Parse(var1) - decimal.Parse(var2));
}
if (op == "*")
{
textBox1.Text = Convert.ToString(decimal.Parse(var1) * decimal.Parse(var2));
}
if (op == "/")
{
textBox1.Text = Convert.ToString(decimal.Parse(var1) / decimal.Parse(var2));
}
}
private void btnac_Click(object sender, EventArgs e)
{
textBox1.Text = "";
var1 = "0";
var2 = "0";
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class dynamic : Form
{
public dynamic()
{
InitializeComponent();
}
private void dynamic_Load(object sender, EventArgs e)
{
Button btn = new Button();
btn.Text = "Click";
btn.Size = new Size(76, 26);
btn.Location = new Point(100, 100);
this.Controls.Add(btn);
btn.Click += new EventHandler(btn_Click);
}
void btn_Click(object sender, EventArgs e)
{
MessageBox.Show("Clicked");
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class dynamic : Form
{
public dynamic()
{
InitializeComponent();
}
private void dynamic_Load(object sender, EventArgs e)
{
Button btn = new Button();
btn.Text = "Click";
btn.Size = new Size(76, 26);
btn.Location = new Point(100, 100);
this.Controls.Add(btn);
btn.Click += new EventHandler(btn_Click);
}
void btn_Click(object sender, EventArgs e)
{
MessageBox.Show("Clicked");
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class dynamic : Form
{
public dynamic()
{
InitializeComponent();
}
private void dynamic_Load(object sender, EventArgs e)
{
Button btn = new Button();
btn.Text = "Click";
btn.Size = new Size(76, 26);
btn.Location = new Point(100, 100);
this.Controls.Add(btn);
btn.Click += new EventHandler(btn_Click);
}
void btn_Click(object sender, EventArgs e)
{
MessageBox.Show("Clicked");
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Form1 f = new Form1();
f.Show();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Login : Form
{
public Login()
{
InitializeComponent();
}
private void btnlogin_Click(object sender, EventArgs e)
{
if (txtuname.Text == "abc" && txtpwd.Text == "abc")
{
MessageBox.Show("Login Succeed");
}
else
{
MessageBox.Show("Login failed");
}
}
private void btncancel_Click(object sender, EventArgs e)
{
txtpwd.Text = "";
txtuname.Text = "";
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace WindowsFormsApplication1
{
public partial class arraylist : Form
{
public arraylist()
{
InitializeComponent();
}
private void arraylist_Load(object sender, EventArgs e)
{
ArrayList arr = new ArrayList();
arr.Add("hello");
arr.Add("1234");
arr.Add("cmd");
comboBox1.DataSource = arr;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class stack : Form
{
public stack()
{
InitializeComponent();
}
Stack<int> st = new Stack<int>();
private void btnpush_Click(object sender, EventArgs e)
{
st.Push(Convert.ToInt32(textBox1.Text));
listBox1.Items.Add(textBox1.Text);
textBox1.Text = "";
}
private void btnpop_Click(object sender, EventArgs e)
{
int i = st.Pop();
MessageBox.Show(i + "is poped");
int k = st.Count;
listBox1.Items.RemoveAt(k);
listBox1.Refresh();
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class queue : Form
{
public queue()
{
InitializeComponent();
}
Queue<string> qu = new Queue<string>();
private void btnen_Click(object sender, EventArgs e)
{
qu.Enqueue(textBox1.Text);
listBox1.Items.Add(textBox1.Text);
textBox1.Text = "";
}
private void btnde_Click(object sender, EventArgs e)
{
listBox1.Items.RemoveAt(0);
listBox1.Refresh();
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class calc : Form
{
public calc()
{
InitializeComponent();
}
Stack<int> st = new Stack<int>();
private void button1_Click(object sender, EventArgs e)
{
string[] ar = new string[100];
string fg = textBox1.Text;
ar = fg.Split('+');
int total = 0;
for (int i = 0; i < ar.Length; i++)
{
st.Push(Convert.ToInt32(ar[i].ToString()));
listBox1.Items.Add(ar[i].ToString());
}
MessageBox.Show(st.Count.ToString());
for (int i = st.Count; i > 0; i--)
{
int s = st.Pop();
total = total + s;
}
MessageBox.Show(Convert.ToString(total));
}
}
}
Com: =Microsoft Shell Controls and Automation
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Shell32;
namespace WindowsFormsApplication1
{
public partial class com : Form
{
public com()
{
InitializeComponent();
}
private void com_Load(object sender, EventArgs e)
{
}
ShellClass sc = new ShellClass();
private void button1_Click(object sender, EventArgs e)
{
sc.ShutdownWindows();
}
private void button2_Click(object sender, EventArgs e)
{
sc.RefreshMenu();
}
private void button4_Click(object sender, EventArgs e)
{
sc.SetTime();
}
private void button5_Click(object sender, EventArgs e)
{
sc.FindComputer();
sc.FindFiles();
sc.FileRun();
sc.Help();
sc.MinimizeAll();
sc.ControlPanelItem("C://");
sc.EjectPC();
sc.GetHashCode();
sc.Windows();
sc.Suspend();
sc.UndoMinimizeALL();
sc.TrayProperties();
}
}
}
Win32-Operating System
CSName,Caption,Windows directory,serialno,registered user,no of processes
Win32-Bios
Version,name,manufacturer,serialnumber,release date,current language,primary bios,status
Win32-ComputerSystem
Manufacturer,username,totalphysicalmemory,system type,model
Win32-Processor
Caption,Current Clock Speed
Win32-TimeZone
Caption
Win32-CDROMDrive
Caption,mediatype,drive,manufacturer,description
Win32-Account
Name,description,domain,sid
Win32-DiskDrive
Caption,deviceid,index,partitions
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Management;
namespace WindowsFormsApplication1
{
public partial class WMI : Form
{
public WMI()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SelectQuery query = new SelectQuery("Win32OperatingSystem");
ManagementObjectSearcher search = new ManagementObjectSearcher(query);
foreach (ManagementObject mobj in search.Get())
{
MessageBox.Show(mobj["CSname"].ToString());
}
}
private void WMI_Load(object sender, EventArgs e)
{
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace WindowsFormsApplication1
{
public partial class graphics : Form
{
public graphics()
{
InitializeComponent();
}
private void graphics_Load(object sender, EventArgs e)
{
}
private void graphics_Paint(object sender, PaintEventArgs e)
{
Graphics g;
g = this.CreateGraphics();
Pen p = new Pen(Brushes.Purple, 3);
g.DrawEllipse(p, 100, 100, 100, 100);
g.FillEllipse(Brushes.Plum, 200, 100, 100, 200);
g.DrawLine(p, new Point(100, 200), new Point(200, 100));
Point[] p1 = new Point[5];
p1[0] = new Point(100, 50);
p1[1] = new Point(200, 100);
p1[2] = new Point(250, 100);
p1[3] = new Point(200, 50);
p1[4] = new Point(100, 250);
g.DrawPolygon(p, p1);
System.Drawing.Font f = new Font("Times New Roman", 15, FontStyle.Bold);
StringFormat f1 = new StringFormat(StringFormatFlags.NoClip);
StringFormat f2 = new StringFormat(f1);
f1.LineAlignment = StringAlignment.Center;
f2.FormatFlags = StringFormatFlags.DirectionVertical;
f2.Alignment = StringAlignment.Far;
Rectangle r=new Rectangle(100,100,100,100);
g.DrawRectangle(Pens.Brown, r);
g.DrawString("Hello world", f, Brushes.Red, r.X, r.Y, f1);
g.DrawString("Hai", f, Brushes.SlateBlue, r.Left, r.Top, f2);
}
}}