Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 C#: Objects and Encapsulation - Hard to Comprehend
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
syntax0r




PostPosted: Tue Apr 26, 2005 8:22 pm   Post subject: C#: Objects and Encapsulation - Hard to Comprehend

I am using C# and not C or C++. I thought this section of the forum would fit C# a question.

Question:

Am I correct? I will explain what I'm doing (in English) to do in this "OOP" with C#. I want to confirm the thing I'm doing, so I understand it properly and not just memorize syntaxes.

Why create an object?

I create an object as a piece of code that I can refer to in my "Main" section (don't know technical term for this main section, sadly). This somehow makes the coding process easier. I can picture an object as a foreign item that can be retrieved into my program by calling it, right?

What to do.

I create an object, with the line

code:

Static void doSomething (int Number) { // declare variable number as integer for later reference within the proceeding lines
string message = "";
message += "This is ";
message += Number
message += ".\n This is ";
message += getPlace(Number); // VERY CONFUSING HERE. MY TUT SAYS THAT I HAVE TO USE a getPlace method. I thought  I could just put the variable 'Number'  again, like previously
Console.WriteLine (message);
}//ends object called doSomething



OK.. I made my beatiful object with the peculiar getPlace() method. But why my must I use it? Let's continue by making the "main section" display this info.

code:


using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
           doSomething(1);
           doSomething(2); // What do these numbers refer to? The variable "Interger"

        }
    }
}



So the getNumber() method contains the variable "Number." I assume I have to make ANOTHER object, but I can't do another line with "static void getPlace", cuz it wont make sense.

Anways, the Tut says you need a Case Switch in order to refer the variable Number (1) to a value (let's say a string = "number 1") and a varialbe Number (2) to another value (let's say a string = "number 2")

This is all should work, outputting according to the Case Switch:



String Variable "Message", in theory, should equal:

This is number 1.
This is number 2

Yes, my questions may be incoherent, and I apologize. But if anyone can assist me with this creating the method getPlace(Number) would help a lot. Also, simple source code, tutorials or examples demonstrating the creating Object and Encapsulation purposes would help a lot!


[/code]
Sponsor
Sponsor
Sponsor
sponsor
1of42




PostPosted: Tue Apr 26, 2005 9:58 pm   Post subject: (No subject)

That's really confusing, you're being kind of unclear, but a couple of things:

1) That's not an object, that's a method.

2) I assume the getPlace() method is another method used in the tutorial that you must use in the doSomething() method.

Might you provide a link to the tutorial? What you're doing without a sense of context is very confusing...

btw: those code fragments, as far as I can tell, have nothing to do with objects or encapsulation...
syntax0r




PostPosted: Tue Apr 26, 2005 10:16 pm   Post subject: (No subject)

It's hard for me to convey my problems when I don't know what the problem is, hehe...

Maybe I need to become acquainted with OOP. Could somebody be kind enough to explain what is encapsulation and objects (without analogies-I hate analogies for some reason)

The tutorial is in a book, so I don't think I can show you the context of the code Sad.

However, the chapter from which I got this tutorial was dedicated to Encapsulation and Objects.
wtd




PostPosted: Tue Apr 26, 2005 10:26 pm   Post subject: (No subject)

http://en.wikipedia.org/wiki/Object-oriented
syntax0r




PostPosted: Wed Apr 27, 2005 7:20 pm   Post subject: (No subject)

Hehe, how foolish of me.

I continued reading when I finally semi-understood the tutorial. Turns out the Void NameOfMethod (int num( is a method, not an object.

And a class is an object, right?

Well, this OOP is still hard to understand, so I really need to go over that chapter until I get it.

What mind-boggles me the most is the getPlace() method.
wtd




PostPosted: Wed Apr 27, 2005 8:56 pm   Post subject: (No subject)

syntax0r wrote:
And a class is an object, right?


Not in C#, but in some languages, yes.

The general idea you should get is that a class is a blueprint for an object, specifying the data it contains (variables) and the actions permitted on it (methods). It also specifies the level of access permitted to each of those variables and methods. They can either be public, and thus accessible anywhere, or private, and only accessible within that particular class, or protected and live in something of a gray area. Protected variables and methods can be accessed both inside the class, and by subclasses.

Why do we have both variables and methods? Variables represent the internal state of an object. Methods represent ways we can interact with those variables in a controlled manner. Consider a car. To turn the car, we turn the steering wheel, rather than directly manipulating each individual wheel, because if we did we could really screw things up.

The same is true of objects.
syntax0r




PostPosted: Thu Apr 28, 2005 6:48 am   Post subject: (No subject)

Thanks for the post, it has helped me understand the concept, and now, after having read the tutorial once more, I am confident that I understand Encapsulation.

Just wondering about Private Instances in an object that have properties shared amongst other classes ( I hope I got the terminology correct):

code:


Class newThing {
      private string pName;
     
      public string name {
          get {
             return pName;
                 }
         
         set {
         pName = value;
         }
   } // end name property in class newThing





Correct me if I'm wrong, but I would still have something public, so my "name" property will be "SENT" via the get method to other classes.

And the set method will receive (from w/e class) the desired value of the name property?

Also, I will have to declare an instance variable inside my Main(), like so

code:


newThing thing = new newThing(); // thing is my variable pertaining to the property

thing.name = "Bob"; // give it's string name



The Private instance variable and property is a lil tricky, but i think i get it, right?
wtd




PostPosted: Thu Apr 28, 2005 9:57 am   Post subject: (No subject)

Basically, yes. You just need to work on your C# capitalization.

code:
class NewThing
{
   private string m_name;
     
   public string Name
   {
      get
      {
         return m_name;
      }
         
      set
      {
         m_name = value;
      }
   }
}


Please note that you can add code to check "value" inside "set". So if you want to only make the change if the new value is different from the existing name, or throw an exception, you could do that.
Sponsor
Sponsor
Sponsor
sponsor
syntax0r




PostPosted: Fri Apr 29, 2005 5:03 pm   Post subject: (No subject)

Thanks for the help WTD and others!

Now I'm moving on to Constructors and Polymorphesism, which sounds very intimidating.

I hope I will understand these new concepts in OOP.
wtd




PostPosted: Fri Apr 29, 2005 5:14 pm   Post subject: (No subject)

Well, constructors aren't too difficult. They basically just set up the initial state of the object by assigning values to instance variables.

Consider a simple Name class:

code:
class Name
{
   // instance variables
   private string first, last;

   // read-only properties.
   public string First
   {
      get { return first; }
   }

   public string Last
   {
      get { return last; }
   }

   // for auto-magical stringification
   public string ToString()
   {
      return first + " " + last;
   }
}


But now I need some way to set initial values for the names, especially since a name should be immutable.

code:
class Name
{
   // instance variables
   private string first, last;

   // constructor
   public Name(string f, string l)
   {
      first = f;
      last = l;
   }

   // read-only properties.
   public string First
   {
      get { return first; }
   }

   public string Last
   {
      get { return last; }
   }

   // for auto-magical stringification
   public string ToString()
   {
      return first + " " + last;
   }
}
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: