0

Read and Write to Text Files in c#

Category:


There are many ways to read and write to text files. We will see one option here. Be sure to add using System.IO namespace.
This is a function to write to a Text File.
private bool writeToTextFile()
    {
        bool success = true;
        //path and filename
        string fileName = Server.MapPath("abc.txt");
        //data to be written in the file
        //\r\n is to insert a new line
        string data = "This is the first line \r\n" +
                      "This is the second line \r\n\r\n" +
                      "This is the third line";
        try
        {
          File.WriteAllText(fileName, data);//write the data to the text file
        }
        catch (Exception e)
        {
            Response.Output.Write(e.Message);
            success = false;
        }
        return success;
    }
In the above function,\r\n is used to insert a new line.
This function is used to read from text file.
 
      private bool readFromTextFile()
    {
        bool success = true;
        //path and filename
        string fileName = Server.MapPath("abc.txt");
        try
        {
            string data = File.ReadAllText(fileName);//read from text file
            data = data.Replace("\r\n", "");//\r\n is replaced by
            Response.Output.Write(data);           
        }
        catch (Exception e)
        {
            Response.Output.Write(e.Message);
            success = false;
        }
        return success;
    }

0

Operator Overloading Page 2

Category:

class operatordemo
{
int a, b;
string s1, s2;
public operatordemo()
{
a = 10; b = 20; s1 = "csharp"; s2 = ".net";
}
public static int operator +(operatordemo r1, operatordemo r2)
{
int tot;
string str;
str = r1.s1 + r2.s2;
tot = r1.a + r2.b;
MessageBox.Show(str);
return tot;
}
}
private void button1_Click(object sender, EventArgs e)
{
operatordemo obj1 = new operatordemo();
operatordemo obj2 = new operatordemo();
int t;
t = obj1+obj2;
MessageBox.Show(t.ToString());
}

Press F5 and you see this Outputs



GOT it?
First Page in Operator Overloading

0

Operator Overloading Page 1

Category:

Operator Overloading:
                Using operator overloading we can change the meaning to the existing operator, All operator over-loading functions should be static, Using operator keyword we can over-load unary-operator and binary operators.

Example on Operator Overloading:

Start Microsoft Visual Studio - Click on file- New- Open New Project


Name your Project in the given field and press OK button, there you see a form that is in design mode

Add a Button Control on to your form, from ToolBox


Double click on Button here comes the Coding Part


0

Function Overloading Page 2

Category:

You need to include this Overloading functions in Our Class Named "emp" as shown below

class emp
{
       int ts;
//I am creating three functions using same name
       public void tsal(int sal)
       {
             MessageBox.Show(sal.ToString(),"Salary");
       }
       public void tsal(int sal,int bonus)
      {
            ts = sal + bonus;
            MessageBox.Show(ts.ToString(),"Salar+Bonus is ");
      }
      public void tsal(int sal, int bonus, int ta)
      {
             ts = sal + bonus + ta;
             MessageBox.Show(ts.ToString(),"Salary+Bonus+ta is ");
       }

}
private void Form1_Load(object sender, EventArgs e)
{

}
//I am calling three functions using button click event
private void button1_Click(object sender, EventArgs e)
{
      emp e1 = new emp();
      int s, b, t;
      s = 5000;
      b = 300;
       t = 200;
       e1.tsal(s);
       e1.tsal(s,b);
       e1.tsal(s,b,t);

}

Coming to the Output Use f5

You see a window as shown below


Click on Button

Output1
You see Salary=5000then click OK
Output2


You see Salary +Bonus =5300
Output3

You see Salary+Bonus+ta =5500
Got it?
To know about Operator Overloading Click Here

0

Function Overloading Page 1

Category:

Function Overloading

                          Using function overloading we can implement any no.of functions with the same name. by providing different prototypes or Arguments, While implement the function overloading we have to check only with function name and with the parameters, not with the return types.

Example on Function Over-loading

Start Microsoft Visual Studio - Click on file- New- Open New Project


Name your Project in the given field and press OK button, there you see a form that is in design mode

Add a Button Control on to your form, from ToolBox


Double click on Button here comes the Coding Part


0

Designtime Binding

Category:

Designtime Binding:
                                 In the design time binding the total instructions we can give using design mode to the compiler the design time binding we can classify in to two types

  1. Function Overloading
    • void tsal()
    • void tsal(int s)
    • void tsal(int s,int b)
    • int tsal();
  2. Operator Overloading
    • 10+20
    • str+str
    • obj+obj

0

Polymorphism

Category:

Polymorphism :
                           The polymorphism is the concept of sharing and ability features to the multiple forms, The polymorphism. we can classify into two types

  1. Designtime Binding
  2. Runtime Binding
    • virtual function

0

Random Topics for Intermediate Level

Category:

Oops Concepts

  • Data Encapsulation
  • Data Abstraction
  • Inheritance
  • Polymorphism

0

Getting Started with C# .NET Page 3

Category:

by Emmaneale mendu
The code itself will look very complicated, if you're new to programming. We'll get to it shortly. For now, right click the Program.cs tab at the top, and click Close from the menu that appears:
Now double click the Program.cs file in the Solution Explorer:

When you double click Program.cs, you should see the code reappear. So this code is the programme that will run when anyone starts your application.

Now click the plus symbol next to Properties in the Solution Explorer above. You'll see the following:





The file called AssemblyInfo.cs contains information about your programme. Double click this file to open it up and see the code. Here's just some of it:


The reddish colour text is something you can change. You can add a Title, Description, Copyright, Trademark, etc.

But right click the AssemblyInfo.cs tab at the top, and click Close from the menu. Now, in the Solution Explorer, click the plus symbol next to References:
These are references to code built in to C#. Much later, you'll see how to add your own files to this section.

Before we add some code, let's save the project. We'll do that in the next part below.

Previous Page

0

Getting Started with C# .NET Page 2

Category:

by Emmaneale mendu
So with Visual C# Express open, click File from the menu bar at the top. From the File menu, select New Project:



When you click on New Project, you'll see the following dialogue box appear:


This is where you select the type of project you want to create. If you only have the Express edition of Visual C#, the options are limited. For the rest of this book, we'll be creating Windows Applications. For now, select Console Application. Then click OK.

When you click OK, a new Console Application project will be created for you. Some code should be displayed:



As well as the code, have a look on the right hand side and you'll see the Solution Explorer. This is where all the files for your project are. (If you can't see the Solution Explorer, click View from the C# menu bar at the top. From the View menu, click Solution Explorer.)

  Previous Page                                                                      Next Page

0

Getting Started with C# .NET Page1

Category:

by Emmaneale mendu
What we're going to do first is to create a very simple programme, so that you can see what makes up a C# .NET project. By the end of this chapter, you'll have learnt the following:

  • How to create new projects
  • What the Solution Explorer is
  • The various files that make up of a C# .NET project
  • How to save your work
  • How to run programmes
  • The importance of the Main statement
The simple programme we'll create is called a Console Application. We won't be doing much else with this type of application, as this is a book about Windows Applications. Off we go then!

Open the Visual C# Express software from your programs menu. When you first open C# (pronounced C Sharp), you should see a screen something like this (screenshot is for 2005 version, but 2008 version is the same):



When you're looking at this piece of software for the first time, it can seem hugely complex and daunting. The temptation is to think you're never going to get to grips with something so difficult. But don't worry - after a few lessons things will start to feel familiar, and you won't feel nearly half as intimidated as you do now!


A Simple C# Console Application
A Console Application is one that looks like a DOS window. If you don't know what these are, click your Start menu in the bottom left of your screen. Click on Run. From the dialogue box that appears, type cmd:

Click OK and you'll see a black screen, like this one:



This is the type of window you'll see for our Console Application. When you create your Windows forms, there's a whole lot of code to get used to. But Console Applications start off fairly simple, and you can see which part of the programme is the most important.

0

Architectures

Category:

c-language Arch:
                   

0

Random Topics in C#.Net for Beginners

Category:

Below you see Random topics from C#.Net for Beginners

0

Category:

Asp.Net Interview Question
What is the difference between DataList and Repeater data binding controls?

Answer: The DataList control is similar to the Repeater control. However, it has some additional properties and templates that you can use to display its data in a diverse fashion. The Repeater control does not have any built-in layout or style. We are forced to specify all formatting-related HTML elements and style tags. On the other hand, a DataList control provides more flexibility to display data in a desired layout. It also provides data selection and editing capabilities. How does it do it? Well, in addition to the five templates (Item Template, AlternatingItem Template, Separator Template, Header Template, Footer Template that a repeater has, the DataList control has two more templates: SelectedItemTemplate, and EditItemTemplate. These templates are useful for allowing data selection and data editing functionalities.

Furthermore, the RepeatDirection and RepeatColumns properties of a DataList control can be exploited to lay out the data in horizontal or vertical fashions.