delivered the opinion of the court.
These two cases involve the same questions of law, were heard together in the court below, and will be disposed of in a single opinion here.
While the sole assignment of error in each case is to the action of the trial court in sustaining a demurrer to the notice of motion for judgment, a statement of the factual background of the litigation, as disclosed by the briefs, will perhaps clarify the issues presented.
In November, 1939, A. A. Fleming and W. M. McFall were elected to the respective offices of sheriff and treasurer of Dickenson county, and duly qualified as such for the term beginning January 1, 1940, and ending December 31, 1943.
*791At the election held in November, 1943, both of the incumbents were unsuccessful candidates to succeed themselves for the four-year term beginning January 1, 1944, and ending December 31, 1947. On the face of the returns Fleming was defeated for the office of sheriff by J. H. Anderson, who received a certificate of election, qualified, took over and performed the duties of that office until July 16, 1946. Likewise, on the face of the returns, McFall was defeated for the office of treasurer by Charles P. Mullins, who received a certificate of election, qualified, took over and performed the duties of that office until July 16, 1946.
On July 9, 1946, in a proceeding to contest the election, the Circuit Court of Dickenson county held that, “because of the fraud and irregularities and improper conduct of the parties holding the election, and the friends and adherents of the candidates,” the purported election was void, and accordingly the certificates of election issued to Anderson and Mullins, showing them to have been elected as sheriff and treasurer, respectively, of the county, were canceled as of July 16, 1946. Thereupon, pursuant to Code, section 136, the court appointed Cowan Edwards, as sheriff, and W. B. Trivett, as treasurer, for the four-year term beginning January 1, 1944.
On February 12, 1947, Fleming filed a notice of motion for judgment against Anderson, and each of the latter’s deputies, to recover the salaries and emoluments of office which the defendants had received from January 1, 1944, to July 16, 1946, amounting, it was alleged, to approximately $40,000.
About the same time McFall filed a similar notice of motion for judgment against Mullins, and the latter’s deputies, to recover the salaries which the defendants had received during the same period, and alleged to be approximately $26,000.
Each of these notices of motion contained two counts. The first count was in the usual form of the money counts in an action of indebitatus assumpsit.
*792The next was a special count which in the Fleming v. Anderson Case, read thus:
“And for this also, to wit: That in the year 1939 I was duly elected to the office of sheriff for Dickenson County, Virginia, for the term beginning January 1, 1940, and until my successor qualified, and pursuant to said election I qualified, gave the bond and entered upon the discharge of the duties of said office on January 1, 1940, and was entitled to receive the fees and emoluments thereof and have always been ready and willing from and after that date to perform the duties and receive the fees and emoluments thereof, but on, to wit, January 1, 1944, you wrongfully, fraudulently and illegally secured certificates of election to said office and intruded yourselves into the same and took upon yourselves the discharge of its duties and illegally held the same from the 1st day of January, 1944, to August, 1946,, when pursuant to the judgment of the Circuit Court for Dickenson County your certificate and right to said office was canceled and annulled ah initio, and during said time you have prevented me from exercising and discharging the duties of the same or receiving any emoluments thereof, but you kept and received emoluments thereof during said time; that during the period from January 1, 1944, to August 1, 1946, you enjoyed the said office, collected and converted to. your use the fees and emoluments thereof, which amounted to the sum of $40,000, .and by reason thereof you became indebted to me in the said sum of $40,000, and in consideration thereof undertook and promised to pay me said sum of money when thereunto afterwards requested.
“Nevertheless, you and each of you not regarding your said several promises and undertakings hath not paid to me the said several sums of money or any part thereof, although often requested so to do, to my damage in the sum of $40,000.”
A similar special count was filed in the McFall v. Mullins Case.
Attached to each of the notices of motion for judgment *793was a statement of the salaries received by the respective officers and their deputies during the period.
In each case the defendants filed a demurrer to the notice of motion for judgment, and to each and every count thereof, specifying numerous grounds of legal insufficiency which need not be detailed. Suffice it to say that they properly raised the question as to whether, the plaintiff in each case was entitled to recover under the allegations of the special count.
In a written opinion the lower court reached the conclusion that the plaintiffs had not stated a legal cause of action. Consequently, the demurrers were sustained and the actions were dismissed.
To review the judgments entered in the two cases writs were awarded the plaintiffs. The parties will be referred to as they appeared in the court below.
The first assignment of error is to the action of the court in sustaining the demurrers to the first counts, which, as has been said, were in the usual form of the money counts in assumpsit.
It is, of course, well settled that a demurrer will not lie to the common counts in assumpsit. Burks Pleading and Practice, 3d Ed., section 93, p. 180; Oliver Refin. Co. v. Portsmouth Cotton Oil Refin. Corp., 109 Va. 513, 517, 64 S. E. 56, 132 Am. St. Rep. 924; Norfolk v. Norfolk County, 120 Va. 356, 360, 91 S. E. 820. Hence, the plaintiffs are technically correct in their contention that the demurrers to the notices of motion as a whole should have been overruled. Grubb v. Burford, 98 Va. 553, 555, 37 S. E. 4.
Indeed, the ruling of the trial court in this respect seems to have been due to an oversight, since the matter is not mentioned in its opinion which goes to the heart of the cases as presented in the demurrers to the special counts.
While it is true that an action of this character—to recover funds alleged to have been improperly collected by a de facto officer and due by him to a de jure officer—may be in the form of an action for money had and received (Booker v. Donohoe, 95 Va. 359, 361-2, 28 S. E. 584; 43 *794Am. Jur., Public Officers, section 392, p. 172), it is perfectly apparent here that unless the plaintiffs below are entitled to recover under their special counts, they have no case and their actions must fail. (See Booker v. Donohoe, supra, 95 Va., at page 362.) There is no contention that they can make out a stronger case under the common counts, than they have under the special counts.
Manifestly, then, if the lower court was correct in holding that the special counts do not state a cause of action, it would be quite useless, because of the technical error complained of, to remand the cases for a new trial. The litigation would merely be prolonged and yet the ultimate result would be the same.
It is the function of courts to put an end to litigation between parties, not to prolong it. With this end in view, we pass then to the merits of the actions which are raised in the demurrers to the special counts and are fully discussed in the briefs filed on both sides.
At the threshold of this phase of the case we are met with the contention that the trial court had no right, in considering the questions presented by the demurrers, to look to the terms of the final judgment entered by it in the previous contested election proceedings. There is no merit in this contention.
It is true that ordinarily a nisi prius court* will not take judicial notice of its records, judgments and orders in other and different cases or proceedings, even though such cases or proceedings may be between the same parties and in relation to the same subject matter. Wilson v. Wilson, 136 Va. 643, 650, 118 S. E. 270; 20 Am. Jur., Evidence, section 87, p. 105; 31 C. J. S., Evidence, section 50-c, pp. 623-4.
But it is a well-recognized exception to this rule, particularly in recent cases, that where the plaintiff refers *795to another proceeding or judgment, and specifically bases his right of action, in whole or in part, on something which appears in the record of the prior case, the court, in passing on a demurrer to the complaint, will take judicial notice of the matters appearing in the former case. See Frank v. Wilson & Co. (Del.), 32 A. (2d) 277, 280; Cogburn v. Collier, 213 Ala. 38, 104 So. 328, 330; Schneider v. Decker, 144 Okla. 213, 291 P. 80, 81; Stewart v. Phoenix Nat. Bank, 49 Ariz. 34, 64 P. (2d) 101, 104; Hobbs v. Hot Springs, 44 N. M. 592, 106 P. (2d) 856, 860; Slaughter v. Wright, C. C. A. 4, 135 F. (2d) 613, 615; 9 Wigmore on Evidence, 3d Ed., section 2579, p. 570, 1947 Supp., note 2.
The case now before us comes squarely within that principle, for in the special counts the plaintiffs not only refer to the final judgment in the election proceedings, which was entered in the same court,** but partly base their causes of action thereon, in that they allege that by virtue of such judgment the principal defendants’ certificates of election and right to said offices were “cancelled and annulled ab initio.”
On the merits, the argument in the briefs covers a wide range, but the issue is really a narrow one. The plaintiffs admit that under the principles laid down in DeShazo v. Davis, 157 Va. 517, 162 S. E. 320, 81 A. L. R. 614, and like cases, when the defendants, Anderson and Mullins, received from the proper authorities certificates of election, regular on their face, and duly qualified for the offices, they were prima facie entitled to enter the offices at the beginning of the term for which the certificates showed that they had been elected, to perform the duties, and to receive the emoluments thereof until the properly constituted authority had determined that they had not in fact been elected.
But, they say, when the Circuit Court of Dickenson county adjudicated in its order entered on July 9, 1946, that the election was void and that the defendants, Ander*796son and Mullins, had not been legally elected and were, therefore, not entitled to the certificates of election which had been previously issued to them, the effect of such adjudication was to reinstate the plaintiffs as de jure officers in the respective offices which they held at the time of the election. Having been reinstated, they say, as de jure officers, they were entitled, under section 33 of the Constitution, to discharge the duties as such until their successors, who were appointed by the Circuit Court of Dickenson county by the order entered on July 9, 1946, had qualified.
Moreover, the plaintiffs say, as such de jure officers they were entitled to recover of the defendants, Anderson and Muffins, who were at best only de facto officers, the salaries and emoluments received by the defendants during the time that the latter were in office.
It is, of course, well settled that, “Compensation for services performed by a public officer is an incident to the office and belongs to the de jure officer. Although he may not in some jurisdictions recover from the state or municipality which has paid the compensation to a de facto officer, he nevertheless has the right in most states upon establishing his title to the office to recover from the de facto officer whatever sums have been paid that officer by way of salary, fees, or emoluments, even though the latter may have performed the duties of the office pursuant to an erroneous judgment, or may have held a certificate of election and entered into the office in good faith. It is not a question of intention, but one of legal right to the compensation in dispute. The underlying principle is that the de facto officer before entering on the discharge of the duties of the office and receiving its emoluments is bound to know whether he has title. His position is the same as a person who, having a defective title to a tract of land, enters into possession and receives at his peril the rents and profits.” 43 Am. Jur., Public Officers, section 386, pp. 167, 168; Annotations: 93 A. L. R. 276, 151 A. L. R. 960.
As was said in Blair v. Marye, 80 Va. 485, 492, “The *797right to the salary follows the office, as shadow follows the substance.”
The principle was applied in Booker v. Donohoe, supra. But there the declaration alleged that the plaintiff had been lawfully elected for the term in controversy, and we held that if that allegation was true he was a de jure officer and entitled to the emoluments of the office. In the case before us there is no allegation that the plaintiffs were elected for the portion of the term in controversy, and the judgment entered in the contest proceedings showed that they were not.
It is thus apparent that the determinative issue is whether the adjudication in the contested election proceedings— that the two principal defendants, Anderson and Mullins, had not been legally elected to the respective offices for which they qualified for the term beginning January 1, 1944—had the effect, in the light of section 33 of the Constitution, of reinstating the plaintiffs in the two offices as de jure officers and entitling them to discharge the duties thereof during the time the defendants, Anderson and Mullins, held the offices under color of title.
The material portion of section 33 of the Constitution reads thus: * * All officers, elected or appointed, shall continue to discharge the duties of their offices after their terms of service have expired until their successors have qualified.”
Constitutional and statutory provisions of like import have frequently been before the courts for interpretation. There is authority for the position that under such provisions an incumbent, by reason of an invalid election, may hold over as a de jure officer beyond the term for which he was elected. See 43 Am. Jur., Public Officers, section 163, pp. 20, 21; Walker v. Hughes, 3 Terry (42 Del.) 447, 36 A (2d) 47, 151 A. L. R. 946.
A somewhat similar situation was involved in Chadduck v. Burke, 103 Va. 694, 49 S. E. 976, which was a proceeding involving title to the office of superintendent of the poor of Culpeper county. The incumbent, Chadduck, *798had been appointed to a new term by the judge of the county court, while Burke had been appointed for the same term by the circuit court. After reaching the conclusion that both appointments 'were invalid, in that they failed to meet the statutory requirements, this court held that Chadduck, by virtue of section 33 of the Constitution, continued in office until his successor had been appointed in a lawful manner and had qualified. In the course of the opinion this was said (103 Va., at pages 698, 699):
“The regular term of the plaintiff in error expired, under the law, on the 1st day of January, 1904, but he was just as fully authorized by law to hold the office and exercise the powers and perform the duties appertaining to it after that time, until his successor had been duly appointed and qualified, as he was before the expiration of his regular term. Indeed, the period between the expiration of his term and the qualification of his successor is as much a part of the incumbent’s term of office as the fixed statutory period, when the law provides that he shall hold until his successor qualifies.”
But as this court pointed out in Frantz v. Davis, 144 Va. 320, 131 S. E. 784, in the case of a contested election for county or city officers, different statutes from those construed in Chadduck v. Burke, supra, are involved.
In Frantz v. Davis, supra, Davis had been elected treasurer of the city of Roanoke in November, 1921, for the four-year term beginning January 1, 1922, and ending December 31, 1925. He was not a candidate at the election held in November, 1925, for the next succeeding term, at which Frantz, on the face of the returns, was elected. However, Frantz’s election was contested, and was adjudged null and void by the Corporation Court of the city of Roanoke on December 15, 1925. The council of the city of Roanoke; acting under Code, section 136, and section 39 of the city charter, unanimously elected Frantz treasurer of the city for the term beginning January 1, 1926, that is, the same term which he would have filled pursuant to the result of the election which was annulled.
*799Davis refused to turn over the office to Frantz, claiming that by virtue of section 33 of the Constitution, he (Davis) was entitled to hold over and to perform the duties of the office, and that hence there was no vacancy in the office which the city council could have legally filled.
We held that the principles announced in Chadduck v. Burke, supra, invoked by Davis, and likewise invoked by the plaintiffs here, did not apply.
This court, speaking through President Prentis, said, (144 Va., at page 325):
“* * * This case, however, requires the construction of different statutes from those which were construed in Chadduck v. Burke, supra. ■
“Here there was a contested election and at the very foundation of the question is Code, section 267, dealing with contested elections, which undertakes to provide for contingencies precisely like this—that is, where it has been adjudged that there has been no valid election of any person at the regular election. There is in that section no suggestion that the incumbent of the office whose term has expired is to hold over. On the contrary, there is in the language used the very clear indication that he is not to hold over for it is expressly provided that where it is determined that there ‘has been no valid election of any person, the proceeding shall be in conformity with section 136.’ Now section 136 does not, it is true, in terms refer to contested elections, but when an election is contested and annulled it is so clearly linked up with section 267, which specifically relates to contested election cases terminated as this was, that it is just as if there were but one section; just as if these two sections were combined. * *
We, therefore, concluded that Davis did not hold over beyond the term of his office, under section 33 of the Constitution, and that a vacancy in the office existed which had been properly filled by the appointment of Frantz.
In this latter connection it was said (144 Va., at pages 327, 328):
*800“There can be no donbt, we assume, that the General Assembly has ample power to declare when the office shall be deemed vacant, and for all the results of contested elections, because Constitution, section 56, expressly so provides in this language: ‘The manner of conducting and malting returns of elections, of determining contested elections, and of filling vacancies in office, in cases not specially provided for by this Constitution, shall be prescribed by law, and the General Assembly may declare the cases in which any office shall be deemed vacant where no provision is made for that purpose in this Constitution.’ ”
In the case now before us the Circuit Court of Dickenson county, after having decided in a proper proceeding that the purported election held on November 2, 1943, was null and void, proceeded under Code, section 136, as the order recites, to make the necessary appointments.
If the contention of the plaintiffs were sound, then it might be argued with considerable plausibility, as the incumbent claimed in the case of Frantz v. Davis, supra, that they held over for the full four-year term beginning January 1, 1944, and hence there was no vacancy which the lower court had the authority to fill.
We are of opinion that the trial court correctly held that under the authority of Frantz v. Davis, supra, the plaintiffs below—the prior incumbents—did not hold over after their terms of office had expired on December 31, 1943.
A consideration of the purpose and object of the constitutional provision strengthens the view that the conclusion of the trial court is sound.
As is said in 43 Am. Jur., Public Officers, section 164, p. 21, “The purpose of provisions authorizing public officers to hold over is to prevent a hiatus in the government pending the time when a successor may be chosen and inducted into office.”
“The extension is for the public benefit, and not to confer on the incumbent the right to continue.” Wardwell v. Leggat, 291 Mass. 428, 197 N. E. 164, 165.
*801The right to hold over is a contingent right designed to meet a public necessity. It becomes operative only when the necessity arises—that is, when there is no one with the present better right to occupy the office.
In the present case when Anderson and Mullins received certificates of election from the proper authorities and qualified for the respective offices, they had the prima facie right to retain the offices until their election was declared illegal in the manner provided by law. In the meanwhile the terms of their predecessors had expired.
It is true that some two years after the defendants had taken office their election was annulled and they became de facto officers for the period in which they were in office. But it is well settled that the acts of a de facto officer are valid as to third persons and the public. 3 Michie’s Digest of Virginia and West Virginia Reports, De Facto Officers, section 9, p. 591, and cases there cited.
“The rule which declares that the acts of an officer de facto are as valid and binding as if he were an officer de jure, is founded on the soundest principles of public policy, and is absolutely essential to the protection of the best interests of society.” McCraw v. Williams, 33 Gratt. (74 Va.) 510, 514.
Hence, while the defendants remained in office under color of authority, no necessity arose for the operation of the constitutional provision that the plaintiff incumbents should hold over and discharge the duties of the offices. There might have been no contest, or it might have terminated favorably for the defendants. In either of these events the defendants’ right to the offices would have been beyond question.
Moreover, when the offices became vacant upon the adjudication that the election was void, the vacancies were immediately filled, and hence there was no hiatus.
As the trial court tersely said in its opinion, “A mere retroactive right, such as is claimed by the plaintiffs, would benefit only the individuals who hope to profit therefrom. This is clearly not in keeping with the contem*802plated purpose of the Constitution. The mandate of the Constitution is for the purpose of operating when the necessity therefor arises, in the present for the benefit of the public, and any rights arising therefrom are coexistent with the present right to actually hold over the office. Since there was no hiatus in the office of treasurer and sheriff of Dickenson county by reason of the fact that these offices were' occupied and the functions thereof performed, respectively, by the defendants, Mullins and Anderson, as de jacto officers, the hold over provision of the Constitution never came into operation, and consequently the plaintiffs have no right upon which to base these actions.”
We are of opinion that the trial court correctly held that the special counts in the notices of motion failed to state causes of action, and hence the demurrers thereto were properly sustained.
For these reasons the judgments are
Affirmed.
A different rule applies to appellate courts. West v. Bromm Baking Co., 166 Va. 530, 536, 186 S. E. 291, 294, and cases there cited. See also, United States v. Pink, 315 U. S. 203, 216, 62 S. Ct. 552, 558, 559, 86 L. Ed. 796, 810.
By appropriate orders the final judgment in the election proceedings was made a part of the record in each of the present cases.