Tuesday, 30 April 2013

pmr 6


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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("I am a line");
            Console.WriteLine("I am a circle");
            Console.WriteLine("I am a square");
            Console.ReadLine();
        }
    }
}

John is a software developer, who works for Mega Technologies. He is currently under the project of geometric calculations. The program that John has to develop should: Calculate the area of circle. Calculate the volume of the cube.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace ConsoleApplication7

{



class geo

{

int a, r,area,volume;

public void ca()

{

Console.WriteLine("Enter the radius of circle:");

r = Convert.ToInt32(Console.ReadLine());

area = 22/7 * r * r;

Console.WriteLine("the area of circle:" + area);

}

public void cv()

{

Console.WriteLine("Enter the side of cone:");

a = Convert.ToInt32(Console.ReadLine());

volume = a*a*a;

Console.WriteLine("the volume of cone:" + volume);

}

}

class Program

{

static void Main(string[] args)

{

int s;

Console.WriteLine("Enter 1 for area of circle and 2 for Volume of cone:");

s=Convert.ToInt32(Console.ReadLine());

geo g = new geo();

switch (s)

{

case 1:

g.ca();

break;

case 2:

g.cv();

break;

default:

Console.WriteLine("invalid selection");

break;

}

Console.ReadLine();

}

}

}


Sam is working on an application wherein he needs to accept the size of a one dimensional array and its values. He needs to identify the largest and smallest number in the array and based on that he needs to display the result. Help him to create the application.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] io=new int[3]{5,4,6};
            Console.WriteLine("Sam is working on an application wherein he needs to accept the size of a one dimensional array and its values. He needs to identify the largest and smallest number in the array and based on that he needs to display the result. Help him to create the application.  ");
            if(io[0]>io[1] && io[0]>io[2])
            {
                Console.WriteLine("the largest number is:" + io[0]);
            }
            if (io[1] > io[0] && io[1] > io[2])
            {
                Console.WriteLine("the largest number is:" + io[1]);
            }
            if (io[2] > io[0] && io[2] > io[1])
            {
                Console.WriteLine("the largest number is:" + io[2]);
            }
            if (io[0] < io[1] && io[0] < io[2])
            {
                Console.WriteLine("the smallest number is:" + io[0]);
            }
            if (io[1] < io[0] && io[1] < io[2])
            {
                Console.WriteLine("the smallest number is:" + io[1]);
            }
            if (io[2] < io[0] && io[2] < io[1])
            {
                Console.WriteLine("the smallest number is:" + io[2]);
            }
            Console.ReadLine();
        }
    }
}

Monday, 29 April 2013

Create or delete a macro


Create or delete a macro

Show All
To automate a repetitive task, you can quickly record a macro in Microsoft Excel. You can also create a macro by using the Visual Basic Editor in Microsoft Visual Basic for Applications (VBA) to write your own macro script, or to copy all or part of a macro to a new macro. After you create a macro, you can assign it to an object (such as a toolbar button, graphic, or control) so that you can run it by clicking the object. If you no longer use a macro, you can delete it.
What do you want to do?

Record a macro

When you record a macro, the macro recorder records all the steps required to complete the actions that you want your macro to perform. Navigation on the Ribbon is not included in the recorded steps.
  1. If the Developer tab is not available, do the following to display it:
    1. Click the File tab.
  1. Click Options, and then click Customize Ribbon.
  2. In the Customize Ribbon category, in the Main Tabs list, select the Developer check box, and then click OK.
  1. To set the security level temporarily to enable all macros, do the following:
  1. On the Developer tab, in the Code group, click Macro Security.
The Code group on the Developer tab
  1. Under Macro Settings, click Enable all macros (not recommended, potentially dangerous code can run), and then click OK.
Note    To help prevent potentially dangerous code from running, we recommend that you return to any one of the settings that disable all macros after you finish working with macros.
For more information about how to change the settings, see Change macro security settings in Excel.
  1. On the Developer tab, in the Code group, click Record Macro.
  2. In the Macro name box, enter a name for the macro.
Note    The first character of the macro name must be a letter. Subsequent characters can be letters, numbers, or underscore characters. Spaces cannot be used in a macro name; an underscore character works well as a word separator. If you use a macro name that is also a cell reference, you may get an error message that the macro name is not valid.
  1. To assign a CTRL combination shortcut key to run the macro, in the Shortcut key box, type any lowercase letter or uppercase letter that you want to use.
Note    The shortcut key will override any equivalent default Excel shortcut key while the workbook that contains the macro is open.
For a list of CTRL combination shortcut keys that are already assigned in Excel, see Excel shortcut and function keys.
  1. In the Store macro in list, select the workbook where you want to store the macro.
