a noobish question on variable scope

This is a discussion on a noobish question on variable scope within the C# Programming forums, part of the General Programming Boards category; what is the easiest way to access variables defined in another class for example Code: public partial class Form1 : ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    85

    a noobish question on variable scope

    what is the easiest way to access variables defined in another class

    for example
    Code:
    public partial class Form1 : Form
        {
                public int x ; 
                public int y ;
    
                public [,]int array = new int[10,10];
                
    
    // more functions etc
    }
    now in a new class
    Code:
    public class myclass()
    {
              public void addup()
             {
              x = 50;
              y = 100;
            array[0,1] = x+y;
            }
    this is very simplified and i can do what i want by passing them to the function but in some cases i need to alter a lot of variables which were not created in this class. Passing them all over will not be helpful or easy to follow. i have tried a few ways but being new to OOP i was wondering if i am missing something here?

    once again thanks in advance

  2. #2
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    pass in the class, and access the fields directly.
    Code:
    public partial class Form1 : Form {
                public int x ; 
                public int y ;
                public int[,] array = new int[10,10];
    }
    public class myclass {
      public void addup(Form1 f) {
        f.x = 50;
        f.y = 100;
        f.array[0, 1] = f.x + f.y;
      }
    }
    also, the "correct" way is to use properties, like this:
    Code:
    class A {
      private int _x;
      public int X { get { return _x; } set { _x = value; }
    }
    if u use a newer compiler (2008+) u can do this
    Code:
    class A {
      public int X { get; set; }
    }

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    85
    im using 2008 so i can use the last method. i have had a play around with this to try to get my head round it and have read stuff about encapsulation to aid in the process.

    when you say pass in the class

    what do i pass it as, an object ? and where is the correct place to instantiate it.


    Code:
    public partial class Form1 : Form {
                public int x ; 
                public int y ;
                public int[,] array = new int[10,10];
    
                myclass testclass = new myclass();
    
     public Form1()
            {
                InitializeComponent();
    
                testclass.addup(object Form1);
    
              . . . . . .
    }
    
    public class myclass {
      public void addup(Form1 f) {
        f.x = 50;
        f.y = 100;
        f.array[0, 1] = f.x + f.y;
      }
    }
    ???

    thanks

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by bling View Post
    if u use a newer compiler (2008+) u can do this
    Code:
    class A {
      public int X { get; set; }
    }
    So with the above you will have a private variable (name?) and a public property that sets, gets it? Like having:
    Code:
    class A {
      private int nameless;
      public int X { get{return nameless;} set{nameless = value;}}
    }

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Code:
    public partial class Form1 : Form {
                public int x ; 
                public int y ;
                public int[,] array = new int[10,10];
    
                myclass testclass = new myclass();
    
     public Form1()
            {
                InitializeComponent();
    
                testclass.addup(this);
    
              . . . . . .
    }
    
    public class myclass {
      public void addup(Form1 f) {
        f.x = 50;
        f.y = 100;
        f.array[0, 1] = f.x + f.y;
      }
    }
    maybe you want this?

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    85
    this i guess is sending by reference so has no overheads in terms of memory ?

    btw that works fine now, can the same method be used to allow one class to access another through the main form, if it has been instantiated in the main form ?

  7. #7
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    from your other posts i'm a little surprised you're asking about OOP stuff. i'd recommend googling for some tutorials about OOP and you'll be up to speed in no time.

    the same method can be used for any instance of Form1. it doesn't have to be instantiated, it can be null, and you'll just get a NullReferenceException.

    also, don't think of them as references, despite the name. semantically, it is much more similar to pointers.

    Code:
    public void Blah(SomeClass c) {
    }
    is the same as
    Code:
    void Blah(SomeClass* c) {
    }
    EDIT: that's for classes. if you use value types (i.e. int, double, float, string [a special exception]) it is pass by value. for primitive types (value types), u can use the 'ref' keyword, which is the same as passing in as int& in C++.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    85
    small snag here - something i need to access from the class is an object on the form and as such is defined as private in the form.designer.cs. is it wise to change it from private to public ? or can this be circumvented in some way as a temporay measure. or can i instantiate it and use a copy maybe ?

    also why is it not accessible if i have passed the form1 over - it is accesible there ?


    Bling - my problem is i am new to oop and havent coded for years. when i did it was old style coding without all the nifty additions you have now so i am really getting up to speed with things. i have spent the last 3- 4 weeks on C# and 70 percent of the time i have been reading different methods and appraoches to problems and debugging code to see why and how it works or sitting trying to figure out why something is not doing what i expected. sometimes i just need to overcome a particular issue to get on with the task at hand. i hope all the folks in the forum will bear with me and hopefully i will not have so many questions very soon
    Last edited by deviousdexter; 12-03-2008 at 07:55 PM.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by deviousdexter View Post
    small snag here - something i need to access from the class is an object on the form and as such is defined as private in the form.designer.cs. is it wise to change it from private to public ? or can this be circumvented in some way as a temporay measure. or can i instantiate it and use a copy maybe ?
    Just declare a public setter/getter function. Then if you need to perform some special synch operations, you can do that.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    deviousdexter - Yes it is safe, in fact you can click a control and set its access level through the properties tab. But probably a better way to go is what matsp said, and in c# we have a special way to facilitate this called a property.

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    85
    ok i see what your saying about the getter/setter but the item in question is a picturebox.

    and the following line
    Code:
    private System.Windows.Forms.PictureBox mapBox;
    appears in the Form1.Designer.cs

    so can you explain how i would go about using a getter/setter on this ?

    i can create a class and derive it from picturebox base - is this the way to go or can i achieve it another way?

    or cheat and change "private" to "public" lol
    i also see i can change its modifiers from the propertys page but this is the same as cheating right ?
    Last edited by deviousdexter; 12-03-2008 at 08:32 PM.

  12. #12
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    Code:
    public System.Windows.Forms.PictureBox MapBox {
      get { return this.mapBox; }
    }

  13. #13
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by deviousdexter View Post
    ok i see what your saying about the getter/setter but the item in question is a picturebox.

    and the following line
    Code:
    private System.Windows.Forms.PictureBox mapBox;
    appears in the Form1.Designer.cs

    so can you explain how i would go about using a getter/setter on this ?

    i can create a class and derive it from picturebox base - is this the way to go or can i achieve it another way?

    or cheat and change "private" to "public" lol
    i also see i can change its modifiers from the propertys page but this is the same as cheating right ?
    Err, there is no cheating If you want it public make it public. It will be more clear with a property because it is like saying "you can access this". The point being not changing anything in the Designer.cs.
    Personally, I would make it public it it was a project you will enterilly write by yourself. But that is just preferences.

    A getter/setter is really easy. The general format is:
    Code:
    private int x
    public int X
    {
        get { return x;}
        set
        {
            x = value;
        }
    }
    
    x = 5; //is like value = 5; setX(value);
    int t = x; //is like int t = getX();

  14. #14
    Registered User
    Join Date
    Oct 2008
    Posts
    85
    can i just confirm i have done this the way you intended ?

    i added
    Code:
     public PictureBox MapBox
            {
                get { return this.mapBox; } // couldnt use in class function as 'this' is invalid
            }
    onto the end of Form1.

    then added
    Code:
    PictureBox pictbox;
                pictbox = update.MapBox;  //update=public void myclass(Form1 update)
    to the function i have originally passed Form1 into.
    i can now access 'mapBox' ok so it works, but is this the correct implementation ?

  15. #15
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Yes, or since you are calling that method from a Form1 instantiation, why not just pass its PictureBox variable. Your way works though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Noobish question about graphics
    By ThWolf in forum Game Programming
    Replies: 5
    Last Post: 09-01-2006, 11:40 AM
  2. newb question: dynamic variable names
    By Dash_Riprock in forum C++ Programming
    Replies: 6
    Last Post: 07-17-2006, 10:48 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  5. Hammer, a question for you - var scope
    By * in forum C Programming
    Replies: 21
    Last Post: 06-24-2002, 09:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21