Page 1 of 1

With Like Class In C#

#1 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 464
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Posted 22 February 2011 - 04:55 PM

Hello :)
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 :)

Is This A Good Question/Topic? 0
  • +

Replies To: With Like Class In C#

#2 scalt  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 63
  • View blog
  • Posts: 342
  • Joined: 22-November 07

Posted 02 March 2011 - 07:42 PM

I'm not sure I like this idea. In the standard .NET libraries if you come across methods that return their parent class (such as string.Replace()) it is generally assumed (correctly) that the original object is not modified, instead it is copied within the method which then modifies and returns the copy, ie
string str_a = "hello";
string str_b = str_a.Replace("el", "a");

MessageBox.Show("Original - " + str_a + "  Modified - " + str_B)/>;



str_a is never modified, merely copied.

While it is technically possible to do what you have done in the C# language I think you will find a lot of coders disagree with it because what is going on behind the scenes is not what your method signatures imply.
Was This Post Helpful? 2
  • +
  • -

#3 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 464
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Posted 07 March 2011 - 03:40 AM

thats true replace do return a new string not the old that is modified. however the idea of this tutorial is to easy populate members if your going to use it a lot(populating several member at not specific order in your code) without typing the class before calling each method.
Was This Post Helpful? 0
  • +
  • -

#4 Jeff H  Icon User is offline

  • D.I.C Regular

Reputation: 112
  • View blog
  • Posts: 307
  • Joined: 30-January 11

Posted 17 April 2011 - 02:51 AM

strings are immutable you cannot modify them you will always get a copy.


a stringbuilder is like a string but mutable
Was This Post Helpful? 0
  • +
  • -

#5 here.to.code  Icon User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 55
  • Joined: 15-February 11

Posted 06 May 2011 - 11:24 AM

Why can't you just doing this? That's what I generally do with what you seem to be doing.

Panel p = new Panel()
{
Name = "this",
Text = "that"
};
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1