Archive for the ‘C#’ Category

Nested classes in C#

Wednesday, September 6th, 2006

A sample code for nested classes in C#.NET

nested.cs

public class zzz
{
public static void Main()
{
yyy.xxx a = new yyy.xxx();
a.ni();
}
}
public class yyy
{
public xxx abc()
{
return new xxx();
}
public class xxx
{
public void ni(){
System.Console.WriteLine(”HERE”);
}
}
}

Namespace in C#

Wednesday, August 9th, 2006

C# code : Look how in class1.cs the abc function is being called from class2.cs.

class1.cs

using newy;
class class1 : class2
{
public static void Main()
{
abc();
}
}

class2.cs

using System;
namespace newy{
public class class2{
static void Main()
{
abc();
}
public static void abc()
{
string a=System.Console.ReadLine();
System.Console.Write(a);
}}
}

oneinallnamespace.cs (Example)

class zzz
{
static void Main()
{
vijay.yyy.abc();
abc();
zzz.abc();
}
public static void abc()
{
System.Console.WriteLine(”abc in zzz “);
}
}
namespace vijay
{
class yyy
{
public static void abc()
{
System.Console.WriteLine(”abc”);
}}
}

using System.Diagnostics

Tuesday, July 25th, 2006

checkdll.css.

using System;
using System.Diagnostics;
using System.IO;

namespace FrameworkExamples
{
//HOW TO Get information about the modules being referenced
// by a running process with the DOT NET Framework

class SampleProcessModules
{
static void Main()
{
Process[] p;

//get the current process
//p = Process.GetCurrentProcess(”iexplore.exe”);
p=Process.GetProcessesByName(”Iexplore”);

//get all the dlls this class is using
foreach(ProcessModule module in p.Modules)
{
//dll name
Print(module.ModuleName, 20);

//full path
Print(module.FileName, 75);

//dll version
Print(module.FileVersionInfo.FileVersion, 20);

//last write time
Print(File.GetLastWriteTime(module.FileName).ToShortDateString(), 20);

Console.WriteLine();
}

//cleanup
p.Close();
p = null;
}

//utility function for writing to the console
static void Print(string towrite, int maxlen)
{
if (towrite.Length >= maxlen)
{
Console.Write(towrite.Substring(0, maxlen – 4)+”… “);
return;
}
towrite += new string(’ ‘, maxlen – towrite.Length);

Console.Write(towrite);
}
}
}

Calling dll and Calling int C# code

Friday, July 7th, 2006

1.Callingint.cs
===

class zzz
{
static void Main()
{
int ii;
ii = abc();
System.Console.WriteLine(”hi {0}”,ii);
}
static int abc()
{
System.Console.WriteLine(”abc”);
return 100;
}
}

===

2. Callingdll.css
===

public class tt{

public static void Main(){
zzz making=new zzz();

making.abc();
}

}

Compile by using csc callingDLL.cs /r:ocx.dll

===

Calling function in C#

Tuesday, May 16th, 2006

Callingfunct.cs

class zzz
{
static void Main()
{
abc();
}
static void abc()
{
System.Console.WriteLine (”Hell”);
zzz a=new zzz();
a.bbc(); //HAVE TO CREATE TO CALL NON STATIC
ggg(); // CALL STATIC DIRECTLY
}
void bbc()
{
System.Console.WriteLine(”Hi”);
ccc(); //SAME TYPE CALL DIRECTLY
}
void ccc()
{
System.Console.Write(”BYE”);
}
static void ggg()
{
System.Console.Write(”Adios”);}
}

Callingfunction.cs
class callingfunction
{
static void Main(){

abc();
}
static void abc(){
System.Console.WriteLine(”Function called from main”);
}
}

Boolean in C#

Sunday, May 7th, 2006

Define:Boolean

The base class is System.ValueType, System.Object.
Assembly: Mscorlib (in Mscorlib.dll)

Instances of this type have a resulting value as true or false.

It is similar to bool as in other programming languages and you can find an example on the same in C# below.

Bool.cs

class zzz
{
static void Main()
{
bool ii;
ii=true;
if ( ii )
{
System.Console.WriteLine (”Hi”);
System.Console.WriteLine (”Bye”);
}
}
}

Public methods for the same include : CompareTo, Equals, GetHashCode, GetType, GetTypeCode, Parse, ToString.

More about boolean structure can also be found out on the msdn site at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystembooleanclasstopic.asp

