public knowledge blog_load(sender Virendra)

                                                                                                                          Nothing shocks me, I am a Software Engineer.

Archive for September, 2009

Difference between C# and VB.NET

Posted by Virendra Dugar on September 18, 2009

This question is almost asked in every interview, if you have worked with both the language. There are syntax difference, keyword difference in both the language. But the interviewer is not looking for such answers. One need to explain the functional difference between both the languages.

Here are some key difference in C# and VB.NET other than syntax differences.

  1. In C#, using keyword is used to release unmanaged resources. (Not available in VB.NET)
  2. Optional parameter is supported in VB.NET. (Not available in C# 3.0 or below version).
  3. Structure and unstructured error handling (On Error GoTo) is supported in VB.NET. (Unstructured error handling is not supported in C#).
  4. Event gets bind automatically in VB.Net.
  5. VB.NET is not case sensitive where C# is.
  6. Shadowing: – This is a VB.Net Concept by which you can provide a new implementation for the base class member without overriding the member. You can shadow a base class member in the derived class by using the keyword “Shadows”. The method signature, access level and return type of the shadowed member can be completely different than the base class member.

    Hiding: – This is a C# Concept by which you can provide a new implementation for the base class member without overriding the member. You can hide a base class member in the derived class by using the keyword “new”. The method signature, access level and return type of the hidden member has to be same as the base class member.

    Comparing the two:-
    1) The access level, signature and the return type can only be changed when you are shadowing with VB.NET. Hiding and overriding demands these parameters as same.
    2) The difference lies when you call the derived class object with a base class variable. In class of overriding although you assign a derived class object to base class variable it will call the derived class function. In case of shadowing or hiding the base class function will be called.

  7. Visual Basic .NET can also force parameters to be passed by value, regardless of how they are declared, by enclosing the parameters in extra parentheses. There is no way to achieve this thing in C#.
    For Example:

    Dim y As Integer = 5
    Dim z As Integer
    z = Add(y) //This will set both Y and Z to 6.
    z = Add((y)) //This will set Z to 6 but Value of Y will not be change, as we have included extra parenthese while calling.
    

    The Add function:

    Public Function Add(ByRef x As Integer) As Integer
    x = x + 1
    Return x
    End Function
    

I know this is not a complete list, so if you know any other difference kindly post your comments.

Enjoy..

Posted in C#, VB.NET | Tagged: , | 2 Comments »

Execute Web Service via GET Method

Posted by Virendra Dugar on September 8, 2009

When we created any web service via Vistual Studio, we always get a default method named “Hello world”.

When you hit this URL in your browser, http://localhost/SampleWebService/Service.asmx, it shows list of all the web method. Click on Hello World and then Invoke. It gives following output:

I am assuming that you have hosted your web service in IIS.

<?xml version=”1.0″ encoding=”utf-8″ ?>
<string xmlns=”http://tempuri.org/“>Hello World</string>

Looks good. Now let’s go and modify our service.cs file and add a new method which takes one parameter.

[WebMethod]
public string HelloWorldWithParam(int a)
{
return "Hello World " + a.ToString();
}

Now, hit this URL http://localhost/SampleWebService/Service.asmx. It is showing both the methods.

WebService

Click on HelloWorldWithParam. It asks for parameter value. Without giving any value just click invoke.

WebService1

It shows following error:

Error1

Above error is coming as method is expecting a value, and method tries to convert null value to string. If you refresh your browser then following error comes:

Error

Now let’s provide the parameter value via URL only. Hit this URL, http://localhost/SampleWebService/Service.asmx/HelloWorldWithParam?a=1 , and then also you will receive the above error.

Reason: By default, Web service created via Visual Studio executes through HTTP Post method. When Post method is used, querystring is not visible. We need to configure our web service, so it works with HTTP GET and POST method. How can we do this?

Go to web.config and add this code in system.web section.

<webServices>
      <protocols>
        <add name=”HttpGet”/>
        <add name=”HttpPost”/>
      </protocols>
</webServices>

Good to go now. Hit this URL again and this time you will see “Hello World 1”.

Enjoy….

Posted in ASP.NET | Tagged: | 1 Comment »

 
%d bloggers like this: