473,396 Members | 1,970 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Passing new class instance to method

Hi I am trying to access my XmlTextWriter class within a called method, but
am unable to do this. I have initiated the new class within the main method

static void Main
{
XmlTextWriter tw = new XmlTextWriter(fileName,null);
//lots more code including xml text writing calls
// i then call my method
MyMethod(text);
}

public static void MyMethod(string text)
{
tw.WriteStartElement("stuff goes here ect"); //this does not work
//more xml write stuff
}

How do i use my tw object everywhere?
Thanks for your help

Nov 17 '05 #1
1 1287
Matt wrote:
Hi I am trying to access my XmlTextWriter class within a called method, but am unable to do this. I have initiated the new class within the main method
static void Main
{
XmlTextWriter tw = new XmlTextWriter(fileName,null);
//lots more code including xml text writing calls
// i then call my method
MyMethod(text);
}
So you declared tw inside a static method; whenever you leave that method,
tw is out of scope.

public static void MyMethod(string text)
{
tw.WriteStartElement("stuff goes here ect"); //this does not work
Certainly, since tw is not visible from here.
//more xml write stuff
}

How do i use my tw object everywhere?


Declare tw as a static variable of your class (outside any method):
<code>

class YourClass
{
private static XmlTextWriter tw;

// and use it in your Main() method:

static void Main()
{
// some stuff
tw = new XmlTextWriter(fileName,null);
//...
</code>

So tw will be visible in every method of YourClass.


Nov 17 '05 #2

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

Similar topics

11
by: Arsen Vladimirskiy | last post by:
Hello, If I have a few simple classes to represent Entities such as Customers and Orders. What is the proper way to pass information to the Data Access Layer? 1) Pass the actual ENTITY to...
8
by: Dennis Myrén | last post by:
I have these tiny classes, implementing an interface through which their method Render ( CosWriter writer ) ; is called. Given a specific context, there are potentially a lot of such objects,...
2
by: lolomgwtf | last post by:
I have a managed C++ method that wraps unmanaged code and creates a managed object holding data retrieved form an unmanged one. I want create an instance of this managed class in C#, pass it to...
6
by: Max | last post by:
Last time I tried to explain this on another forum it didn't go too well, so I'll try my best and if you know what I'm talking about then please tell me how to do this. I have a class, inside I...
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.