3

Ok, I have this prototype that was written by someone else in C# and I'm trying to put it into Objective-C. Now, I haven't had any formal experience with C# yet, so I don't know everything about it yet. I understand what the first three variables are, but I'm running into problems with what the fourth and fifth lines (c_data) are doing. Is the fourth declaring a method and then the fifth defining it or what's happening? Thanks for your help!

public class c_data {
    public double value;
    public int label;
    public int ID;
    public c_data() { }
    public c_data(double val) {
        value = val;
    }
}
Quinn Taylor
  • 44,553
  • 16
  • 113
  • 131
Josh Bradley
  • 4,630
  • 13
  • 54
  • 79
  • 1
    That is some mighty sad looking C# code there. I hope you do better with naming and formatting when you convert it. – Michael Meadows Jul 13 '09 at 15:20
  • You're telling me. I have a ton of code just like this that I have to convert within a week. Long gone are the nights of peaceful sleep and here come the all-nighters! – Josh Bradley Jul 13 '09 at 15:46
  • I agree and this is probably an example from a text book. Which is why so many programming books are horrible. If you wouldn't do it in real code then don't teach someone how to do it in fake code 'cause in 5 years you'll have a bunch of code that looks like your examples... I'm talking to you Microsoft and the myStupidPrefix. –  Jul 13 '09 at 18:11

7 Answers7

16

The fourth and fifth lines are constructors in C#. They are the equivalent to [[c_data alloc] init] chains in objective-c. C# allows you to overload constructors based on the parameters they take. This is equivalent to having two different initialization methods in Objective-C:

@interface CData : NSObject
{
   double value;
   int label;
   int ID;
}

@property double value;
@property int label;
@property int ID;

-(id) init;
-(id) initWithValue:(double)value;

@end
Quinn Taylor
  • 44,553
  • 16
  • 113
  • 131
LBushkin
  • 129,300
  • 32
  • 216
  • 265
  • Thanks so much...I would have been fumbling around this for another hour if it wasn't for you. – Josh Bradley Jul 13 '09 at 15:51
  • (Changed init return types to "id" and declared to extend NSObject.) – Quinn Taylor Jul 13 '09 at 18:07
  • Good answer, particularly from the Objective-C porting standpoint. It's also a good idea to obey the naming conventions of the destination language (e.g. "CData" instead of "c_data" for a class name) when porting. – Quinn Taylor Jul 13 '09 at 18:11
  • Of course, the C# code never obeyed the source language's conventions (it goes far beyond naming). :( – Michael Meadows Jul 13 '09 at 18:15
  • Thanks. I originally excluded some of these conventions to make it easier to highlight the essential porting differences. But it's worth having a nice clean version to highlight. I will mention that in some instances it is easier to port code that breaks conventions if it allows you to avoid having to do global renaming or refactoring. I don't know if that applied in this situation - but I suspect the OP is in a better position to decide that. – LBushkin Jul 13 '09 at 21:21
2

The 4th line is a parameterless constructor and the 5th line is a parameterfull constructor.

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
2

The first c_data is a default no-args constructor which initialises the structure's fields to default values (value -> 0.0, label -> 0, ID -> 0) and the second c_data is a constructor which sets the value field of the instance to the passed-in parameter val and the other fields to their default values. What I've described is how those two constructor calls initialise the instance.

Vinay Sajip
  • 95,872
  • 14
  • 179
  • 191
1

4th and 5th are constructors that are used to initialize the instance of c_data when you new up one.

Mehmet Aras
  • 5,284
  • 1
  • 25
  • 32
1

The fourth is defining a constructor for the class which takes no parameters and has no actions, and the fifth is defining a constructor for the class which takes as a parameter a double value and which sets the class-internal member variable value to the passed value val.

Paul Sonier
  • 38,903
  • 3
  • 77
  • 117
1

The 4th and 5th lines are both constructors.

The 4th line one is the "default" constructor, which in this case does not initialize any variables.

The constructor on the 5th line sets the variable named value to the parameter passed in.

nikmd23
  • 9,095
  • 4
  • 42
  • 57
0

If I may, it is rather like the having both the following methods in an Objective-C class:

  • (id)init;
  • (id)initWithNumber:(NSNumber *)number;

Constructors and initializers are analogues, they just look a little different.

Jonathan Sterling
  • 18,320
  • 12
  • 67
  • 79