Click here to Skip to main content
15,891,725 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hey guys,

I'm trying to implement a sample code here, I just want to ask:

1. Whenever I create a class, by default if the class name is Car1, it automatically has "public class Car1" and inside it, the first method "public Car1". How come, that whenever I create another method let's say: Car2 inside "public class Car1". But out side the curly bracket of "public Car1" it says "Method must have a return type"?

C#
public class Car1{
public Car1()

{
//Stuff here
}

public car2()    <<<===============This will cause an error method must have a return type, unless I put  void before the car2
{
//Stuff here
}

}


There you go, I hope someone can tell me because I'm trying to create a new method that will send a data to another class in another file and I'm not sure if I will be able to do that with the use of void.

Thanks,
Posted
Updated 16-Apr-14 4:09am
v2
Comments
Rahul VB 29-Jan-14 2:42am    
Question up voted, so that anyone who sees it, will learn the very basics of Class, Constructor.

Here You need to know fewthings. These are

1. Constructor : It's name same as class name and it doesn't have any return type.
2. Method : Each method has a return type. If you don't return any value from a method then it is void type.
3. void : Void is a return type and it used when method doesn't returns.

In your example Car1 is your class so it constructor is

public car1()
{
}

But Car2() is not a constructor for Car1. It is a method for Car1 so it should be a valid return type.

Check following example:

C#
using System;

namespace PrimeNumber
{
    class Program
    {
        static void Main(string[] args)
        {
            Car2 car = new Car2(100.12f);
            Console.WriteLine("Speed is :{0}",car.Speed);
            Console.Read();
        }
    }

    public class Car1
    {
        private float mSpeed;

        public Car1(float speed)
        {
            mSpeed = speed;
        }
        public float Speed
        {
            get { return mSpeed; }
        }
    }
    public class Car2 : Car1
    {
        public Car2(float speed)
            : base(speed)
        {
        }

    }

}
 
Share this answer
 
v2
Comments
Karthik_Mahalingam 29-Jan-14 1:50am    
5 neat
Sandeep Singh Shekhawat 29-Jan-14 2:04am    
Thanks
 
Share this answer
 
Comments
KatsuneShinsengumi 29-Jan-14 2:46am    
Hi, I have a question, how this code work?

public mySampleClass(): this(10)
This is from the link you gave me. I dot get the "this(10)" part, I'm sorry, Im just new to this stuff and really want to be in web dev. I'm following a book and got stuck to the constructors part.

Thanks,
Karthik_Mahalingam 29-Jan-14 3:18am    
this will work if it has a constructor of integer parameter.
as

public class mySampleClass
{
public mySampleClass()
: this(10)
{
}
public mySampleClass(int a)
{
}
}
KatsuneShinsengumi 29-Jan-14 3:26am    
So this means that the mySampleClass constructor has an int named "a" and has a value of 10?
Karthik_Mahalingam 29-Jan-14 3:57am    
a is the name of the parameter, it can be of any name, event katsune :) also... it will work...
Constructors are the special types of methods of a class which get executed when it's object is created. Constructors are responsible for object initialization and memory allocation of its class.They have same name as of class and have no return type.

Different Constructor types are as :
Default Constructor
Parameterized constructor
Private Constructor
Static Constructor
Copy Constructor

Here Car1() is the constructor so it has no return type , also Car1() doesn't take any argument therefore it is an example of default/non-parameterized constructor .

Car2() is a method so it must have a return type
 
Share this answer
 
v2
Comments
Rahul VB 29-Jan-14 2:44am    
Very nice, please tell me what is a "Copy constructor"?
Praveen_P 29-Jan-14 3:22am    
Copy constructor creates an object by coping value from another object.
Rahul VB 29-Jan-14 3:53am    
thanks a ton
Rahul VB 29-Jan-14 4:30am    
Thanks, brother i have referred your solution and improved mine. Please go through and tell me if it is correct. Thanks.
Praveen_P 29-Jan-14 6:28am    
thank you , plz refer these url's http://www.aspdotnet-suresh.com/2013/09/csharp-constructor-example-types-of-constructor-in-csharp.html , http://www.c-sharpcorner.com/UploadFile/0c1bb2/constructors-and-its-types-in-C-Sharp/ where u can find sample code for different constructor types
Hello,

what i can see is that you are confused between a constructor and a class. Lets start with some basics:

Quote:
What is a Class?

Class is a collection of related things(it should be). Classes are used to organize the functionality of your code.

For example you have a class which contains all the methods related to networking: For example, pinging, creating a socket etc.

When i create a class i am just creating its blueprint. How should i realize it? to do that i will say:

Class1 c = new Class1();


Please note: the
Quote:
new
keyword is used to allocate memory for the
Quote:
object
of the class.


Pay attention to the words i have put in quotes. I can use the functionality of that class using its object. So to use the functionality of Class1, i can now use c.

So i say c.some_method();

Quote:
What is a constructor?


Constructor can be called a special method which is used to initialize the fields of a class.Its name is the same as that of the class. Constructors are of 2 types: Parametrized and Parameter less.

As explained by my friend(Praveenpk3) in Solution 3 there are many other types of constructors, referring to his solution,

Quote:
Different Constructor types are as :
Default Constructor
Parameterized constructor
Private Constructor
Static Constructor
Copy Constructor



Look at the code below:
C#
public class Class2
   {
     string s;

     public Class2()   ////parameterless constructor
      {

      }

      public Class2(string s) ////parametrized constructor , because it accepts a string.
      {
          this.s = s;
          Console.WriteLine(this.s);

      }

      ////Please note there is no return type for a constructor.
      ///Homework1: Why is there no return type for a constructor?
      ///Homework2 : Whats the difference between "this.s" and "s"?


   }


Quote:
Why to use a constructor?


- Suppose that you want to initialize some field of a class at start up? how do you do that?
using the code i showed you.Experience will help you figure out when to use a parameterized constructor.


Quote:
What is a method?


A method is some thing which performs some operation or function.
It can take one/many inputs perform an action
Quote:
depending on those inputs
and return results.
For example :

C#
public bool method1(int i)
       {

           return i == 1; ///returns true of i =1

       }

I dont have a void here. Why? because it does not return a void(which means nothing/empty). It returns something, a result, whose type is a bool.

It may not take any inputs and simply return the results.

C#
public bool method1()
      {

          return false;

      }




It may neither take any input nor return any results.

C#
public void method2()
      {

          Console.WriteLine("hello");

      }



I hope this has helped you to learn the significance of a return type.

Quote:
Homework3:
public bool method1(int i)
{

return i == 1; ///returns true of i =1

}


public bool method1()
{

return false;

}

What is the above phenomena called? Why to use it?




Start studying and happy learning.

One last thing: Please do the homework, will be good for you.

Thanks
 
Share this answer
 
v4
Comments
KatsuneShinsengumi 29-Jan-14 3:27am    
Thanks, that was very clear and informative,. you should teach in university.
Rahul VB 29-Jan-14 3:52am    
:)Thanks a lot, i am just a simple developer nothing more. Happy learning. One request, please do the homework. Thanks.
KatsuneShinsengumi 29-Jan-14 5:32am    
I'm on it pal,. thanks again,.
KatsuneShinsengumi 29-Jan-14 6:09am    
Hey here's my answer, I hope it proves that I'm really getting this stuff in the right direction.

Homework1: Why is there no return type for a constructor?

-bacause based on my understanding this is a method(although it's not) that acts as an initial method to the whole class. So logically there should be no return to other class that will happen because this is the first to execute. So no other request of data should be running before this one.

///Homework2 : Whats the difference between "this.s" and "s"?

-this might depend on how it is being use but "s" is the variable that holds data and can be access by anyone if in public and only to a it's method or class if private and "this.s" means you are pointing to the "s" variable that belongs to a specific method.

///Homework3 : What is the above phenomena called? Why to use it?

-I dont know the right term but, but it seems like the two method did just passed true in the first method and false in the second method as how bool is supposed to beused. Maybe encapsulation because this method can be tapped by another method or theconstructor without influencing the stuff that's internal stuff of the method method1
Rahul VB 29-Jan-14 7:25am    
Homework1:
According to what i know, its correct. I am not that knowledgeable. Your logic is correct brother. Good work.

Homework2:
"this.s" tells you that "s" is the variable which belongs to the same class.
"s" is a different variable altogether. It is an external variable(a parameter variable) which does not belong to this class.
As you can see we have defined "s" 2 times. Once in the class2 and once as a parameter of the constructor.
So how do you differentiate between them? thats why we say "this.s".

Homework3:
This is called method overloading. One of the type of polymorphism. You can change the behaivour of the function at runtime.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900