473,503 Members | 939 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# Object Creation General Question

I have a general question regarding this piece of code that I wrote. This
works fine, however I am confused as to how I am able to pull the sb instance
out at the end? When I create the XmlTextWriter class and pass it in sw
(which in turn sw was created with sb) how is the sb object being maintained
and populated via the writer instance? My initial thought was that only
writer would contain the string....but to my surprise the sb instance
contained the string? how, why? How is sb being built? My goal is to
understand this concept so I can develop similar functionality in my classes.
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
XmlTextWriter writer = new XmlTextWriter(sw);

writer.Formatting = Formatting.Indented;
writer.Indentation = 5;

writer.WriteStartDocument();
writer.WriteComment("XML Validation Request " + System.DateTime.Now);
writer.WriteStartElement("XmlValidationTicket");
writer.WriteStartElement("Request");
writer.WriteCData(fragment);
writer.WriteEndElement();
writer.WriteElementString("Status", status);
writer.WriteElementString("ErrorMessage", errorMsg);
writer.WriteEndElement();
writer.Close();

return sb.ToString();
Nov 16 '05 #1
1 1168
Hi Chris,

This isn't a very good explanation, but maybe it can shed some light.
writer transfers data to an object of your choice, so when you write to
it, it is in fact passing the data to the StringWriter, which in turn
passes it on to a StringBuilder.

sb is created in the first line, a reference to it is held by sw and a
reference to sw is held by writer.

public class A
{
public string Text;
}

public class B
{
private A a;
public B(A a)
{
this.a = a;
}

public void Write(string s)
{
a.Text += s;
}
}

public class C
{
private B b;
public C(B b)
{
this.b = b;
}

public WriteStuff(string s)
{
b.Write(s);
}
}

....

A a = new A();
B b = new B(a);
C c = new C(b);
c.WriteStuff("Hello World");

//a.Text == "Hello World"

The code is untested so there might be typos.

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
5636
by: Richard | last post by:
Hi all, I am looking for some help on understanding the overhead associated with object creation in Java. I am writing an application where I have written a class to encapsulate some text...
44
2397
by: Steven T. Hatton | last post by:
This may seem like such a simple question, I should be embarrassed to ask it. The FAQ says an object is "A region of storage with associated semantics." OK, what exactly is meant by "associated...
6
1187
by: Charles Law | last post by:
As a matter of practice, where would people put the following elements of object creation/initialisation: Create shared member objects Initialise shared member objects Create non-shared member...
15
1875
by: Sam Kong | last post by:
Hello! I got recently intrigued with JavaScript's prototype-based object-orientation. However, I still don't understand the mechanism clearly. What's the difference between the following...
4
2102
by: alex | last post by:
I am so confused with these three concept,who can explained it?thanks so much? e.g. var f= new Function("x", "y", "return x * y"); function f(x,y){ return x*y } var f=function(x,y){
0
7203
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7339
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5581
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5017
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4678
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3157
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1515
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
389
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.