Tip    If you want a macro to be available whenever you use Excel, select Personal Macro Workbook. When you select Personal Macro Workbook, Excel creates a hidden personal macro workbook (Personal.xlsb) if it does not already exist, and saves the macro in this workbook. In Windows Vista, this workbook is saved in the C:\Users\user name\AppData\Local\Microsoft\Excel\XLStart folder. In Microsoft Windows XP, this workbook is saved in the C:\Documents and Settings\user name\Application Data\Microsoft\Excel\XLStart folder. Workbooks in the XLStart folder are opened automatically whenever Excel starts. If you want a macro in the personal macro workbook to be run automatically in another workbook, you must also save that workbook in the XLStart folder so that both workbooks are opened when Excel starts.
  1. In the Description box, type a description of the macro.
  2. Click OK to start recording.
  3. Perform the actions that you want to record.
  4. On the Developer tab, in the Code group, click Stop Recording Button image.
Tip    You can also click Stop Recording Button image on the left side of the status bar.

Create a macro by using VBA

  1. If the Developer tab is not available, do the following to display it:
    1. Click the File tab.
  1. Click Options, and then click Customize Ribbon.
  2. In the Customize Ribbon category, in the Main Tabs list, select the Developer check box, and then click OK.
  1. To set the security level temporarily to enable all macros, do the following:
  1. On the Developer tab, in the Code group, click Macro Security.
The Code group on the Developer tab
  1. Under Macro Settings, click Enable all macros (not recommended, potentially dangerous code can run), and then click OK.
Note    To help prevent potentially dangerous code from running, we recommend that you return to any one of the settings that disable all macros after you finish working with macros.
For more information about how to change the settings, see Change macro security settings in Excel.
  1. On the Developer tab, in the Code group, click Visual Basic.
  1. If needed, in the Visual Basic Editor, on the Insert menu, click Module.
Note    Modules are automatically created for all sheets in the workbook.
  1. In the code window of the module, type or copy the macro code that you want to use.
  2. To run the macro from the module window, press F5.
  3. In the Visual Basic Editor, on the File menu, click Close and Return to Microsoft Excel when you finish writing the macro.

Copy part of a macro to create another macro

  1. If the Developer tab is not available, do the following to display it:
    1. Click the File tab.
  1. Click Options, and then click Customize Ribbon.
  2. In the Customize Ribbon category, in the Main Tabs list, select the Developer check box, and then click OK.
  1. To set the security level temporarily to enable all macros, do the following:
  1. On the Developer tab, in the Code group, click Macro Security.
The Code group on the Developer tab
  1. Under Macro Settings, click Enable all macros (not recommended, potentially dangerous code can run), and then click OK.
Note    To help prevent potentially dangerous code from running, we recommend that you return to any one of the settings that disable all macros after you finish working with macros.
For more information about how to change the settings, see Change macro security settings in Excel.
  1. Open the workbook that contains the macro that you want to copy.
  2. On the Developer tab, in the Code group, click Macros.
  3. In the Macro name box, click the name of the macro that you want to copy.
  4. Click Edit.
  5. In the code window of the Visual Basic Editor, select the lines of the macro that you want to copy.
Tip    To copy the whole macro, include the Sub and End Sub lines in the selection.
  1. On the Edit menu, click Copy.
Tip    You can also right-click and then click Copy, or you can press CTRL+C.
  1. In the Procedure box of the code window, click the module where you want to place the code.
  2. On the Edit menu, click Paste.
Tip    You can also right-click and then click Paste, or you can press CTRL+V.
Tip    You cannot edit your Personal Macro Workbook file (Personal.xlsb) because it is a hidden workbook that is always open. You must first unhide it by using the Unhide command. You can also open it in the Visual Basic Editor by pressing ALT+F11.

Assign a macro to an object, graphic, or control

  1. On a worksheet, right-click the object, graphic, or control to which you want to assign an existing macro, and then click Assign Macro.
  2. In the Macro name box, click the macro that you want to assign.

Delete a macro

  1. Do one of the following:
    • Open the workbook that contains the macro that you want to delete.
    • If the macro that you want to delete is stored in the personal macro workbook (Personal.xlsb), and this workbook is hidden, do the following to unhide the workbook:
      1. On the View tab, in the Window group, click Unhide.
      2. Under Unhide workbooks, click PERSONAL, and then click OK.
  2. If the Developer tab is not available, do the following to display it:
  1. Click the File tab.
  1. Click Options, and then click Customize Ribbon.
  2. In the Customize Ribbon category, in the Main Tabs list, select the Developer check box, and then click OK.
  1. On the Developer tab, in the Code group, click Macros.
The Code group on the Developer tab
  1. In the Macros in list, select the workbook that contains the macro that you want to delete. For example, clickThis Workbook.
  2. In the Macro name box, click the name of the macro that you want to delete.
  3. Click Delete.

Saturday, 27 April 2013

jhon wants to enter player name (range int 1 to 4)

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

namespace ConsoleApplication20
{
    class Program
    {
        static void Main(string[] args)
        {
            int a,i;
            Console.WriteLine("enter the number of players");
            a=Convert.ToInt32(Console.ReadLine());

            string[] arr= new string [a];
            for(i=0;i<a;i++)
            {
                arr[i]=Console.ReadLine();
            }
            for (i = 0; i < a; i++)
            {
                Console.WriteLine("name of player:" + arr[i]);
            }
            Console.ReadLine();
        }
    }
}

lab @ home 10

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace ConsoleApplication4



{
class duplicate



{
static void Main(string[] args)



{
char[] ch = new char[5] { 'a', 'b', 'a', 'c', 'd' };

int i, count = 0;

Console.WriteLine("enter a character:");

char input = Convert.ToChar(Console.ReadLine());

for (i = 0; i < 5; i++)



{
if (ch[i] == input)



{

count++;

}

}


Console.WriteLine("{0} appears {1} times in the character array", input, count);

Console.ReadLine();





}

}

}

Friday, 26 April 2013

File handling....in C#

File handling in C#
 
File handling is an unmanaged resource in your application system. It is outside your application domain (unmanaged resource). It is not managed by CLR.
 
Data is stored in two ways, persistent and non-persistent manner.
 
When you open a file for reading or writing, it becomes stream.
 
Stream: Stream is a sequence of bytes traveling from a source to a destination over a communication path.
 
The two basic streams are input and output streams. Input stream is used to read and output stream is used to write.
 
The System.IO namespace includes various classes for file handling.
 
The parent class of file processing is stream. Stream is an abstract class, which is used as the parent of the classes that actually implement the necessary operations.
 
The primary support of a file as an object is provided by a .NET Framework class called File. This static class is equipped with various types of (static) methods to create, save, open, copy, move, delete, or check the existence of a file.
 
Diagram to represent file-handling class hierarchy 
 
 file_handling.gif
 
Note: FileIno, DirectoryInfo and DriveInfo classes have instance methods. File, Directory, Path classes have static methods.
 
The following table describes some commonly used classes in the System.IO namespace.
 
Class Name
Description

FileStream

It is used to read from and write to any location within a file
BinaryReader
It is used to read primitive data types from a binary stream
BinaryWriter
It is used to write primitive data types in binary format
StreamReader
It is used to read characters from a byte Stream
StreamWriter
It is used to write characters to a stream.
StringReader
It is used to read from a string buffer
StringWriter
It is used to write into a string buffer
DirectoryInfo
It is used to perform operations on directories
FileInfo
It is used to perform operations on files


 

Reading and writing in the text file

 

StreamWriter Class

 
The StreamWriter class in inherited from the abstract class TextWriter. The TextWriter class represents a writer, which can write a series of characters.
 
The following table describes some of the methods used by StreamWriter class.
 
Methods
Description
Close
Closes the current StreamWriter object and the underlying stream

Flush

Clears all buffers for the current writer and causes any buffered data to be written to the underlying stream

Write

Writes to the stream

WriteLine

Writes data specified by the overloaded parameters, followed by end of line

 

Program to write user input to a file using StreamWriter Class

 
using System;
using System.Text;
using System.IO;
 
namespace FileWriting_SW
{
    class Program
    {
        class FileWrite
        {
            public void WriteData()
            {
                FileStream fs = new FileStream("c:\\test.txt", FileMode.Append, FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs);
                Console.WriteLine("Enter the text which you want to write to the file");
                string str = Console.ReadLine();
                sw.WriteLine(str);
                sw.Flush();
                sw.Close();
                fs.Close();
            }
        }
        static void Main(string[] args)
        {
            FileWrite wr = new FileWrite();
            wr.WriteData();
        }
    }
}
 

StreamReader Class

 
The StreamReader class is inherited from the abstract class TextReader. The TextReader class represents a reader, which can read series of characters.
 
The following table describes some methods of the StreamReader class.
 
Methods
Description
Close
Closes the object of StreamReader class and the underlying stream, and release any system resources associated with the reader
Peek
Returns the next available character but doesn't consume it
Read
Reads the next character or the next set of characters from the stream
ReadLine
Reads a line of characters from the current stream and returns data as a string
Seek
Allows the read/write position to be moved to any position with the file

 

Program to read from a file using StreamReader Class

 
using System;
using System.IO;
 
namespace FileReading_SR
{
    class Program
    {
        class FileRead
        {
            public void ReadData()
            {
                FileStream fs = new FileStream("c:\\test.txt", FileMode.Open, FileAccess.Read);
                StreamReader sr = new StreamReader(fs);
                Console.WriteLine("Program to show content of test file");
                sr.BaseStream.Seek(0, SeekOrigin.Begin);
                string str = sr.ReadLine();
                while (str != null)
                {
                    Console.WriteLine(str);
                    str = sr.ReadLine();
                }
                Console.ReadLine();
                sr.Close();
                fs.Close();
            }
        }
        static void Main(string[] args)
        {
            FileRead wr = new FileRead();
            wr.ReadData();
 
        }
    }
}
 
I hope that this article would have helped you in understanding file handling.
 

Your feedback and constructive contributions are welcome. Please feel free to contact me for feedback or comments you may have about this article.