How to call one constructor from another?

S

Sheila Jones

Hello,

Can somebody tell me how to do the following, please?

I have written a 'GridReference' class to encapsulate map locations. It has
a constructor that takes a couple of doubles, representing latitude and
longitude, i.e.
public GridReference(double latitude, double longitude) {
...
}

I now want to write another constructor that takes two strings. The strings
again contain latitude and longitude, but formatted as degrees, minutes and
seconds, for example: 52° 39' 27.246"N.

How do I write this second constructor so that it converts the strings to
the double equivalents (I can do that), then 'calls' the first constructor
with these values? Sort of like this:

public GridReference(string latitude, string longitude) {
double lat = ConvertToDouble(latitude);
double lng = ConvertToDouble(longitude);
//somehow jump to GridReference(lat, lng);
}

Thanks.
 
J

Jon Skeet [C# MVP]

Sheila Jones said:
Can somebody tell me how to do the following, please?

I have written a 'GridReference' class to encapsulate map locations. It has
a constructor that takes a couple of doubles, representing latitude and
longitude, i.e.
public GridReference(double latitude, double longitude) {
...
}

I now want to write another constructor that takes two strings. The strings
again contain latitude and longitude, but formatted as degrees, minutes and
seconds, for example: 52° 39' 27.246"N.

How do I write this second constructor so that it converts the strings to
the double equivalents (I can do that), then 'calls' the first constructor
with these values? Sort of like this:

public GridReference(string latitude, string longitude) {
double lat = ConvertToDouble(latitude);
double lng = ConvertToDouble(longitude);
//somehow jump to GridReference(lat, lng);
}

You'd do:

public GridReference(string latitude, string longitude)
: this (ConvertToDouble(latitude), ConvertToDouble(longitude))
{
}

IIRC, the ConvertToDouble method needs to be static.
 
?

=?ISO-8859-2?Q?Marcin_Grz=EAbski?=

Hi Sheila,
public GridReference(double latitude, double longitude) {
// body
}
public GridReference(string latitude, string longitude)
: this(ConvertToDouble(latitude), ConvertToDouble(longitude))
// double lat = ConvertToDouble(latitude);
// double lng = ConvertToDouble(longitude);
//somehow jump to GridReference(lat, lng);
// don't need to jump ;)

Regards

Marcin
 
S

Sheila Jones

Thank you both for the quick replies! That's exactly what I wanted - and it
even works if ConvertToDouble is private! (although it has to be static?)


Sheila Jones said:
Can somebody tell me how to do the following, please?

I have written a 'GridReference' class to encapsulate map locations. It has
a constructor that takes a couple of doubles, representing latitude and
longitude, i.e.
public GridReference(double latitude, double longitude) {
...
}

I now want to write another constructor that takes two strings. The strings
again contain latitude and longitude, but formatted as degrees, minutes and
seconds, for example: 52° 39' 27.246"N.

How do I write this second constructor so that it converts the strings to
the double equivalents (I can do that), then 'calls' the first constructor
with these values? Sort of like this:

public GridReference(string latitude, string longitude) {
double lat = ConvertToDouble(latitude);
double lng = ConvertToDouble(longitude);
//somehow jump to GridReference(lat, lng);
}

You'd do:

public GridReference(string latitude, string longitude)
: this (ConvertToDouble(latitude), ConvertToDouble(longitude))
{
}

IIRC, the ConvertToDouble method needs to be static.
 
J

Jon Skeet [C# MVP]

Sheila Jones said:
Thank you both for the quick replies! That's exactly what I wanted - and it
even works if ConvertToDouble is private! (although it has to be static?)

Indeed - it being private isn't a problem at all. The reason it has to
be static is that it's being called effectively before the rest of the
object's state has a chance to "settle down" - so anything which *does*
rely on its state is probably broken.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top