Read and write application in C#

Wednesday, May 3rd, 2006

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace csharpWindowsApplication1
{
///


/// Summary description for Form1.
///

public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.MenuItem menuItem4;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.MenuItem menuItem5;

private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem6;
private System.Windows.Forms.MenuItem menuItem7;

///


/// Required designer variable.
///

private System.ComponentModel.Container components = null;

public Form1()
{

//
// Required for Windows Form Designer support
//
InitializeComponent();
System.IO.StreamReader s=new System.IO.StreamReader(”a.txt”,System.Text.Encoding.ASCII);
string d=s.ReadToEnd().ToString();
textBox1.Text=d;
s.Close();

System.IO.StreamReader t=new System.IO.StreamReader(”b.txt”,System.Text.Encoding.ASCII);
string e=t.ReadToEnd().ToString();
textBox2.Text=e;
t.Close();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

///


/// Clean up any resources being used.
///

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
///


/// Required method for Designer support – do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem5 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.menuItem4 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem6 = new System.Windows.Forms.MenuItem();
this.menuItem7 = new System.Windows.Forms.MenuItem();
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1,
this.menuItem3,
this.menuItem2,
this.menuItem6,
this.menuItem7});
this.mainMenu1.RightToLeft = System.Windows.Forms.RightToLeft.No;
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem5});
this.menuItem1.Text = “&File”;
//
// menuItem5
//
this.menuItem5.Index = 0;
this.menuItem5.Text = “E&xit”;
this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click);
//
// menuItem3
//
this.menuItem3.Index = 1;
this.menuItem3.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem4});
this.menuItem3.Shortcut = System.Windows.Forms.Shortcut.CtrlShiftH;
this.menuItem3.Text = “&Help”;
//
// menuItem4
//
this.menuItem4.Index = 0;
this.menuItem4.Shortcut = System.Windows.Forms.Shortcut.F1;
this.menuItem4.Text = “About”;
this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
//
// menuItem2
//
this.menuItem2.Index = 2;
this.menuItem2.Shortcut = System.Windows.Forms.Shortcut.F2;
this.menuItem2.Text = ” ->”;
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click_1);
//
// menuItem6
//
this.menuItem6.Index = 3;
this.menuItem6.Text = “11/3/2002 8:09:41 PM”;
//
// menuItem7
//
this.menuItem7.Index = 4;
this.menuItem7.Text = “< - ";
this.menuItem7.Click += new System.EventHandler(this.menuItem7_Click);
//
// button1
//
this.button1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(224)), ((System.Byte)(224)), ((System.Byte)(224)));
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Font = new System.Drawing.Font("Verdana", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.button1.ForeColor = System.Drawing.SystemColors.Desktop;
this.button1.Location = new System.Drawing.Point(208, 280);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(120, 24);
this.button1.TabIndex = 2;
this.button1.Text = "&Save";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.BackColor = System.Drawing.Color.WhiteSmoke;
this.textBox1.Font = new System.Drawing.Font("Verdana", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.textBox1.ForeColor = System.Drawing.SystemColors.ActiveCaption;
this.textBox1.Location = new System.Drawing.Point(8, 32);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(240, 232);
this.textBox1.TabIndex = 4;
this.textBox1.Text = "";
//
// label1
//
this.label1.BackColor = System.Drawing.Color.LightSteelBlue;
this.label1.Font = new System.Drawing.Font("Verdana", 10F, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Underline), System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label1.ForeColor = System.Drawing.SystemColors.Highlight;
this.label1.Location = new System.Drawing.Point(16, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(64, 16);
this.label1.TabIndex = 5;
this.label1.Text = "To Do...";
//
// label2
//
this.label2.BackColor = System.Drawing.Color.LightSteelBlue;
this.label2.Font = new System.Drawing.Font("Verdana", 10F, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Underline), System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label2.ForeColor = System.Drawing.SystemColors.ActiveCaption;
this.label2.Location = new System.Drawing.Point(288, 8);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(88, 16);
this.label2.TabIndex = 6;
this.label2.Text = "Checkout";
this.label2.Click += new System.EventHandler(this.label2_Click);
//
// textBox2
//
this.textBox2.AutoSize = false;
this.textBox2.BackColor = System.Drawing.Color.WhiteSmoke;
this.textBox2.Font = new System.Drawing.Font("Verdana", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.textBox2.ForeColor = System.Drawing.SystemColors.ActiveCaption;
this.textBox2.Location = new System.Drawing.Point(280, 32);
this.textBox2.Multiline = true;
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(224, 232);
this.textBox2.TabIndex = 7;
this.textBox2.Text = "";
this.textBox2.TextChanged += new System.EventHandler(this.textBox2_TextChanged);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.Color.LightSteelBlue;
this.ClientSize = new System.Drawing.Size(518, 319);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.textBox2,
this.label2,
this.label1,
this.textBox1,
this.button1});
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Opacity = 0.99000000953674316;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Jnote Version 2.0";
this.ResumeLayout(false);

}
#endregion

///


/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void menuItem1_Click(object sender, System.EventArgs e)
{

}

private void menuItem4_Click(object sender, System.EventArgs e)
{

new Form2().Show();

// Application.Run(new Form2());

}

private void menuItem2_Click(object sender, System.EventArgs e)
{
//p.Write(listBox1.Items.Count.ToString());
//397983 repair
System.IO.StreamWriter p=new System.IO.StreamWriter(”d://a.txt”,true,System.Text.Encoding.ASCII);
p.Write(textBox1.Text);
p.Close();

}

private void button1_Click(object sender, System.EventArgs e)
{
System.IO.StreamWriter p=new System.IO.StreamWriter(”a.txt”,false,System.Text.Encoding.ASCII);
p.Write(textBox1.Text);
p.Close();
System.IO.StreamWriter q=new System.IO.StreamWriter(”b.txt”,false,System.Text.Encoding.ASCII);
q.Write(textBox2.Text);
q.Close();

}

private void menuItem5_Click(object sender, System.EventArgs e)
{
base.Dispose();
Application.Exit();

}

private void menuItem2_Click_1(object sender, System.EventArgs e)
{
this.Opacity=(this.Opacity)+0.1;
}

private void menuItem7_Click(object sender, System.EventArgs e)
{
this.Opacity=(this.Opacity)-0.1;
}

private void textBox2_TextChanged(object sender, System.EventArgs e)
{

}

private void label2_Click(object sender, System.EventArgs e)
{

}
}
}

