Results 1 to 15 of 15

Thread: Private variable problem

  1. #1
    DaoK
    Guest

    Private variable problem

    ok I have declared a variable as Private Static.

    Code:
    public class tp3
    
    {
    
    private static String allInformation[];
    ...
    but in a SUB I do that :
    Code:
    allInformation = new String[10000];
        for (i = 0; i < 10000; ++i)
          allInformation[i] = new String();
       //After that code I start to pu DATA in the ARRAY.
       //All is great since that state.
    but now I want to use the DATA in the ARRAY in an other sub and I can not find it because it's empty ?

    Why?

  2. #2
    DaoK
    Guest
    Usually when I put my declaration Private Static it work everywhere but not now why?

  3. #3
    Variable scope problem because your mind has been tainted by VB. Try deallocating it (BTW, do i++, not ++i) or doing this:

    Code:
    String[] toReturn;
    // do stuff on toReturn
    return toReturn;
    Last edited by filburt1; Nov 22nd, 2001 at 05:55 PM.

  4. #4
    Member
    Join Date
    Nov 2001
    Posts
    41

    Re: Private variable problem

    Originally posted by DaoK
    I want to use the DATA in the ARRAY in an other sub and I can not find it because it's empty ?

    Why? [/B]
    what do u mean by " can't find it"?

  5. #5
    DaoK
    Guest
    Anyone know ?

  6. #6
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    It looks like a scope problem. The String array that you are creating within your method is scoped to that method. Once the method terminates the garbage collector starts to reclaim memory. Try creating a method that takes a String array as an arguement and returns a String array.

  7. #7
    DaoK
    Guest
    OK but how ? It's very important I need to be able to use all data in that array everywhere in the project. Please tell me how to declare it.

  8. #8
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Im sure you could use getters and setters and declare your String array which is a member of your class private. This would probably be good if your application has multiple classes.

  9. #9
    DaoK
    Guest
    you could use getters and setters and declare your String array which is a member of your class private.
    Ok but how...they didnt learn us to do that and I need to be able to that array in all my class please.

  10. #10
    DaoK
    Guest
    ANy one can give me a piece of code ?

  11. #11
    DaoK
    Guest
    Question is :

    Give me the way to declare a simple Array variable who will be able to access the data anywhere in the class.
    THANK YOU

  12. #12
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    It depends on the way the variable is declared and in what context you are trying to access your array. For instance if your array is declared non-static and you are trying to refrence it from a static context then it wont work. The code that you posted.
    Code:
    allInformation = new String[10000];
        for (i = 0; i < 10000; ++i)
          allInformation[i] = new String();
       //After that code I start to pu DATA in the ARRAY.
       //All is great since that state.
    you said that in a sub, so i am assuming that you have a method which you are trying too access the String array. You also said that the array is empty or i guess padded with null values, but the block of code you provided only creates (as far as i can see)
    empty strings.

  13. #13
    DaoK
    Guest
    Yes that empty then I put data on it but when I am in another sub after have put some data on it I can not access to it why?

  14. #14

  15. #15
    DaoK
    Guest
    well thx you but i changed the code and remake one who work now.

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