PDA

Click to See Complete Forum and Search --> : Private variable problem


DaoK
Nov 22nd, 2001, 04:16 PM
ok I have declared a variable as Private Static.


public class tp3

{

private static String allInformation[];
...


but in a SUB I do that :

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?

DaoK
Nov 22nd, 2001, 04:32 PM
Usually when I put my declaration Private Static it work everywhere but not now why?

filburt1
Nov 22nd, 2001, 04:46 PM
Variable scope problem because your mind has been tainted by VB. Try deallocating it (BTW, do i++, not ++i) or doing this:


String[] toReturn;
// do stuff on toReturn
return toReturn;

jwm
Nov 22nd, 2001, 04:52 PM
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"?

DaoK
Nov 23rd, 2001, 03:45 PM
Anyone know ?

Dillinger4
Nov 24th, 2001, 10:16 AM
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.

DaoK
Nov 24th, 2001, 10:50 AM
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.

Dillinger4
Nov 24th, 2001, 11:55 AM
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.

DaoK
Nov 24th, 2001, 01:59 PM
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.

DaoK
Nov 24th, 2001, 08:58 PM
ANy one can give me a piece of code ?

DaoK
Nov 25th, 2001, 10:41 AM
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

Dillinger4
Nov 25th, 2001, 10:59 AM
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.

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.

DaoK
Nov 26th, 2001, 08:22 PM
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?

Dillinger4
Nov 26th, 2001, 08:58 PM
Post your code in it's entirety. This was we can get a better picture of whats going on. :p

DaoK
Nov 27th, 2001, 06:57 PM
well thx you but i changed the code and remake one who work now.