The error occurs when an attempt is made to open a file that is not available
The following statement
stream = File.Open("data.xml", FileMode.Open);
will throw an error if data.xml is not available
Wednesday, March 31, 2010
Program '..Sample.exe' has more than one entry point defined: '..Program.Main()'. Compile with /main to specify the type that contains the entry poin
The type or namespace name 'SoapFormatter' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'Soap' does not exist in the namespace 'System.Runtime.Serialization.Formatters' (are you missing an assembly reference?)
The type or namespace name 'SoapFormatter' could not be found (are you missing a using directive or an assembly reference?)
Add the following reference to the project to solve the error
The type or namespace name 'SoapFormatter' could not be found (are you missing a using directive or an assembly reference?)
Add the following reference to the project to solve the error
Tuesday, March 30, 2010
Accessors and mutators in C#
Accessors and mutators do the job of retrieving and storing the data to a class member. The class member will be usually private and hence cannot be accessed directly by the object.
The following code provides a hint of the same
Here _PlayerType is declared as private. This can be however accessed in the main program as shown below:
The statement CT.PlayerType = ClassTeam.playerType.Contract; Calls the Mutator (set property) and sets the value and CT.PlayerType.ToString() uses Get property and retrieves the value
The following code provides a hint of the same
public enum playerType { ClassA, ClassB, ClassC, Contract }
private playerType _PlayerType;
string Name;
string Age;
string Phone;
#endregion
#region properties
public playerType PlayerType
{
//accessor
get { return _PlayerType; }
//mutator
set
{
_PlayerType = value;
}
}
Here _PlayerType is declared as private. This can be however accessed in the main program as shown below:
ClassTeam CT = new ClassTeam();
CT.PlayerType = ClassTeam.playerType.Contract; //Calls the Mutator
MessageBox.Show(CT.PlayerType.ToString()); // Calls the Accessor
The statement CT.PlayerType = ClassTeam.playerType.Contract; Calls the Mutator (set property) and sets the value and CT.PlayerType.ToString() uses Get property and retrieves the value
System.StackOverflowException was unhandled - Set Property C#
Set Property C# goes to infinite loop
If the set property goes to infinite loop, then there is some problem with accessor and mutator.
The following example is a good (rather bad) for recursive looping of the properties
In order to correct it, declare a private variable and get/set that variable.
If the set property goes to infinite loop, then there is some problem with accessor and mutator.
The following example is a good (rather bad) for recursive looping of the properties
public playerType PlayerType
{
get { return PlayerType; }
set
{
PlayerType = value;
}
}
In order to correct it, declare a private variable and get/set that variable.
private playerType _PlayerType;
public playerType PlayerType
{
get { return _PlayerType; }
set
{
_PlayerType = value;
}
}
Monday, March 29, 2010
Your step-into request resulted in an automatic step-over of a property or operator.
Your step-into request resulted in an automatic step-over of a property or operator.
This behavior can be overridden in the context menu for the line being executed by choosing 'Step Into Specific' or by unchecking the option 'Step over properties and operators'.
Do you want to continue being notified when an automatic step-over happens?
This behavior can be overridden in the context menu for the line being executed by choosing 'Step Into Specific' or by unchecking the option 'Step over properties and operators'.
Do you want to continue being notified when an automatic step-over happens?
Sunday, March 28, 2010
How to effectively use #region in Visual Studio
#region lets you specify a block of code that you can expand or collapse when using the outlining feature of the Visual Studio Code Editor.
The collapse and expand feature can be used for advantage with good declaration of regions
For example:
Would look like the following when collapsed:
Please share your thoughts on effective usage of #region
The collapse and expand feature can be used for advantage with good declaration of regions
For example:
namespace BlogExample_WinAppOne
{
class ClassTeam
{
#region declarations
public enum playerType { ClassA, ClassB, ClassC, Contract }
#endregion
#region properties
public playerType PlayerType
{
get { return PlayerType; }
set
{
PlayerType = value;
}
}
#endregion
#region public methods
public string SetPlayer
{
}
#endregion
}
}
Would look like the following when collapsed:
Please share your thoughts on effective usage of #region
Invalid expression term '>'
Only assignment, call, increment, decrement, and new object expressions can be used as a statement
Invalid expression term '>'
The error is most likely for some VB programmer coding C#. The following snippet shows you why:
Wrong
if (value.Length <> 0)
Correct
if (value.Length != 0)
or
if (value.Length > 0)
VB Programmers tend to use <>:)
Semicolon after method or accessor block is not valid
Semicolon after method or accessor block is not valid error is thrown when the semicolon is not appropriately placed.
For example
should be
You can post your own examples for helping readers
For example
set { PlayerType = value };
should be
set { PlayerType = value; }
You can post your own examples for helping readers
Saturday, March 27, 2010
Enumeration in C#
Here is a simple way to declare and use enumerations
Declaration is by using the enum keyword
And you can use as shown below:
Declaration is by using the enum keyword
public enum playerType { ClassA, ClassB, ClassC, Contract }
And you can use as shown below:
ClassTeam CT = new ClassTeam();
CT.PlayerType = ClassTeam.playerType.Contract;
Partial Classes in C# (.NET)
Why to use Partial Classes?
Partial classes have many advantages. The most visible being the ability to split the class in multiple files. This is quite evident in Windows forms.
Windows forms has the class being split between two files - Form1.cs and FormDesigner.cs
partial class
Form1
{
///
/// Required designer variable.
///
private System.ComponentModel.IContainer components = null;
And
public partial class
Form1 : Form
{
publicForm1()
{
check_whitespace();
InitializeComponent();
}
The same concept can be effectively used in the classes you create also.
A namespace does not directly contain members such as fields or methods
A using clause must precede all other elements defined in the namespace except extern alias declarations
Subscribe to:
Posts (Atom)