By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,413 Members | 1,451 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,413 IT Pros & Developers. It's quick & easy.

How to call one constructor from another?

P: n/a
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.
Nov 15 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
Nov 15 '05 #2

P: n/a
Sheila Jones <sh**********@btopenworld.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3

P: n/a
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?)
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Sheila Jones <sh**********@btopenworld.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4

P: n/a
Sheila Jones <sh**********@btopenworld.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.