const

S

shapper

Hello,

I have a class, TweetService, with many methods which all use a
Twiiter object:

public class TwitterService : ITwitterService {

private const Twitter twitter = new Twitter("myusername",
"mypassword");

public void Create(Tweet tweet) {
TwitterStatus status = twitter.Status.Update
(tweet.Text);
} // Create

// Other methods
}

Is it correct to define it as a const?
Or should I just define it as follows:

private Twitter twitter;

And then initialized it on the TwitterService constructor:

public TwitterService() {
twitter = new Twitter("myusername", "mypassword");
} // TwitterService

Or some other way?

I just want twitter to be available to all methods and not having to
define it all the time and having the username and password every
where.

Thanks,
Miguel
 
F

Family Tree Mike

shapper said:
Hello,

I have a class, TweetService, with many methods which all use a
Twiiter object:

public class TwitterService : ITwitterService {

private const Twitter twitter = new Twitter("myusername",
"mypassword");

public void Create(Tweet tweet) {
TwitterStatus status = twitter.Status.Update
(tweet.Text);
} // Create

// Other methods
}

Is it correct to define it as a const?
Or should I just define it as follows:

private Twitter twitter;

And then initialized it on the TwitterService constructor:

public TwitterService() {
twitter = new Twitter("myusername", "mypassword");
} // TwitterService

Or some other way?

I just want twitter to be available to all methods and not having to
define it all the time and having the username and password every
where.

Thanks,
Miguel

I believe you must use the second option, as const cannot be used on
references. Didn't you get an error trying the first method?

Mike
 
P

Peter Duniho

Hello,

I have a class, TweetService, with many methods which all use a
Twiiter object:

public class TwitterService : ITwitterService {

private const Twitter twitter = new Twitter("myusername",
"mypassword");

// Other methods
}

Is it correct to define it as a const?

Try compiling it and see.
Or should I just define it as follows:

private Twitter twitter;

Do you want a different value for the variable for each instance of
TwitterService? If so, the above is exactly right. If not, you should
make the value "static".
And then initialized it on the TwitterService constructor:

public TwitterService() {
twitter = new Twitter("myusername", "mypassword");
} // TwitterService

If it's an instance member, you can initialize it using the initializer
syntax you showed originally.

Pete
 
B

Ben Voigt [C++ MVP]

shapper said:
Hello,

I have a class, TweetService, with many methods which all use a
Twiiter object:

public class TwitterService : ITwitterService {

private const Twitter twitter = new Twitter("myusername",
"mypassword");

public void Create(Tweet tweet) {
TwitterStatus status = twitter.Status.Update
(tweet.Text);
} // Create

// Other methods
}

Is it correct to define it as a const?

No. C# "const" is pretty different from what const means in other
languages. I think what you really want is "static readonly".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top