VB.NET Interview Questions and Answers (91) - Page 4

How to get only Month from date-part?

With the help of Month in-built function,we can get Month part from date-part.

It also returns Integer Value.

For Example:-


It will return current month in number
i.e. 12 as 12 is a December.
How to get Month Name from Given Date?

We use MonthName in-built function to get Month Name.It returns string as Januarary,February and so on.

For Example:-

Dim month_name As String = MonthName(Month(Now()))

Output would be - December
How to Type cast controls?

With the help of CType method,we can cast any controls or any values.

For Example:-

Dim lbl_employee_id As Label = CType(e.Row.FindControl("lbl_employee_id"), Label)

Dim value As Integer = CType(12.05 * 12.05, Single)

What is an alternate way of Casting controls other than CType in VB.Net?

DirectCast is an an alternate way of Casting controls other than CType in VB.Net.
DirectCast is same as CType.

For Example:-

Dim lbl_employee_name As Label = DirectCast(e.Row.FindControl("lbl_employee_name"), Label)

What is the output of below VB.Net code? MessageBox.Show(Math.Sin(90))

Output would be 0.893996663600558.
As Math is an in-built VB.Net class and Sin is just static method of Mat class.Sin is a Trigonometry sign.
What is the output of below code? Dim dt As Date = DateAdd(DateInterval.Day,1,Date.Now())

It will show Tomorrow' date as 12/25/2013 2:02:08 PM as today id 24th December.

DateAdd as the name implies,Adds date prior or later based on the format and DateInterval is an Enum that has Day,Hour,Minute and so on. values.
What is the output of dt variable in below code? Dim dt As Date = DateAdd(DateInterval.Year,1,Date.Now())

Output would be 12/24/2014 2:33:08 PM.

As we have supplied DateInterval.Year Enum value,so it will add 1 year in current year,that will be 2014.
And also as Date.Now() means current date and time.
What is the output of below code? Dim dt As Date = DateTime.Now.AddDays(-1)

It will show Yesterday date.
Suppose today is 24 December 2013,then Adddays(-1) will subtract Days from Current date as AddDays method as name supplies will add/subtract days based on the number supplied in parameter.
What will the output of value? Dim value As Decimal = DirectCast(987.67, Decimal)

It will throw a compile-time error saying that

Value of type 'Double' cannot be converted to 'Decimal'.

DirectCast is used for type-cast conversion in VB.Net.
How to get Dropdown list Selected Item Text?

We can get Dropdown list SelectedText in 2 ways:-

There is a Dropdown list SelectedItem property by which we can get Dropdown list selected item text.

For Example:-

Dim text1 As String = DropDownList1.SelectedItem.Text

Dim text2 As String = DropDownList1.Text

How to check whether string contains values or not?

We can check with Contains method of string.It returns true if text is there in a given string otherwise returns false.

we can check Contains method as:

Dim str As String = "Dot Net Funda"

If (str.Contains("Dot Net")) Then
End If

Output would be "True" because Dot net is present in a supplied string.
How to check whether Generic List contains items or Not or what is the use of generic List Contains method?

With Contains method,we can check.It returns true if item is there in a List otherwise returns false.It's same as String Contains method which checks for particular Texts or Words in a string.So in case of List Contains,it does the same things.Instead of String,it checks items inside a List.


If lst.Contains("any items") Then

'perform any action
End If

Where lst is my Generic List object.
What will be the output of is_contains variable? Dim is_contains As Boolean = False Dim lst_courses As New List(Of String) lst_courses.Add("C#") lst_courses.Add("VB.Net") lst_courses.Add("ASP.Net") is_contains = IIf(lst_courses.Contains("C#"), True, False)

It will return True.As contains method returns True if it finds value inside List otherwise False.
Here,lst_course have C# item.And Contains method is checking whether C# is there in a List.So it will find C# inside lst_courses.So it will return True.
So the value of is_contains will be True.
What are the option statements that are supported in vb.net?

Option explicit:
It is by default ON for vb.net programming.
By setting the option explicit ON, avoids implicitly declaring variables and hence writing better code. This forces the developers to explicitly declare the variables using the Dim keyword and specifying the data type.

Option compare:
By default, the option compare is bit.
However we can change it to text if needed.

Option strict:
It is by default set to OFF mode. Its preferable to write the code by setting the Option strict mode ON, as it prevents implicit type conversions by the compiler which is preferable to develop the code.
What is an alternative of Convert.ToString() in VB.Net?

Use CSTR in-build vb.net function which works same as Convert.ToString().

For Example:

string value = CSTR(ds.Tables(0).Rows(0)("Name"));

string value = Convert.ToString(ds.Tables(0).Rows(0)("Name"));

Both expressions are same.
What is an alternative of Convert.ToInt32 in VB.Net?

We use CINT in-built VB.Net functionto convert values into Integer.It works exactly as Convert.ToInt32 function.

int number = Convert.ToInt32("1");

int number = CINT("1");

Both expressions are same.
How to write property in VB.Net?

Dim _first_name As String

Public Property First_Name() As String
Return _first_name
End Get
Set(ByVal value As String)
_first_name = value
End Set
End Property

For assigning value to First_Name property as
First_Name = "Rajesh" - Set Property

to get the value from First_Name
Dim emp_first_name As String = First_Name - Get Property
How to write only Readonly property in VB.Net?

In Vb.Net,we have to write ReadOnly keyword in property definition as

Public ReadOnly Property Age() As Integer

Return 30
End Get
End Property

What is an alternate way of showing Messages in VB.Net?

We have also MsgBox in-built dot net method,which provides the same functionality as MessageBox.Show does.

For Example:

What is the best way to rename a file on the webserver through Code?

Just write below code:-
first import System.IO namespace as:-

Import System.IO

File.Move("C:\file_name1", "C:\file_name2")

Where, file_name2 is renamed to file_name1.
Found this useful, bookmark this page to the blog or social networking websites. Page copy protected against web site content infringement by Copyscape

 Interview Questions and Answers Categories