I will show you how to make your own class to looks like your calling a with keyword in C#. To begin with i will show you what keyword with do in VB.NET. Lest assume we have a class that we make named clsMyClass. So this class have several function to set some string value to variable etc. In VB.NET you can use with operator and you can use this function without writing your class name before each call of the function:
'Class that we make'
Public Class clsMyClass
Private strText As String = String.Empty
Private intTest As Int32 = 0
Private dblTest As Double = 0
Public Function SetText(ByVal Text As String) As clsMyClass
strText = Text
Return Me
End Function
Public Function SetInt(ByVal intVal As Int32) As clsMyClass
intTest = intVal
Return Me
End Function
Public Function SetDbl(ByVal dblVal As Double) As clsMyClass
dblTest = dblVal
Return Me
End Function
Public Overrides Function ToString() As String
Return (("strText value: " & strText) & Environment.NewLine & "intTest value: " & intTest.ToString()) & Environment.NewLine & "dblTest value: " & dblTest.ToString()
End Function
End Class
'Windows form button click that will use this class'
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cMC As clsMyClass = New clsMyClass()
With cMC
.SetText("AAA")
.SetInt(5)
.SetDbl(0.25)
End With
MessageBox.Show(cMC.ToString())
End Sub
So in C# you dont have this thing because if you input 2 withs one in another you dont know the compiler what functions will call. For example if you call with with .ToString() of 2 classes you dont know the compiler what function of witch class will call. However you can make your classes to support this kind of stuff. Its useful if your going to use lots of functions to set different things in order like with the with keyword above. Its very simple in your class make the functions that you are going to use to return the class itself so you can use it again. This is illustrated with .Replace() that return string and you can replace some more characters. In your custom class if your functions return the class itself you will be able to use the other functions:
//'your class'
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApplication1
{
class cWithLike
{
private string strText = string.Empty;
private Int32 intTest = 0;
private double dblTest = 0;
public cWithLike SetText(string Text)
{
strText = Text;
return this;
}
public cWithLike SetInt(Int32 intVal)
{
intTest = intVal;
return this;
}
public cWithLike SetDbl(double dblVal)
{
dblTest = dblVal;
return this;
}
public override string ToString()
{
return "strText value: " + strText + Environment.NewLine +
"intTest value: " + intTest.ToString() + Environment.NewLine +
"dblTest value: " + dblTest.ToString();
}
}
}
//'Form button click to set values'
private void button1_Click(object sender, EventArgs e)
{
cWithLike cWL = new cWithLike();
cWL
.SetText("AAA")
.SetInt(5)
.SetDbl(0.25);
MessageBox.Show(cWL.ToString());
}
As you can see it looks very much like your using with keyword in VB.NET. Hope you enjoy it




MultiQuote






|