Class Help

This is a discussion on Class Help within the C# Programming forums, part of the General Programming Boards category; i am using the VB2008 IDE for C#. my question may seem very basic but the solution is eluding me ...

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

    Class Help

    i am using the VB2008 IDE for C#.

    my question may seem very basic but the solution is eluding me due to my lack of underastanding of classes and OOP programming i think.

    i am using the following code on Form1.cs

    Code:
    private void button2_Click(object sender, EventArgs e)
            {
               textBox1.Text = Convert.ToString(addit(10, 21));
            }
    and have a new class.cs page with the following in it

    Code:
    namespace WindowsFormsApplication1
    {
        public class AdderClass
        {
    
            public int addit(int x, int y)
            { 
                return (x + y);
            }
    
            public int subit(int x, int y)
            {
                return (x - y);
            }
    
            public int multit(int x, int y)
            {
                return (x * y);
            }
    
        }
    }

    why is the IDE saying that the name "addit" does not exist in the current context on Form1.cs ?

    they both appear in the same namespace although on different code sheets.

    i now this is something really simple that i am not getting the grasp of but it has eluded me up to now.

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    You must make an object of the class to call that method, either that or make the method static and then you can call it with AdderClass.addit(). Make sure the class file is in the same namespace as form1 as well. Static methods are really as close as it comes in C# to straight functions, other then that you will have to instantiate all your classes.

    So in form1 in class scope put something like this.

    AdderClass m_MathUtil;

    Then in form1's constructor, the part where it InitializeInstance(), put m_MathUtil = new AdderClass();

    Now in your event handler for when the button is clicked put, m_MathUtil.addit(10, 21);

    This should get you what you want.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    85
    thanks Valaris

    i have actually done than since the posting and managed to get it to work.
    many thanks for your response.

    I think the problem was i assumed ( incorrectly) that because it was in the same namespace it should be accessible to anything within that namespace.

    Does everything that is defined in another class in the same namepsace have to instantiated before it can be used? and does where it is instantiated make a difference as to where it is accessible from within the namespace ?

    Would i be right in my assumption that if i created a class in a different namespace i could use that with the "using" directive ?

  4. #4
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    Quote Originally Posted by deviousdexter View Post
    Does everything that is defined in another class in the same namepsace have to instantiated before it can be used?
    Yes, unless its a static class... Simple example, you have a class named Chair, now this chair can have 4 or 3 legs...

    But how can you tell what kind of chair it is , if you have not even created the chair yet? So first you would have to create an instance of the chair , lets call this aThreeLeggedChair.

    Code:
    public class Chair {
      private int legs;
      public Chair (int legs) {
        this.legs = legs;  
      }
    
      public int Legs {
        get {
          return this.legs 
        } 
        set {
          this.legs = value;
        }
      }
    
    }
    Then elsewhere in the same namespace you can just say:

    Chair aThreeLeggedChair = new Chair(3);

    and does where it is instantiated make a difference as to where it is accessible from within the namespace ?
    Look up the meaning of scope...

    Would i be right in my assumption that if i created a class in a different namespace i could use that with the "using" directive ?
    Yes.


    My advice is, read some tutorials first before you ask questions about trivial things. Things one ought to know even well before you start using objects (cfr scope of variables). Just type C# tutorial in your favorite search engine and start reading/learning .
    Last edited by GanglyLamb; 10-22-2008 at 03:55 PM.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    85
    thank you.

    i have read many tutorials and looked and stepped through many code segments, how ever there are certain elements that do not appear to be as apparent as i would like. Couple this with having to continually cross reference code from different "levels" of programmers, makes it quite a hard task. I have done programming before just not extrensively in this environment or using forms.

    Having the IDE automatically create big lumps of code is convenient but not really informative for newcomers (IMO). And because lots of people create there routines slightly differently, i have to double check that it is doing what i think it is or isnt doing.

    i like to know what the code i am using is doing before actually implementing it.

    i hope you will bear with me while i ask questions while i adapt to this style of programming and forgive me any ignorances i may show in the early stages.

    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 08:10 AM
  2. Two conceptual questions
    By AntiScience in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2007, 11:36 AM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

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