Solved

Convert CPP to C#

Posted on 2014-04-12
6
515 Views
Last Modified: 2014-04-12
How do I convert the following to C#

struct fpoint : pair<double> {
    fpoint() : pair<double>() { }
    fpoint(double x, double y) : pair<double>(x, y) { }
};
0
Comment
Question by:JElster
  • 2
  • 2
  • 2
6 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 100 total points
ID: 39996739
The C# equivalent could be

public struct fpoint : System.Collections.Generic.KeyValuePair<double,double> {
    fpoint() : base() { }
    fpoint(double x, double y) : base((x, y) { }
}; 

Open in new window

0
 
LVL 1

Author Comment

by:JElster
ID: 39996749
Get a bunch of compile errors,, like missing param.
Probably close... thx
0
 
LVL 86

Expert Comment

by:jkr
ID: 39996758
Soory, had my mind tilted towards C++ a bit too much - does

public struct fpoint : System.Collections.Generic.KeyValuePair<double,double> {
    fpoint() : base() { }
    fpoint(double x, double y) : base() { key = x; value = y; }
}; 
                                            

Open in new window


work better?
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Author Comment

by:JElster
ID: 39996764
Error      3      Structs cannot contain explicit parameterless constructors      

Error      2      Type 'System.Collections.Generic.KeyValuePair<...>' in interface list is not an interface
0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 400 total points
ID: 39996766
I disagree slightly, but I don't think it's of major concern. From what I read, the Tuple class would be closer to the exact match for pair.

e.g.

public class fpoint : Tuple<double, double>
{
    public fpoint() : base(default(double), default(double)) { }
    public fpoint(double x, double y) : base(x, y) { }
}

Open in new window


The reason I disagree is that in C# you cannot reassign struct members once you have created the instance--you have to create a second instance with the new values. This is because KeyValuePair is a value type. A Tuple is a reference type, so you can reassign its member even after the instance is created. Albeit in either case there is no swap method, so you'd need to roll your own.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 39996770
Actually, never mind. The Tuple's members are read-only also. I saw "tuple" mentioned in the documentation I linked to, so I naturally thought it would be the equivalent. I think either data structure would yield the same results.

Of course, you could always write your own class/struct to be an exact match!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now