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.

Check whether a URL exist

Use this code snippet to check whether a url exist or not .. . . .. .

MSDN Trackback : http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/c34f4f2e-038d-43da-a9f3-fc1ef64c49c7/#ea274ea3-31e8-4d61-a892-9e1bd4b2a97f

image

Getting the Radio Button Value in Windows Forms

I came across a question in the MSDN forums about accessing the Radio Buttons in Windows Forms. After reading the question I realized that radio buttons in Windows Forms are little tricky compared to the HTML or ASPX ones.

We can group the radio buttons using GroupBox control or the Panel Control. Then the set of radio buttons works together. The following code snippet explains on how to access the clicked radio buttons and their value when a button is clicked.

Radio buttons have the Click event and CheckChanged event, which we can use to get the value, but programmatically this snippet explains how to access the radio buttons inside a GroupBox.

image

Calendar in ASP.NET

We all know that .NET provides us rich set tools for us. Calendar control is one of them. We can simply use the calendar control for date input. But it is more powerful and we can simply create a complete application using calendar control which shows the tasks that have been scheduled.

To use the calendar control in a such way we should have to know about, how the calendar control is rendered in the HTML. Calendar control is rendered in as a table in HTML and every day is rendered in the order.

And calendar control provides a DayRender event as well. That’s enough for us to create the application.

Simply drag drop the calendar control inside your ASP.NET page. Make it big enough to fit to the screen.

And put the code behind as described here. I hardcoded the tasks in a HashTable but if you want, you can keep them in a data source.

 

image

 

In the Page_Load we load some data into the HashTable. This scenario may vary based on where you keep your data source. (As I mentioned earlier here it is simply hard coded).

DayRender method is triggered for each day that is rendered for the month. First in the if condition we check whether the rendered date is in the HashTable. Here we are providing the key and check whether value of the key is null or not. If it is not null then the key is available in the HashTable so a task has been assigned on that day.

Then we proceed inside the if condition.

We create a Literal control and make gives the text as <br/> (I don’t need to explain this as we all know why ?) And I add the Literal Control to the specific cell of the calendar control. (because as I explained earlier calendar control is rendered as a table in HTML)

Then I add a Label and feed the task of that particular day. Here DayRenderEventArgs object is very useful in getting the rendered day.

image

The above is simple and yet powerful. 🙂

Silverlight with PHP

I do not really need to tell about the Silverlight and its power, since it is apparent. But at the same time I cannot keep mouth shut with the features that Silverlight offers. I have been working on WPF and Silverlight these days. And Just wondering the power the Silverlight has over the Web Apps. It drives me crazy especially compared to ASP.NET Ajax.

What an effective and an easy way to build RIAs. Wondering about the Silverlight architecture and how the mixture of XAML and the managed code sit on the back of the Silverlight.js. Really Excellent.

Using the Silverlight with ASPX is OK, what about with the other languages. Of course not a big deal. Here I explained how to use Silverlight in your PHP application with the C# managed code.

Create a simple PHP file that can take your name and say Hello !

image

The above PHP script gets post to itself and say Hello.

Now build the Silverlight in VS 2010, when creating the Silverlight application project select the temporary hosting in an HTML file rather than the Web project. This will create an HTML where your Silverlight object is embedded; it would be easy to get the code later.

Put the following in the Layout Grid – XAML Code

image

 

Code for button1_Click event in the MainPage.xaml.cs

 

image

Now we have the PHP file and the Silverlight application, Let’s put them together.

I’m hosting the PHP using the WAMP, Create a folder SilverlightPHP and save the above PHP file as index.php (so it’s easy to access)

Browse your Silverlight application project folder and inside the Bin->Debug there would be a file with the extension of .xap (description says as XACT project file)

Copy that file and put it into the SilverlightPHP folder.

Then open the HTML file in the Silverlight application project folder (it’s also in the Debug folder)

copy the Javascript code and paste it inside the <head> </head> or keep it as separate .js file and link it.

Then copy the Silverlight embedding code from the HTML and paste it inside the body. You can set the size here.

image

Here rather than copying the .xap file to the target folder you can set the value to as the path of the .xap file.

Save the index.php

Then RUN !@# and have FUN.

The real amazing thing is this a very cool way to bring the RIA capability to our normal PHP apps. You can notice that when you click the Say Hello button of the PHP the page reloads but when you click the Say Hello button of Silverlight it is completely an asynchronous call to the Silverlight object model. (great AJAX feature). More than that here we use C#.NET and the PHP in the same web interface.

image

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)

{

}