Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code from C# to Objective-C for Constructors

We have to convert our C# code to Objective-C code and i was having a hard time figuring out how to create one constructor with no arguments while the other with 2 arguments. This is the C# code that I am trying to convert:

     namespace Account
{
class Program
{

    public class Account
    {

        private double balance;
        private int accountNumber;

        public Account()
        {
            balance = 0;
            accountNumber = 999;
        }

        public Account(int accNum, double bal)
        {
            balance = bal;
            accountNumber = accNum;
        }
        }
}

}

And this is what I have so far for the Objective C not sure if it is even correct or not

     @interface classname : Account 
   {
@private double balance;
@private int accountNumber;

@public Account()
   }

Open to any help i can get thank you very much, Danny

like image 422
Danny Duberstein Avatar asked Sep 09 '26 15:09

Danny Duberstein


1 Answers

you simply provide two initializers, which takes the general form:

@interface MONAccount : NSObject
@private
    double balance;
    int accountNumber;
}

/* declare default initializer */
- (id)init;

/* declare parameterized initializer */
- (id)initWithAccountNumber:(int)inAccountNumber balance:(int)inBalance;

@end

@implementation MONAccount

- (id)init
{
    self = [super init];
    /* objc object allocations are zeroed. the default may suffice. */
    if (nil != self) {
        balance = 0;
        accountNumber = 999;
    }
    return self;
}

- (id)initWithAccountNumber:(int)inAccountNumber balance:(int)inBalance
{
    self = [super init];
    if (nil != self) {
        balance = inBalance;
        accountNumber = inAccountNumber;
    }
    return self;
}

@end
like image 138
justin Avatar answered Sep 12 '26 05:09

justin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!