Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Using 'scope'?


  • Please log in to reply
16 replies to this topic

#1 Wolfy87

Wolfy87

  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:06:25 AM

Posted 24 September 2008 - 05:37 AM

I have recently lernt how to use arrays in C# and am tryingtotest them by having a save button (button1) a load button (button2) and two textBoxes (1 and2) where in 1 the textwill be written in to the array then on theclick of button2 the arrays string will write in textBox2. i do not knowhow to scope though and need to do this if i want to use the same array in 2 seperate tasks, ie theonei am trying to acheve, i havetryedusingpublic void instead of private void but to no avail. how do i unlocalize this array ie 'Scope' which i cant seem to find anything on?

BC AdBot (Login to Remove)

 


m

#2 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:12:25 AM

Posted 24 September 2008 - 05:58 AM

I typed 'scope' into my browser; this was the second result:
http://en.wikipedia.org/wiki/Scope_(programming)

#3 Wolfy87

Wolfy87
  • Topic Starter

  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:06:25 AM

Posted 24 September 2008 - 06:17 AM

This is what i mean, i have read this topic already but do not understand what it means or what it is talking about. I just need to find a sorce that explains it in a bit of a simpler way like what dose void f (bool :thumbsup: meen? is it naming the scope? so the array wouldgo in the parts where thef and b are like algebra?

#4 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:12:25 AM

Posted 24 September 2008 - 08:30 AM

Scope is how one defines the visibility of an object. There are two types of scope; local and global. The visibility of an object depends on where within a program it has been instantiated. The problem that you are having is that you instantiated your array local to the function, so the other function can not see it. You need to instantiate/create the array so that it is globally visible.

At the bottom of the page I linked you to was another link to a page on global variables. Maybe as part of trying to understand scope, you should check that one out also. :thumbsup:

#5 Wolfy87

Wolfy87
  • Topic Starter

  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:06:25 AM

Posted 24 September 2008 - 11:07 AM

Ive been looking at this and have found public static string but cant impliment it... -.- i just cant seem to find the awnser. im proboly doing the code completely wrong.

#6 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:12:25 AM

Posted 24 September 2008 - 11:11 AM

What happens if you run this code:
#include <iostream>
 
using namespace std;
 
int global = 3; // This is the global variable.
 
void ChangeGlobal()
{
   global = 5; // Reference to global variable in a function.
}
 
int main()
{
   cout << global << endl; // Reference to global variable in another function.
   ChangeGlobal();
   cout << global << endl;
   return 0;
}

Versus what happens when you run this code:
#include <iostream>
 
using namespace std;
 

 
void ChangeGlobal()
{
   int global = 3; // This is a local variable.
   global = 5; // Reference to local variable.
}
 
int main()
{
   cout << global << endl; // Reference to variable local to another function.
   ChangeGlobal();
   cout << global << endl;
   return 0;
}


#7 Wolfy87

Wolfy87
  • Topic Starter

  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:06:25 AM

Posted 24 September 2008 - 11:12 AM

How about instead of using an array i could use somthing i have found...

{
public static string x = "hello world!";
}

i dont know whether it would work though?

#8 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:12:25 AM

Posted 24 September 2008 - 11:18 AM

All that is doing is declaring a string. Public means that the string is available to the package, and static means that it is not an instance variable. Maybe you should look at the example I gave you and see if you can work out what is going on?

Three questions to ask yourself:
1. Why does the first example work?
2. Why does the second example not work?
3. How can you change the second example so that it does work?

#9 Wolfy87

Wolfy87
  • Topic Starter

  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:06:25 AM

Posted 24 September 2008 - 11:19 AM

How do i implement the code you have sent? i have found it already but overlooked it due to it being too complicated? im sorry about all the bother :thumbsup:

Ok then scrach that last post i made :|

#10 Wolfy87

Wolfy87
  • Topic Starter

  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:06:25 AM

Posted 24 September 2008 - 03:30 PM

Well im not sure about why the first one works but i gess the second one dose not work because it is creating and changing the vairable in the same function, where as they need to be in seperate functions. I do not understand the code at the bottom atall though. The part with cout << etc. Could you explain?

#11 Wolfy87

Wolfy87
  • Topic Starter

  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:06:25 AM

Posted 24 September 2008 - 03:38 PM

Ok i see the problem with the second one now! =]. No 1 is a global vairiable (can be used throught whole program) and in No 2 it is a local veriable so it can only be accessed in that function! So if you want it to be global it has to be declared out side (ubove) any functions!? Is this correct? :thumbsup:

#12 Wolfy87

Wolfy87
  • Topic Starter

  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:06:25 AM

Posted 24 September 2008 - 04:25 PM

Oops forgot 3...
No 3. Move the introduction of the vairiable outside and ubuve the function to declaire it Global so it can be accesed. :thumbsup:

#13 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:12:25 AM

Posted 24 September 2008 - 04:25 PM

Yes.

#14 Wolfy87

Wolfy87
  • Topic Starter

  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:06:25 AM

Posted 24 September 2008 - 04:37 PM

Tomurro i will implement this into my tester form. Thanks so much :thumbsup: and good luck with the report.
Thanks again,
Wolfy87.

#15 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:12:25 AM

Posted 24 September 2008 - 07:17 PM

I am glad you worked it through. It feels sort of good to figure it out on your own, doesn't it?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users