CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    May 2004
    Posts
    106

    public variable not accessible, why??

    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:

    Code:
    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)
                    }
    }

  2. #2
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: public variable not accessible, why??

    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.
    Last edited by darwen; July 20th, 2005 at 08:33 AM.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  3. #3
    Join Date
    May 2004
    Posts
    106

    Re: public variable not accessible, why??

    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??

  4. #4
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Thumbs up Re: public variable not accessible, why??

    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().

  5. #5
    Join Date
    May 2004
    Posts
    106

    Re: public variable not accessible, why??

    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.

  6. #6
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Thumbs up Re: public variable not accessible, why??

    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.
    Quote Originally Posted by LiteUponLite
    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...

    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.
    Last edited by exterminator; July 20th, 2005 at 09:41 AM.

  7. #7
    Join Date
    May 2004
    Posts
    106

    Re: public variable not accessible, why??

    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???

  8. #8
    Join Date
    May 2004
    Posts
    106

    Re: public variable not accessible, why??

    never mind, i had made an error in logic, its under control now ...thnxs for ur help though

  9. #9
    Join Date
    Dec 2003
    Location
    Montreal
    Posts
    58

    Re: public variable not accessible, why??

    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#?

  10. #10
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured