-2

In a C# method I Had:

 var followers = user.GetFollowers(250);
 var friends = user.GetFriends(250);
 var favorites = user.GetFavorites(40);

How can I make these variables accessible to other methods, within my Windows Form App?

I have tried:

 private string followers = user.GetFollowers(250);
 private string friends = user.GetFriends(250);
 private string favorites = user.GetFavorites(40);

And:

private followers = user.GetFollowers(250);
private friends = user.GetFriends(250);
private favorites = user.GetFavorites(40);

I have tried putting the above examples, right at the top of my code, but this does not work. What could I be doing wrong. I'm new to programming.

  • where is the other method? – Sajeetharan Mar 21 '15 at 19:18
  • Do you have access to the fields or are they empty? can you post your class – skyfoot Mar 21 '15 at 19:19
  • The method would be: `code` capture() { var favorites = user.GetFavorites(40); foreach (fav in Favorites) {fav.text} } `code` – ForensicIT Mar 21 '15 at 19:22
  • I would like variable favourites with its info (user.GetFollowers(250);) to be accessible throughout my program. – ForensicIT Mar 21 '15 at 19:27
  • So i dont have to repeat var favorites1 = user.GetFavorites(40); var favorites2 = user.GetFavorites(40); var favorites3 = user.GetFavorites(40); for every new method that uses favorites – ForensicIT Mar 21 '15 at 19:28
  • what is the return type of GetFavorites – Sajeetharan Mar 21 '15 at 19:30
  • 3
    Since your question is so _very_ basic about object orientated programming, i'd suggest you read some basic literature. – TGlatzer Mar 21 '15 at 20:04
  • possible duplicate of [Is it possible to have a "var" as a Global variable](http://stackoverflow.com/questions/14572690/is-it-possible-to-have-a-var-as-a-global-variable) – Eugene Podskal Mar 21 '15 at 20:39
  • Please see ["Should questions include “tags” in their titles?"](http://meta.stackexchange.com/questions/19190/should-questions-include-tags-in-their-titles), where the consensus is "no, they should not"! –  Mar 21 '15 at 20:44
  • https://twitter.com/ExpertBeginner1/status/578612657835507712 –  Mar 21 '15 at 20:47

1 Answers1

1

It all depends on what the return type of the user.GetFollowers(int i) is. And in what context is user defined?

You seem to be a bit confused about the basics of C#. private is simply specifying that you cannot access your member outside the context of your class. It is not a data-type. See this question for more info about access-modifiers as they are called
What is the difference between Public, Private, Protected, and Nothing?

The keyword var is just compliler-magic, and cannot be used for properties or other members of a class.

To implement a member of class (be it a field, property or method) you must know the return-type.

In your case; the easiest way to get it is just to see what user.GetFollowers(int i) returns, the fastest way to do this is to simply browse to it by putting your cursor on it and go to it by pressing the F12 key in visual studio.

You have tagged your question with so I'm going to assume this has something to do with twitter.

For this example, i will just call the unknown types FriendCollection, FollowerCollection and FavoriteCollection. Since "GetFriends" seems to suggest that it will return some sort of collection.

public class TwitterUserInfo
{
    public FriendCollection Friends { get; get; }
    public FollowerCollection Followers { get; set; }
    public FavoriteCollection Favorites { get; set; }

    public TwitterUserInfo(TwitterUser user)
    {
        Friends = user.GetFriends(20);
        Followers = user.GetFollowers(20);
        Favorites = user.GetFavorites(20);
    }
}

You can then use it as such:

TwitterUserInfo userInfo = new TwitterUserInfo(someTwitterUser);

And the "userInfo" will then contain the properties you want. for example userInfo.Friends will contain the friends.

Since you have not provided much information about what is going on, i cannot give a more elaborate answer.

EDIT: Cleared up a few things

Community
  • 1
  • 1
mausworks
  • 1,607
  • 10
  • 23