Click to See Complete Forum and Search --> : public variable not accessible, why??
LiteUponLite
July 20th, 2005, 08:19 AM
i've a public variable declared inside the class (ArrayList arr) and when i try to access it within the methods of the class (like Parser) it gives me an error, why?? like if i've declared it public i shud be able to access it, i've done this before too. Also how wud i access this variable in any other class, do i need to create accessor methods in this class for that. Below is the structure of the class:
using System;
using System.Collections;
using System.Text.RegularExpressions;
namespace HL7Convert
{
class HL7Convert
{
public ArrayList arr = new ArrayList();
public static String Parser (String fileNum, String fileName)
}
}
darwen
July 20th, 2005, 08:31 AM
Your method is static, and your variable is not. To get it to work make the variable static.
Unless you explicitly want the array list to be cached between calls to Parse I wouldn't make the member static if I were you.
What is your array for ? If you need member variables you should be working with an instance of the class and not calling static functions.
You shouldn't ever have public variables either : they should be private and you should have a property which gives access to them.
Darwen.
LiteUponLite
July 20th, 2005, 08:49 AM
i tried changing my function to non-static still it didn't work...this is where i need the arrayList:
in this HL7Convert1 class the Parser fills up the arraylist then, in another class i want to call Parser function and also be able to access the contents of the arrayList arr....you mentioned i should declare the arraylist private and then have what kind of properties to acces it from other class??
exterminator
July 20th, 2005, 09:11 AM
Properties - Get and Set. Search in google for these and how to implement them in C#. Its quite simple.
For that purpose why do you need static members?
Remember - Static methods can only access other static members of the class. Read up more about the static members of a class (there's also a static constructor)
Now to your actual issue.
1. Make the ArrayList member as private.
2. Write a Get property for this ArrayList.
3. In the parse public method, add the element into the private arraylist.
Make an object of the Class of which this arraylist is a member and then add whatever u need to into the arraylist using the parse method and then when u need to get the whole arraylist, use the Get property you have defined for your class. This is how you should go ahead.
What do you mean by - being able to access the members from other class ??? What are you trying to achieve? I am not at all sure.
But if you want to make the arraylist as static (read up on static, this is something you would rarely want to do) make the parse method also as static along with the getter as static. This way you would not need to make an object of class HL7Convert but this is really, really unappreciable. You would only need to write the class name and then access the members as HL7Convert.Parse(...,...) and HL7Convert.GetArrayList(). :thumb:
LiteUponLite
July 20th, 2005, 09:23 AM
hi, thnxs for ur post i'll try and let u noe if it worked and rate ur post...but i dont' think i ever said being able to access the members from other class ???
i just said i wanted to access the arraylist from another class....also i wanted the method to be static cuz my friends who'll use the code dont' want to make an object of the class (to save memory)...and since i'm new to c# i can definitely read up on static stuff, but i think the underlying difference is you dont' 've to create an object of a class, if the function in it is declared static.
exterminator
July 20th, 2005, 09:38 AM
So as I said ... static functions can only access the static data members of the class. Make your arraylist static too. And then you could do what you wish to and that is accessing the public variable .. which now would be called public static variable.
Regarding the thing that you quoted..here's what you wrote that made me think that you said that. in another class i want to call Parser function and also be able to access the contents of the arrayList arr....You are wrong about static has to do with little memory.. remember it always stays there even if you no more need it..care need to be taken to make sure nothing is going wasted. Also, it depends actually. If you think that you this class and the arraylist are to remain same throughout and won't be changed later once the list is populated and would be needed at all stages..then its fine. Else work with objects. Whats the problem with just making one object and then using it .. not allowing any more objects to be created??? Go ahead...Cheers... :thumb:
P.S. - If you wish to design a class of which only object can be constructed...Read up on Singleton classes. And never think of static in terms of savior of memory..it is not..better get a clear idea abt static and then you would be better able to analyse the dos and donts.
LiteUponLite
July 20th, 2005, 12:46 PM
thnxs for clearing on the static topic, if u noe of any good site on that, plz do lemme noe......ok as per the code, i tried making all static the getFunction, the Parser and the ArrayList (since i've been asked to) and everything is stored fine in the HL7Convert1 class however, when i display the contents of the array (through the getFunction) it doesn't display all the contents of the arrayList, why is that???
LiteUponLite
July 20th, 2005, 12:54 PM
never mind, i had made an error in logic, its under control now :)...thnxs for ur help though
CJ1
July 20th, 2005, 01:06 PM
You really don't want to start messing around with static members in the end you will end up with more problems than you will solve.
As for saving memory this is false, it will not matter if you create the object or the compiler reserves the memory for you. In the latter case the GC will not be able to free the memory when you are done with it, so where is the savings?
If you are really concerned with saving memory why are you using C#?
exterminator
July 20th, 2005, 01:15 PM
You want it ..you got it..Here's some links to information on static:
1. Static keyword on MSDN (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfstaticpg.asp)
2. Static members of a class (http://www.csharphelp.com/archives/archive148.html)
3. Static constructors and their usage (http://www.c-sharpcorner.com/Code/2002/Sept/StaticConstructors.asp)
4. Static modifier (http://www.builderau.com.au/program/windows/0,39024644,20281610,00.htm)
Try Gooooooogle (http://www.builderau.com.au/program/windows/0,39024644,20281610,00.htm) too! And yes, you are welcome.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.