简体   繁体   中英

Doesn't exist in current context?

I have the following code and can't for the life of me figure out why it's giving me the error calling the method SeeAllTweeters(); Here's the code and thanks in advance for the help!

TweetManager.cs

namespace Assign3_Twitter
{
    public class TweetManager
    {
        private List<Tweet> tweets = new List<Tweet>();

         TweetManager()
        {
            Tweet tw1 = new Tweet ("Austen", "Hello World!", "#Hey", "Twitter");
            tweets.Add (tw1);
            Tweet tw2 = new Tweet ("Test1", "Hello World! x2", "#Howdy", "Tweeter");
            tweets.Add (tw2);
            Tweet tw3 = new Tweet ("Test2", "Hello World! x3", "#Hey", "Twitter");
            tweets.Add (tw3);
            Tweet tw4 = new Tweet ("Test3", "Hello World! x4", "#Howdy", "Tweeter");
            tweets.Add (tw4);
            Tweet tw5 = new Tweet ("Test4", "Hey there!", "#Hey", "Twitter");
            tweets.Add (tw5);
            Tweet tw6 = new Tweet ("Test5", "Woah this is cool!", "#Howdy", "Tweeter");
            tweets.Add (tw6);

        }

        public void SeeAllTweeters()
        {
            foreach (Tweet Tweets in tweets)
            {
                Console.WriteLine(Tweets);
            }
        }

Tweet.cs

namespace Assign3_Twitter
{
    public class Tweet
    {
        public string HashTag { get; private set; }
        public string Message { get; private set; }
        public string Sender { get; private set; }
        public string Recipient { get; private set; }
        public Tweet (string sender, string message, string hashtag, string reciepient)
        {
            this.Sender = sender;
            this.HashTag = hashtag;
            this.Message = message;
            this.Recipient = reciepient;
        }

        public override string ToString ()
        {
            return string.Format ("[Tweet: HashTag={0}, Message={1}, Sender={2}, Recipient={3}]", HashTag, Message, Sender, Recipient);
        }
    }
}

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Assign3_Twitter
{   
    class MainClass
    {
        public static void Main (string[] args)
        {
            Tweet tw100 = new Tweet("Austen", "Hey guys!", "awesome", "Meee");
            SeeAllTweeters();
        }
    }
}

The SeeAllTweeters method is defined on the TweetManager class, so you can only call it with a reference to an instance of that class. For example:

class MainClass
{
    public static void Main (string[] args)
    {
        TweetManager tweetManager = new TweetManager();
        tweetManager.SeeAllTweeters();
    }
}

Further Reading

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM