4 Replies - 1826 Views - Last Post: 08 September 2012 - 04:26 PM Rate Topic: -----

#1 hutcruchi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 08-September 12

newbie ask about namespaces classes and objects

Posted 08 September 2012 - 10:50 AM

hi all. ive just started to learn asp4.net with visual studio 2010 and c#.

last lesson they talk about objects , classes and namespace. i have to admit that i got a little bit confused.

from what i understood:

object is a variable that can obtain any type of data (int , double , datetime , string....)

class is a group of methods , variables , design preferences(text size , color, etc..) and more which stored toghther under one name and i can create an instance of that class and use its code.

namespace is the collection of selected classes.

my questions are:

1. did i understand correctly?

2. if i create a new class. why i need to create variables inside it? i can use the variables at the specific page where i need them and not at the class itself? , from what i understood class helps as being container for mor than one method. i dont need it for other things... am i right?


3. why to create different namespaces? and not use just one in my whole project? what is it good for?



thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: newbie ask about namespaces classes and objects

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5522
  • View blog
  • Posts: 11,830
  • Joined: 02-June 10

Re: newbie ask about namespaces classes and objects

Posted 08 September 2012 - 11:03 AM

Object is a type. Just like a button or an integer.

A1: Eh, kinda yes, kinda no
A2: You create variables as needed, at the level you need them. That might be within a method, that might be within a class.
Its what we call "scope". A thing exists between the {} it was created in. So if you make a thing within a for{} loop, that's its scope. If you want to access a thing for the entire class, then define it in the class braces

class someClass
{
   int yogi = 5; // Accessible to the class
   
   void someMethod
   {
        int booboo = 3; // Accessible to the method

        while(true)
        {
           string Ranger = "Smith"; // Accessible to the loop
        }
   }
}


A3: Namespaces help break up large volumes of code into logical chunks that are more easily dealt with. .NET has more than a million commands, objects, methods etc. Can you imagine trying to sort your way through that if it were all lumped together? But you can put things together that make sense together.
Everything involving serialport can be found at: System.IO.Ports.Serial making the methods related to serial ports easy to find.


This might help as to what an object or class is:

Think of objects in coding just as you would objects in the real world.

A Dodge Ram is an object.
It is made up of smaller objects: Engine, doors, tires
Each of those is made up of smaller objects: Bolts, pistons, etc.

Objects in coding can inherit from each other, usually from the general to the specific.

  • Class vehicle
    • Class truck : vehicle
      • Class Ram : Truck
        • class 2500FWD : Ram


A class is the blueprint for instanciating (making an instance of) the object.

DodgeRam is a class describing how to make an instance, but itself is not an actual thing you can interact with.

myDodgeRam is an instance of the class DodgeRam

I can do things with the object instance myDodgeRam

myDodgeRam.SerialNumber = 123456789;
myDodgeRam.FillUpTank();
float fuelLevel = myDodgeRam.FuelTankPercentageFull;
if (myDodgeRam.IsReady) myDodgeRam.StartEngine();


Anything defined in the base class is available to a child.

class truck : vehicle
{

    public float FuelTankPercentageFull
    {
       get; set;
    }
}

class DodgeRam : truck
{
    // I don't have to define a FuelTankPercentageFull here because I inherit it from my parent
}


Methods defined as virtual in the base class can be overridden by the child class (at least in .NET languages like C#). This is often to account for more specific needs.

class truck : vehicle
{

    public virtual bool StartEngine()
    {
       // Do something to start the engine
       return true; // No checks or requirements
    }
}

class DodgeRam : truck
{
    public override bool StartEngine()
    {
        // Do a safety check first
        if (IsSeatBeltsEngaged && IsFootOnBrake)
        { 
            return true;
        }
        return false;
    }
}


You can even have a child class call the base classes methods which is often the smart way to go.

class truck : vehicle
{

    public virtual bool StartEngine()
    {
       // Notice there are no safety checks before trying to start up.
       try
       {
          // Do something to start the engine
          return true; // because we succeeded
       }
       catch(exception error)
       {
           return false; // because there was an error
       }
    }
}

class DodgeRam : truck
{
    public override bool StartEngine()
    {
        // Do a safety check first
        if (IsSeatBeltsEngaged && IsFootOnBrake)
        { 
            return base.StartEngine();
            // Now all the electrical work is in the base class
            // and not repeated in every child.
        }
    }
}

Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3575
  • View blog
  • Posts: 11,115
  • Joined: 05-May 12

Re: newbie ask about namespaces classes and objects

Posted 08 September 2012 - 12:12 PM

Yikes! Learning C# with ASP.NET as your primary context is going to be a rough road. I recommend learning as much C# as you can independent of ASP.NET. Just working with the Console for UI at first. Once you get more confidence with C#, start easing back into ASP.NET.
Was This Post Helpful? 2
  • +
  • -

#4 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 488
  • Joined: 13-June 12

Re: newbie ask about namespaces classes and objects

Posted 08 September 2012 - 04:03 PM

Agree with Skydiver... Learning C# through ASP is like learning French by watching Amelie.
Was This Post Helpful? 2
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3575
  • View blog
  • Posts: 11,115
  • Joined: 05-May 12

Re: newbie ask about namespaces classes and objects

Posted 08 September 2012 - 04:26 PM

Thanks for that analogy. I'd been trying to think of one for about half an hour and just gave up and hit the post button. But it had been haunting me the entire day like a song that I couldn't get out of my head. The closest I could come up with was trying to learn Swedish by watching your hand in sock puppet through a monitor.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1