ref & out in C#

This post explains about the ‘ref’ and ‘out’ keywords in C#. In C# we have two types; 1) value types 2) reference types.

Value types variables are passed as copies to the methods and any changes inside a method does not affect the variable. ‘ref’ keyword is used to pass a value type as a reference type.

 

Have look on this code sample and the output.

image

 

Output for the above code

image

 

This is because the value is passed as a copy.

Let’s see hoe to use the ‘ref’ keyword to pass the value types as references in C#.

In order to do this you have to make the change in the methods declaration and as well when you call the method also you have to mention the ‘ref’ keyword.

 

Sample show what exactly you have to do.

image

Output shows

image

 

Now let’s have a look on where to use the ‘out’ keyword. The main purpose of the ‘out’ keyword is that it enables us to create and use uninitialized variables for output. Some ways it can be used to return more than one value.

But ‘out’ keyword is powerful since it uses the local variables to be used in such purposes rather than the traditional use of global variables.

We can accomplish the above task by other ways as well, but still ‘out’ keyword is handy. We can see that the ‘out’ keyword is used in some of the .NET framework methods.

Sample of ‘out’ keyword usage

image

 

Output as follows

image

 

Look that x is local variable and it is initialized in another method. This very handy and powerful concept.

As we can see both of these key words we can assure that ‘ref’ and ‘out’ pass the pointers to the methods with some framework built in restrictions.

And another nice feature of the C# compiler is that it checks whether any methods that initialize the x has been called before accessing it.

If you comment the line myMethod(out x);  and compile the code, it will complain that you are trying to use an unassigned variable.

So it is very safe and nice yet powerful as well.

Blogs Here and There

Based some on going and some few past topics I write this entry here. I summarized some interesting topics that I read from here ant there in the cloud.

As we all know with the launch of Windows 7 (actually windows version 6.1) MS got the sensational hit again which it lost due to the Vista. (personally I don’t find that Vista sucked much but it is really a big fat OS) Another reason that why Vista was not accepted by the users was; MS thought the users are somewhat OK with high security controls in local networks. But really it was not the case. I remember when the launching of Windows 7 there was a blog post saying that Windows  7 will rock, because it is smarter and fool enough. I have another experience with an IT guy complained about the UAC system of Vista. It is really useful indeed and a good security protection. We have this in Windows 7 too, but some people still don’t want to have it. (here most of them complain because they do not know how to stop the UAC – but I recommend you to not to stop that)

Stepping away from the MS and you can see plenty of posts and news around the web about the Oracle’s acquisition of Sun and later part of the Java and mySQL. It was a moment that nobody thought that why Sun can’t stand alone. Of course Sun had the power and the strength to stand alone, but someone from the top rank decided to sell it. After Oracle acquired the Sun first all thought Oracle will bump on the mySQL and bang it. But it won’t happen. There are not any reasonable updates or new releases on any of the Sun’s products after the Oracle acquired Sun. What happened to JavaFX ? It was completely forgotten in the shadow of Silverlight. Now Oracle sue Google on Davlik (a JVM for Andriod). Where the patent rights go? What is the future of the open office ? But still Oracle keeps it competing bob – mySQL alive.

Java is an excellent platform which opened plenty of new trends in modern web standards. No body can deny this, but what is the future. I’m not against Java but the what does the Oracle buggers do? .NET needs a perfect competition.

Apache is funded by the MS in order to get the competition to IIS. (are you shocked ? but it is the truth Apache is funded by MS)

LightSwitch beta 1

Microsoft has introduced a new easy way to create Windows and Web based applications. When we develop applications much of the code we write for CRUD operations and data validation. LightSwitch makes this really easier by providing a very friendly manner.

You can download the LightSwitch beta 1and try it out. Download the LightSwitch Training kit. (Training Kit only provides VB.NET samples ).

LightSwitch is built on the Silverlight out of the browser feature. It has specific data types like phone number, email and others. These types are transparent to the database since a typical RDBMS ( in this case SQL Server) doesn’t have those types. So if you create table with email type in the LightSwitch it makes the SQL data type as varchar and it keeps the data type as email. So this makes LightSwitch to validate the string as email and store it in a typical varchar value in the database.

This works similar to the validation module we have in the ASP.NET where the validation is done through a Javascript code. Adobe Dreamweaver’s Spry also does the same thing.

But LightSwitch has more cool features like guided database relations, screen selections and connecting to external live data sources like bing maps.

Download the module and have fun

Smile

Starting a process from a Windows Service

If you have some knowledge about Windows services and their functionalities then go ahead. Otherwise you may not understand some of the points mentioned here.

Are you suffering from starting a process from your windows service ? I have a cunning solution for that. Normally we cannot start any process either on our local machine or from a remote machine through a windows service.

We can start the process by enabling the Desktop Interactive of the service, but the UAC system of Vista and Windows 7 is a problem, that every time we start the process from a service it asks whether to allow the desktop interactivity. We can’t ask our clients to stop the UAC system. So what is the solution ?

Now I think you have some idea why can’t we start a process from service. But you might have a question in your mind why we need a process for a service ? There can be several answers

  • We need to perform a CPU intensive operation
  • COM interoperability is clashing with some threading components of the service
  • Some actions can be performed more efficiently by a separate process rather than a service

I figured out this solution as I suffered from the bolded point. Here the method I have used.

Create an ASPX webpage (it should be hosted in a later version of IIS 4.0)

In the Page_Load method start the process.

Then Create the Windows Service to access the the web page. (Access it through normal WebRequest and WebResponse). Cool !

The real cool thing is you can almost start all the processes by using this method from a windows service. Even processes with arguments and Verbs.’

Here’s the code for the webpage

protected void Page_Load(object sender, EventArgs e)
{

Process p = new Process();

p.StartInfo.FileName = "path to your process (your .exe file)"

p.StartInfo.UseShellExecute = false;

p.StartInfo.CreateNoWindow = true;

p.Start();

p.WaitForExit();

p.Dispose();

}

 

In the service within the OnStart() put the following code

try

{

HttpWebRequest req = (HttpWebRequest)WebRequest.Create("ASPX page path");

HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

}

catch (Exception ex)

{

}