override and out keyword  
Author Message
samwalrus





PostPosted: Visual C# Language, override and out keyword Top

hello,

I am learning c# and I am not sure if this is possible. If any one can help thanks :).

I have a base class called vehicle and two derived classes car and lorry.

in the vehicle class I would like to have a showDetails() method that returns 3 varribels using the out keyword.

then in the derived classes i would like to override this method doing the same thing as the base class plus out putting an additonal two varibles in the case of the car class, and 3 in the case of the lorry class.

so in the vehicle class I have:

public virtual void ShowDetails(out string engineType,
    out string engineSize,
    out string registrationNumber)
{
 engineType = base.GetEngineType();
 engineSize = base.GetEngineSize().ToString();
 registrationNumber = base.GetRegistrationNumber();
}

 

and in the car class i have:

 

public override void ShowDetails( out string engineType,
     out string engineSize,
     out string registrationNumber,
     out string carType,
     out INT numberOfPassengers )
{
  base.ShowDetails (out engineType, out engineSize, out registrationNumber);
  carType = GetCarType();
  numberOfPassengers = GetNumberOfPassenegers();
  engineType = base.GetEngineType();
  engineSize = base.GetEngineSize().ToString();
  registrationNumber = base.GetRegistrationNumber();
}

this comes up with an error, no suitable method found to override.

What am i doing wrong is this even possible



Visual C#12  
 
 
CetinBasoz





PostPosted: Visual C# Language, override and out keyword Top

Second one is a new method signature rather than being override of an existing one. In other words those 2 are different methods and nothing to override.

PS: Also it would be int not INT.  And you're duplicating call to base.GetEngineType(),Size,RegNumber.

 


 
 
CetinBasoz





PostPosted: Visual C# Language, override and out keyword Top

Edit function here made it a mess.

public void ShowDetails( out string engineType,
out string engineSize,
out string registrationNumber,
out string carType,
out INT numberOfPassengers )
{
base.ShowDetails (out engineType, out engineSize, out registrationNumber);
carType = GetCarType();
numberOfPassengers = GetNumberOfPassenegers();
}


 
 
samwalrus





PostPosted: Visual C# Language, override and out keyword Top

ok thanks for replying.

Im having trouble understanding the signature idea.

why is the second one a new method signature rather than an override of the existing one how do i make the second one a method that over rides the first one

thanks


 
 
Jeff Wharton





PostPosted: Visual C# Language, override and out keyword Top

To override a method, the signature of the new method must be the same as the signature of the method in the base class.

Also, not sure why you are using out parameters to return the data. You could just access the properties of the class instead of calling ShowDetails. You could even use an array to pass in the parameters. Bit hard to see what you are trying to achieve when I can't see what the data from ShowDetails is being used for.



 
 
CetinBasoz





PostPosted: Visual C# Language, override and out keyword Top

C# language specification sections 3.6 and 10.5 explain it detailed. From section 3.6 as is:

"

3.6 Signatures and overloading

Methods, instance constructors, indexers, and operators are characterized by their signatures:

The signature of a method consists of the name of the method and the type and kind (value, reference, or output) of each of its formal parameters, considered in the order left to right. The signature of a method specifically does not include the return type, nor does it include the params modifier that may be specified for the right-most parameter.

"

To say that in a simple way

-The name of a method with exact casing (C# being case sesnsitive language)
-Order of parameters
-Count of parameters
-Modifier and type of parameters

defines a signature (return type is not part of signature). ie:

public void MyMethod(int p1, out string p2, ref int p3, int[] p4) {}
private int MyMethod(int px1, out string px2, ref int px3, int[] px4) {}

would not compile. Because it has 2 methods with the same signature. Look at it as:

MyMethod(int , out string , ref int, int[])

it is the same signature (compiler wouldn't know which one to call).

public void MyMethod(int p1, out string p2, ref int p3, int[] p4) {}

public void MyMethod(string p1, out string p2, ref int p3, int[] p4) {}

these 2 have different signatures (first parameter type is different).


Also these 2 are different:

MyMethod(int p1)

MyMethod(int p1, int p2)


 
 
James Curran





PostPosted: Visual C# Language, override and out keyword Top

The purpose of a virtual function is to allow code like this:


void PrintInfo(Vehicle v)
{
string engineType;
string engineSize;
string registrationNumber;
v.ShowDetails(out engineType, out engineSize, out registrationNumber);
Console.WriteLine("{0}, {1}, {2}", engineType, engineSize, registrationNumber);

}
Main()
{
PrintInfo(New Car());
}

This won't work, because Car.ShowDetails needs two additionals parameters.

Now, there is a way to do exactly what you want. To discover it, you must first ask yourself, "Why am I returning infomation with out parameters, when there's is a perfectly good return mechamism built into functions, which I'm completely ignoring " And, "How can I return that information using a return statement "



 
 
samwalrus





PostPosted: Visual C# Language, override and out keyword Top

probbaly because i dont know about this method!

as i understand it paramters all have to be the same varibel type

to give more backround on the problem what I am doing is making a traffic light simulation.

I want the program to go througth a loop getting all the details of the vehicle which could be a car or a lorry which will include things like colour, number of passengers, type of car etc, then from some of these details it will draw the car or lorry.

I thought when you over ride a method it needs to have differnt number of varribles to the original


 
 
Jeff Wharton





PostPosted: Visual C# Language, override and out keyword Top

I thought when you over ride a method it needs to have differnt number of varribles to the original

When you override a method in a base class the params (signature) need to be the same



 
 
James Curran





PostPosted: Visual C# Language, override and out keyword Top

I thought when you over ride a method it needs to have differnt number of varribles to the original

No. When you overload a method, the signature needs to be different. When you override one, they must be the same.

So, you need a way to return a different number of parameters through methods with the same signature:



class
VehicleDetails
{
public string engineType;
public string engineSize;
public string registrationNumber;
}

class Vehicle
{
public virtual VehicleDetails GetDetails()
{
VehicleDetails vd =
new VehicleDetails();
vd.engineType =
base.GetEngineType();
vd.engineSize =
base.GetEngineSize().ToString();
vd.registrationNumber =
base.GetRegistrationNumber();
return vd;
}
}

class CarDetails : VehicleDetails
{
public string carType;
public string numberOfPassengers;
}

class Car : Vehicle
{
public override VehicleDetails GetDetails()
{
VehicleDetails vd =
base.GetDetails();
CarDetails cd =
new CarDetails();
cd.engineType = vd.engineType
cd.engineSize = vd.engineSize
cd.registrationNumber = vd.registrationNumber;
cd.carType = GetCarType();
cd.numberOfPassengers = GetNumberOfPassenegers();
return (VehicleDetails) cd;
}
}

Note that both GetDetails methods have exactly the same signature. Note also, to access the two additional properties in the object return by Car.GetDetails, you'll have to cast it to a CarDetails object.