Arrays in C# dot net

Saturday, April 29th, 2006

Array is a subtype of System.Array and can be derived from System.Object

Arrays can be single or multi dimensionals.

Below i present to you an example of single array in C#

Array.cs

class zzz
{
static void Main()
{
int[] a= new int[3];
int i;
for( i=0; i<=2; i++)
a[i]= i*10;
for( i=0; i<=2; i++)
System.Console.WriteLine(a[i]);
}
}

Abstract classes in C#

Thursday, April 27th, 2006

1.You cant initialize these classes.
2.For this you use the modifier : abstract
3.Used for : Classes and methods in C#

Abstract.cs

public class zzz
{
public static void Main()
{
new bbb();
}
}
abstract class aaa
{
abstract public void pqr();
public int i;
public void abc()
{
}
}
class bbb : aaa
{
public override void pqr()
{
}
}

2classes.cs and A.cs

Wednesday, April 19th, 2006

1. 2classes.cs
===================================
class zzz
{
public static void Main()
{
second yo=new second();
yo.hey();
second.hey1();
}
}
class second
{
public void hey()
{
System.Console.Write(”YOYO”);
}
public static void hey1()
{
System.Console.Write(”YOYO static”);
}
}
===================================

2. A.cs
===================================
C# Source File — Created with SAPIEN Technologies PrimalSCRIPT(TM)
NAME: filename
AUTHOR: Ashish Thakkar , Jvw
DATE : 3/3/06
COMMENT: comment

class zzz
{
public static void Main()
{
System.Console.WriteLine(”Content-Type:text/html\n”);
System.Console.WriteLine(”hi<b>bye</b>”);
}
}
===================================

Next post we will see example for Abstract classes in C#

New section for C# source code

Friday, March 31st, 2006

C#.NET* is Microsoft’s latest programming language.

This section of the blog will contain the source for code written in C#.

*The .NET framework is a development framework that provides a new programming interface to Windows services and APIS and integrates a number of technologies that emerged from Microsoft during the late 90s.

The code may contain

  • Free source code.
  • Source code for softwares that are available for sale.
  • Code for C# softwares , asp.net and web services.

Tommorow we will start with the C# tutorial chapters.

It is adviced you get ready with your CLR/DOT net framework or buy a copy of Microsoft Visual studio .NET.