If you have got URL of images and you want to download that files to your harddisk from internet then this function will help u:
NOTE: remember that your URL must contain protocol name like "http://"
eg: "http://www.mywebsite.com/aa.jpg"
My.Computer.Network.DownloadFile(Url, "c:\myfolder" & "\" & Url.Substring(url.LastIndexOf("/") + 1))
in above code "Url" is your file Url on internet
and second parameter is where you want to save file.. in above sample code it will get the file name from Url and save it in C:\myfolder with the same name as it was on internet
Download any file or image from internet
Labels: C#.NET and VB.NET Interview Questions , VB.NET
Download any file or image from internet
If you have got URL of images and you want to download that files to your harddisk from internet then this function will help u:
NOTE: remember that your URL must contain protocol name like "http://"
eg: "http://www.mywebsite.com/aa.jpg"
My.Computer.Network.DownloadFile(Url, "c:\myfolder" & "\" & Url.Substring(url.LastIndexOf("/") + 1))
in above code "Url" is your file Url on internet
and second parameter is where you want to save file.. in above sample code it will get the file name from Url and save it in C:\myfolder with the same name as it was on internet
Labels: C#.NET and VB.NET Interview Questions , VB.NET
How to use controls in function used by Thread
When ever we tries to use any control from thread (function used by thread) then we will get following error:
Cross-thread operation not valid: Control 'ControlName' accessed from a thread other than the thread it was created on.
Solution:
For SETTING PROPERTY VALUE for any control below code will help u out from error:
Delegate Sub SetControlValueCallback(ByVal oControl As Control, ByVal propName As String, ByVal propValue As Object)
Private Sub SetControlPropertyValue(ByVal oControl As Control, ByVal propName As String, ByVal propValue As Object)
If oControl.InvokeRequired Then
Dim d As New SetControlValueCallback(AddressOf SetControlPropertyValue)
oControl.Invoke(d, New Object() {oControl, propName, propValue})
Else
Dim t As Type = oControl.[GetType]()
Dim props As PropertyInfo() = t.GetProperties()
For Each p As PropertyInfo In props
If p.Name.ToUpper() = propName.ToUpper() Then
p.SetValue(oControl, propValue, Nothing)
End If
Next
End If
End Sub
call above code as:
to set value in lable:
SetControlPropertyValue(lablel1, "Text", "Hello")
To set value in progressbar:
SetControlPropertyValue(ProgressBar1, "value", i)
For SETTING FUNCTION VALUE for any control below code will help u out from error:
example for listbox:
Private Delegate Sub stringDelegate(ByVal s As String)
Private Sub AddItem(ByVal s As String)
If ListBox1.InvokeRequired Then
Dim sd As New stringDelegate(AddressOf AddItem)
Me.Invoke(sd, New Object() {s})
Else
ListBox1.Items.Add(s)
End If
End Sub
call above code by calling "AddItem()"
Labels: C#.NET and VB.NET Interview Questions , Threading , VB.NET
How to use controls in function used by Thread
When ever we tries to use any control from thread (function used by thread) then we will get following error:
Cross-thread operation not valid: Control 'ControlName' accessed from a thread other than the thread it was created on.
Solution:
For SETTING PROPERTY VALUE for any control below code will help u out from error:
Delegate Sub SetControlValueCallback(ByVal oControl As Control, ByVal propName As String, ByVal propValue As Object)
Private Sub SetControlPropertyValue(ByVal oControl As Control, ByVal propName As String, ByVal propValue As Object)
If oControl.InvokeRequired Then
Dim d As New SetControlValueCallback(AddressOf SetControlPropertyValue)
oControl.Invoke(d, New Object() {oControl, propName, propValue})
Else
Dim t As Type = oControl.[GetType]()
Dim props As PropertyInfo() = t.GetProperties()
For Each p As PropertyInfo In props
If p.Name.ToUpper() = propName.ToUpper() Then
p.SetValue(oControl, propValue, Nothing)
End If
Next
End If
End Sub
call above code as:
to set value in lable:
SetControlPropertyValue(lablel1, "Text", "Hello")
To set value in progressbar:
SetControlPropertyValue(ProgressBar1, "value", i)
For SETTING FUNCTION VALUE for any control below code will help u out from error:
example for listbox:
Private Delegate Sub stringDelegate(ByVal s As String)
Private Sub AddItem(ByVal s As String)
If ListBox1.InvokeRequired Then
Dim sd As New stringDelegate(AddressOf AddItem)
Me.Invoke(sd, New Object() {s})
Else
ListBox1.Items.Add(s)
End If
End Sub
call above code by calling "AddItem()"
Labels: C#.NET and VB.NET Interview Questions , Threading , VB.NET
Cross-thread operation not valid: Control 'ListBox1' accessed from a thread other than the thread it was created on
This error comes when you tries to add items in list box (or any other control) in thread (or from the function which is being used by thread).
Below is solution:
Private Delegate Sub stringDelegate(ByVal s As String)
Private Sub AddItem(ByVal s As String)
If ListBox1.InvokeRequired Then
Dim sd As New stringDelegate(AddressOf AddItem)
Me.Invoke(sd, New Object() {s})
Else
ListBox1.Items.Add(s)
End If
End Sub
now just pass your value in "AddItem()" and it will be added in listbox.
Labels: C#.NET and VB.NET Interview Questions , Threading , VB.NET
Cross-thread operation not valid: Control 'ListBox1' accessed from a thread other than the thread it was created on
This error comes when you tries to add items in list box (or any other control) in thread (or from the function which is being used by thread).
Below is solution:
Private Delegate Sub stringDelegate(ByVal s As String)
Private Sub AddItem(ByVal s As String)
If ListBox1.InvokeRequired Then
Dim sd As New stringDelegate(AddressOf AddItem)
Me.Invoke(sd, New Object() {s})
Else
ListBox1.Items.Add(s)
End If
End Sub
now just pass your value in "AddItem()" and it will be added in listbox.
Labels: C#.NET and VB.NET Interview Questions , Threading , VB.NET
Why constructor not returns value
What actually happens with the constructor is that the runtime uses type data generated by the compiler to determine how much space is needed to store an object instance in memory, be it on the stack or on the heap. This space includes all members variables and the vtbl. After this space is allocated, the constructor is called as an internal part of the instantiation and initialization process to initialize the contents of the fields. Then, when the constructor exits, the runtime returns the newly-created instance. So the reason the constructor doesn't return a value is because it's not called directly by your code, it's called by the memory allocation and object initialization code in the runtime. Its return value (if it actually has one when compiled down to machine code) is opaque to the user - therefore, you can't specify it.
Why constructor not returns value
What actually happens with the constructor is that the runtime uses type data generated by the compiler to determine how much space is needed to store an object instance in memory, be it on the stack or on the heap. This space includes all members variables and the vtbl. After this space is allocated, the constructor is called as an internal part of the instantiation and initialization process to initialize the contents of the fields. Then, when the constructor exits, the runtime returns the newly-created instance. So the reason the constructor doesn't return a value is because it's not called directly by your code, it's called by the memory allocation and object initialization code in the runtime. Its return value (if it actually has one when compiled down to machine code) is opaque to the user - therefore, you can't specify it.
Get Current System Date Format
To get short date pattern of your system:
messagebox.show(System.Globalization.CultureInfo.CurrentCulture
.DateTimeFormat.ShortDatePattern())
To get long date pattern of your system:
messagebox.show(System.Globalization.CultureInfo.CurrentCulture.
DateTimeFormat.LongDatePattern())
Labels: C#.NET , C#.NET and VB.NET Interview Questions , datetime , VB.NET
Get Current System Date Format
To get short date pattern of your system:
messagebox.show(System.Globalization.CultureInfo.CurrentCulture
.DateTimeFormat.ShortDatePattern())
To get long date pattern of your system:
messagebox.show(System.Globalization.CultureInfo.CurrentCulture.
DateTimeFormat.LongDatePattern())
Labels: C#.NET , C#.NET and VB.NET Interview Questions , datetime , VB.NET
Restricting numeric entries only in a DataGridView column
At times we have a requirement where we need to restrict the user to enter only numbers in a column of a DataGridView to achieve this we need to use EditingControlShowing event of DataGridView, this event is new in .Net framework 2.0 and it occurs when a control for editing a cell is showing. Following sample shows a implementation of it:
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if ((int)(((System.Windows.Forms.DataGridView)(sender)).CurrentCell.ColumnIndex) == 1)
{
e.Control.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.TextboxNumeric_KeyPress);
}
}
private void TextboxNumeric_KeyPress(object sender, KeyPressEventArgs e)
{
Boolean nonNumberEntered;
nonNumberEntered = true;
if ((e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == 8)
{
nonNumberEntered = false ;
}
if (nonNumberEntered == true)
{
// Stop the character from being entered into the control since it is non-numerical.
e.Handled = true ;
}
else
{
e.Handled = false;
}
}
Labels: C#.NET , C#.NET and VB.NET Interview Questions , GridView
Restricting numeric entries only in a DataGridView column
At times we have a requirement where we need to restrict the user to enter only numbers in a column of a DataGridView to achieve this we need to use EditingControlShowing event of DataGridView, this event is new in .Net framework 2.0 and it occurs when a control for editing a cell is showing. Following sample shows a implementation of it:
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if ((int)(((System.Windows.Forms.DataGridView)(sender)).CurrentCell.ColumnIndex) == 1)
{
e.Control.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.TextboxNumeric_KeyPress);
}
}
private void TextboxNumeric_KeyPress(object sender, KeyPressEventArgs e)
{
Boolean nonNumberEntered;
nonNumberEntered = true;
if ((e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == 8)
{
nonNumberEntered = false ;
}
if (nonNumberEntered == true)
{
// Stop the character from being entered into the control since it is non-numerical.
e.Handled = true ;
}
else
{
e.Handled = false;
}
}
Labels: C#.NET , C#.NET and VB.NET Interview Questions , GridView
Static Class
Static classes are used when a class provides functionality that is not specific to any unique instance. Here are the features of static classes in C# 2.0.
Static classes can not be instantiated.
Static classes are sealed so they can not be inherited.
Only static members are allowed.
Static classes can only have static constructor to initialize static members.
Advantages
Compiler makes sure that no instance of static class is created. In previous version of C#, the constructor has to be marked private to avoid this from happening.
Also compiler makes sure that no instance members are declared within a static class.
Sample:
Public static class MyStaticClass
{
Private static int _staticVariable;
Public static int staticVariable;
{
Get
{
Return _staticVariable;
}
Set
{
_staticVariable = value;
}
}
Public static void Function()
{
}
}
--------------***********************----------------------------------------
A static class is defined as a class that contains only static members (of course besides the instance members inherited from System.Object and possibly a private constructor). Some languages provide built-in support for static classes. In C# 2.0, when a class is declared to be static, it is sealed, abstract, and no instance members can be overridden or declared.
public static class File {
...
}
If your language does not have built-in support for static classes, you can declare such classes manually as in the following C++ example:
public class File abstract sealed {
...
}
Static classes are a compromise between pure object-oriented design and simplicity. They are commonly used to provide shortcuts to other operations (such as System.IO.File), or functionality for which a full object-oriented wrapper is unwarranted (such as System.Environment).
DO use static classes sparingly.
Static classes should be used only as supporting classes for the object-oriented core of the framework.
DO NOT treat static classes as a miscellaneous bucket.
There should be a clear charter for the class.
DO NOT declare or override instance members in static classes.
DO declare static classes as sealed, abstract, and add a private instance constructor, if your programming language does not have built-in support for static classes.
Example of Static Class in C#.net
using System;
static class MathFunction {
// Return the reciprocal of a value.
static public double reciprocal(double num) {
return 1/num;
}
// Return the fractional part of a value.
static public double fracPart(double num) {
return num - (int) num;
}
// Return true if num is even.
static public bool isEven(double num) {
return (num % 2) == 0 ? true : false;
}
// Return true of num is odd.
static public bool isOdd(double num) {
return !isEven(num);
}
}
class MainClass {
public static void Main() {
Console.WriteLine("Reciprocal of 5 is " +
MathFunction.reciprocal(5.0));
Console.WriteLine("Fractional part of 4.234 is " +
MathFunction.fracPart(4.234));
if(MathFunction.isEven(10))
Console.WriteLine("10 is even.");
if(MathFunction.isOdd(5))
Console.WriteLine("5 is odd.");
// The following attempt to create an instance of
// MathFunction will cause an error.
// MathFunction ob = new MathFunction(); // Wrong!
}
}
Static Class
Static classes are used when a class provides functionality that is not specific to any unique instance. Here are the features of static classes in C# 2.0.
Static classes can not be instantiated.
Static classes are sealed so they can not be inherited.
Only static members are allowed.
Static classes can only have static constructor to initialize static members.
Advantages
Compiler makes sure that no instance of static class is created. In previous version of C#, the constructor has to be marked private to avoid this from happening.
Also compiler makes sure that no instance members are declared within a static class.
Sample:
Public static class MyStaticClass
{
Private static int _staticVariable;
Public static int staticVariable;
{
Get
{
Return _staticVariable;
}
Set
{
_staticVariable = value;
}
}
Public static void Function()
{
}
}
--------------***********************----------------------------------------
A static class is defined as a class that contains only static members (of course besides the instance members inherited from System.Object and possibly a private constructor). Some languages provide built-in support for static classes. In C# 2.0, when a class is declared to be static, it is sealed, abstract, and no instance members can be overridden or declared.
public static class File {
...
}
If your language does not have built-in support for static classes, you can declare such classes manually as in the following C++ example:
public class File abstract sealed {
...
}
Static classes are a compromise between pure object-oriented design and simplicity. They are commonly used to provide shortcuts to other operations (such as System.IO.File), or functionality for which a full object-oriented wrapper is unwarranted (such as System.Environment).
DO use static classes sparingly.
Static classes should be used only as supporting classes for the object-oriented core of the framework.
DO NOT treat static classes as a miscellaneous bucket.
There should be a clear charter for the class.
DO NOT declare or override instance members in static classes.
DO declare static classes as sealed, abstract, and add a private instance constructor, if your programming language does not have built-in support for static classes.
Example of Static Class in C#.net
using System;
static class MathFunction {
// Return the reciprocal of a value.
static public double reciprocal(double num) {
return 1/num;
}
// Return the fractional part of a value.
static public double fracPart(double num) {
return num - (int) num;
}
// Return true if num is even.
static public bool isEven(double num) {
return (num % 2) == 0 ? true : false;
}
// Return true of num is odd.
static public bool isOdd(double num) {
return !isEven(num);
}
}
class MainClass {
public static void Main() {
Console.WriteLine("Reciprocal of 5 is " +
MathFunction.reciprocal(5.0));
Console.WriteLine("Fractional part of 4.234 is " +
MathFunction.fracPart(4.234));
if(MathFunction.isEven(10))
Console.WriteLine("10 is even.");
if(MathFunction.isOdd(5))
Console.WriteLine("5 is odd.");
// The following attempt to create an instance of
// MathFunction will cause an error.
// MathFunction ob = new MathFunction(); // Wrong!
}
}
Difference between Function Overriding and Function Overloading
Difference between Function Overriding and Function Overloading
Overriding is the example of run-time polymorphism and
Overloading is the example of compile-time polymorphism.
The Compile-Time polymorphism have early binding and
Runtime polymorphism have late binding.
The Compile-Time polymorphism is Static and
Runtime polymorphism is Dynamic.
Overriding
¦The return type must exactly match that of the overridden method (Note:- in some languages we can change Return type in overriding).
¦The access level must not be more restrictive than that of the overridden method.
¦The access level can be less restrictive than that of the overridden method.
¦The overriding method must not throw new or broader checked exceptions than those declared by the overridden method.
¦The overriding method can throw narrower or fewer exceptions. Just because an overridden method “takes risks” doesn’t mean that the overriding subclass’ exception takes the same risks. Bottom line: An overriding method doesn’t have to declare any exceptions that it will never throw, regardless of what the overridden method declares.
¦You cannot override a method marked final.
¦Overriding is only possible through inheritance.
¦If a method can’t be inherited, you cannot override it.
Overloaded method
¦Overloaded methods must change the argument list (you can do change in argument list by 1. changing datatype of parameters, 2. changing sequence of parameters, 3. changing count of parameters).
¦Overloaded methods can change the return type (mins its doesn't make any difference that either you change return type or not. its not matter for overloading)
¦Overloaded methods can change the access modifier.
¦Overloaded methods can declare new or broader checked exceptions.
¦A method can be overloaded in the same class or in a subclass.
NOTE:- all most all object oriented languages supports function overloading and function overriding and some of languages also supports operator overloading like : c#.net , c++ etc and some languages not support operator overloading like: vb.net but no language support operator overriding.
Labels: ASP.NET , ASP.NET Interview Questions , C#.NET and VB.NET Interview Questions , Interview Questions
Difference between Function Overriding and Function Overloading
Difference between Function Overriding and Function Overloading
Overriding is the example of run-time polymorphism and
Overloading is the example of compile-time polymorphism.
The Compile-Time polymorphism have early binding and
Runtime polymorphism have late binding.
The Compile-Time polymorphism is Static and
Runtime polymorphism is Dynamic.
Overriding
¦The return type must exactly match that of the overridden method (Note:- in some languages we can change Return type in overriding).
¦The access level must not be more restrictive than that of the overridden method.
¦The access level can be less restrictive than that of the overridden method.
¦The overriding method must not throw new or broader checked exceptions than those declared by the overridden method.
¦The overriding method can throw narrower or fewer exceptions. Just because an overridden method “takes risks” doesn’t mean that the overriding subclass’ exception takes the same risks. Bottom line: An overriding method doesn’t have to declare any exceptions that it will never throw, regardless of what the overridden method declares.
¦You cannot override a method marked final.
¦Overriding is only possible through inheritance.
¦If a method can’t be inherited, you cannot override it.
Overloaded method
¦Overloaded methods must change the argument list (you can do change in argument list by 1. changing datatype of parameters, 2. changing sequence of parameters, 3. changing count of parameters).
¦Overloaded methods can change the return type (mins its doesn't make any difference that either you change return type or not. its not matter for overloading)
¦Overloaded methods can change the access modifier.
¦Overloaded methods can declare new or broader checked exceptions.
¦A method can be overloaded in the same class or in a subclass.
NOTE:- all most all object oriented languages supports function overloading and function overriding and some of languages also supports operator overloading like : c#.net , c++ etc and some languages not support operator overloading like: vb.net but no language support operator overriding.
Labels: ASP.NET , ASP.NET Interview Questions , C#.NET and VB.NET Interview Questions , Interview Questions