Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Wednesday, September 10, 2008

The Output Parameters

Sometimes when you write a function, you may want it to manipulate and return several values rather than one. Whenever I needed to do this I used to return an object.
But there might be a possibility that the returned object has no other purpose than to be used just to return multiple values from a function.
And then I discovered output parameters. Though I haven't really used them. (Just used them to check the functionality, nothing flashy. :) )

With output parameters you define the variables that will be returned. But you don't use the return keyword. With return keyword you can return a single value or object not multiple.
But with output parameters you don't use the return keyword to return multiple values rather you just specify that these variables are output parameters and their values can be used by the calling program and the called program can manipulate their values (in fact, assign them values).
For the above stated purposes you use the keyword out.

Example Code:
Say, you have a class "Class1.cs" (a very innovative name, right?)
In this class you have a function called ProcessNum which takes two integers as input and as output gives their sum and difference. This is how it will work:

public void ProcessNum(int a, int b, out int Sum, out int Diff)
{
Sum = a + b;
Diff = a - b;
}

Calling ProcessNum:

Class1 obj = new Class1();
int S;
int D;
int num1 = 5;
int num2 = 5;
obj.ProcessNum(num1, num2, out S, out D);
Console.WriteLine("Called ProcessNum of Class1 with parameters " +
"a: " + num1 + Environment.NewLine + " b: " + num2 +
Environment.NewLine + "Returned Values:::: " +
Environment.NewLine + "Sum: " + S + Environment.NewLine +
"Diff: " + D);


Source:

Apress Beginning ASP.Net 2.0 in C# 2005
Chapter # 3: Types, Objects and Namespaces
Pg. #: 75

Saturday, June 7, 2008

Changing the shape of Mouse Pointer

I was searching for a way to change the shape of mouse pointer in C#. Say, in any application a button is clicked and the application is performing some functions on the back end, there must be some way through which the user can know that something is working, otherwise the confused user would go on pressing buttons and irritated the user would close the application.
(I know, I know I am not telling you anything new :P)
Anyways, I was searching for a way to do this. I wanted to change the mouse pointer to look like an hourglass to signify "be patient, work is being done, your output will be served to you soon". A simple hourglass to say all that.
First MSDN search returned a page that taught how to change the shape in VB.Net then while checking other articles I found a namespace by the name "System.Windows.Input". But I think it's not a namespace of C# 2005.
Amazingly I found my answer in a question. To change the shape all you have to do is write the following line of code:

Cursor.Current = Cursors.WaitCursor;

As soon as the function ends the cursor turns back to it's original shape.

In case you want to change it back to original shape before the function ends, here's what you'll have to write
Cursor.Current = Cursors.Arrow;

This is where I read it from.

Tuesday, February 5, 2008

Word Net

So here's a somewhat technical post.
See how hard I am trying to write a technical blog?

So, we (a group of 4 people) are currently working on a project, will tell you the details in another post.
During this project, we faced a problem of identifying whether a word falls under the noun category or not. We found the answer immediately. Word Net.

Word Net is basically a database which has classified words into the parts of speech it falls under. There are many other things that Word Net can help you with. Getting Hypernym, for instance.

You can find the Word Net application on it's website:
http://wordnet.princeton.edu/

We downloaded the application from this site, it also gives you the code of word net (after all it's open source)

But the problem was, using word net in our code.
We are working in C#. Word Net is a C++ project.
We tried to call a C++ code from C#.

For this we first tried to get a hang of dll. We made a sample dll in C++ code. Called it through C++ code. Worked perfectly. Yahooooo!!!!
But then we tried to call the same C++ dll through C# code.
Unsuccessful.
Tried hard to get it right.
Surfed through blogs and articles.
Posted questions on the forum.
Got answers too.
But nothing worked.
Yeah, I know we are dumb. :(


But then while trying to get our hands on the Word Net Database. We found a site:
http://sourceforge.net/project/showfiles.php?group_id=135112
which provides the Word Net database in mySQL.

We downloaded it. and yes we thought that we can use it.

Unfortunately we are working on SQL. Not a big problem. We could just as easily have used mySQL. not much difference.
But we always try to do the hardest, most dumbest and senseless thing on earth.
And we continued with the tradition here too.
We decided to convert the database in SQL.


And yeah I did think about downloading a software that could convert mySQL to SQL.
But was only able to find softwares that could convert mySQL database into SQL database. Found none that could do the same with query files.

Then I tried to make a mySQL database with the query files.
again faced some problems.

Now when you download the zip file from the above mentioned site, it provides you with the query files to create and populate database.
On the website the schema is also given.
Using the schema, we created the database on Microsoft SQL Server 2005.
And then we used the query files provided to populate the database. But ofcourse they were in mySQL, so we created a small C# code which reads from .txt file the queries in mySQL, does the necessary manipulation on them and then writes the SQL Queries in a .txt file.

Seems easy enough na?
Well it was easy, but very time consuming. Or maybe I am dumb and couldn't find a less time consuming way.

What I did was I used to open the query file in Firefox. (Wasn't able to open it with Microsoft SQL Server, Internet Explorer or Notepad).
I wanted to populate one table at a time. So I went through the page and found the queries for the table I wanted to populate. Then I would copy paste one line on a note pad. That one line would actually comprise a large number of queries. Then I saved the .txt file and converted into a .txt file with SQL queries through the following C# code:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ConvertMySQLtoSQL
{
class Program
{
static void Main(string[] args)
{
String Path = "Osense.txt";
String TbName = "sense";
StreamReader Reader = new StreamReader(Path);
String Content = Reader.ReadToEnd();
Content = Content.Replace("),", ");\n");


String[] Lines = Content.Split("\n".ToCharArray());
for (int i = 0; i < Lines.Length; i++)
{
Lines[i] = "INSERT INTO " + TbName + " VALUES " + Lines[i];
}


Reader.Close();
String OutputFile = TbName + ".txt";
StreamWriter Writer = new StreamWriter(OutputFile);
for (int i = 0; i < Lines.Length; i++)
Writer.Write(Lines[i] + Environment.NewLine);
Writer.Close();

}
}
}