Click here to Skip to main content
15,888,610 members
Home / Discussions / C#
   

C#

 
AnswerRe: code smell ? C# public class with everything in it declared 'static Pin
englebart6-Mar-23 17:22
professionalenglebart6-Mar-23 17:22 
QuestionRunning MSBuild Commands from My C# console application. Pin
Madhurima Dutta7-Feb-23 4:56
Madhurima Dutta7-Feb-23 4:56 
AnswerRe: Running MSBuild Commands from My C# console application. Pin
Dave Kreskowiak7-Feb-23 5:18
mveDave Kreskowiak7-Feb-23 5:18 
AnswerRe: Running MSBuild Commands from My C# console application. Pin
jschell7-Feb-23 9:10
jschell7-Feb-23 9:10 
GeneralRe: Running MSBuild Commands from My C# console application. Pin
Madhurima Dutta8-Feb-23 4:46
Madhurima Dutta8-Feb-23 4:46 
GeneralRe: Running MSBuild Commands from My C# console application. Pin
jschell8-Feb-23 5:07
jschell8-Feb-23 5:07 
QuestionSystem.InvalidCastException: 'Unable to cast object of type 'System.Collections.Generic.List`1[FirstMVCApplication.Models.Products]' to type 'FirstMVCApplication.Models.Products'.' Pin
Member 136517316-Feb-23 21:41
Member 136517316-Feb-23 21:41 
AnswerRe: System.InvalidCastException: 'Unable to cast object of type 'System.Collections.Generic.List`1[FirstMVCApplication.Models.Products]' to type 'FirstMVCApplication.Models.Products'.' Pin
Eddy Vluggen7-Feb-23 1:13
professionalEddy Vluggen7-Feb-23 1:13 
AnswerRe: System.InvalidCastException: 'Unable to cast object of type 'System.Collections.Generic.List`1[FirstMVCApplication.Models.Products]' to type 'FirstMVCApplication.Models.Products'.' Pin
Gerry Schmitz7-Feb-23 13:26
mveGerry Schmitz7-Feb-23 13:26 
Questionxtendd Pin
PV20235-Feb-23 20:43
PV20235-Feb-23 20:43 
AnswerRe: xtendd Pin
OriginalGriff5-Feb-23 20:44
mveOriginalGriff5-Feb-23 20:44 
GeneralRe: xtendd Pin
PV20235-Feb-23 20:48
PV20235-Feb-23 20:48 
GeneralRe: xtendd Pin
Eddy Vluggen7-Feb-23 1:16
professionalEddy Vluggen7-Feb-23 1:16 
GeneralRe: xtendd Pin
RedDk7-Feb-23 7:05
RedDk7-Feb-23 7:05 
QuestionHow do I take datatable values into postgresql Pin
Member 141039872-Feb-23 8:22
Member 141039872-Feb-23 8:22 
AnswerRe: How do I take datatable values into postgresql Pin
Gerry Schmitz2-Feb-23 9:08
mveGerry Schmitz2-Feb-23 9:08 
GeneralRe: How do I take datatable values into postgresql Pin
jschell3-Feb-23 5:23
jschell3-Feb-23 5:23 
GeneralRe: How do I take datatable values into postgresql Pin
Gerry Schmitz3-Feb-23 7:10
mveGerry Schmitz3-Feb-23 7:10 
AnswerRe: How do I take datatable values into postgresql Pin
Dave Kreskowiak2-Feb-23 9:11
mveDave Kreskowiak2-Feb-23 9:11 
AnswerRe: How do I take datatable values into postgresql Pin
Richard MacCutchan2-Feb-23 21:40
mveRichard MacCutchan2-Feb-23 21:40 
AnswerRe: How do I take datatable values into postgresql Pin
jschell3-Feb-23 5:30
jschell3-Feb-23 5:30 
GeneralRe: How do I take datatable values into postgresql Pin
Member 141039873-Feb-23 21:33
Member 141039873-Feb-23 21:33 
GeneralRe: How do I take datatable values into postgresql Pin
Dave Kreskowiak4-Feb-23 5:40
mveDave Kreskowiak4-Feb-23 5:40 
AnswerRe: How do I take datatable values into postgresql Pin
Richard Deeming5-Feb-23 22:52
mveRichard Deeming5-Feb-23 22:52 
Question18 Errors Needs to be Fixed Pin
Michael Afolayan30-Jan-23 21:46
Michael Afolayan30-Jan-23 21:46 
Hello, Profs

Please, I need help in my c# journal code as I find it difficult to get rid of the errors.
Below is the sample of my c# code and I will appreciate if the errors could be fixed.
Thanks.



using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Journal
{
    class Program
    {
        static void Main(string[] args)
        {
            Journal journal = new Journal();

            while (true)
            {
                Console.WriteLine("1. Write a new entry");
                Console.WriteLine("2. Display journal");
                Console.WriteLine("3. Save journal");
                Console.WriteLine("4. Load journal");
                Console.WriteLine("5. Quit");

                Console.Write("Enter your choice: ");
                int userChoice = Convert.ToInt32(Console.ReadLine());

                if (userChoice == 1)
                {
                    string chosenPrompt = journal.GetRandomPrompt();
                    Console.WriteLine("Prompt: " + chosenPrompt);
                    Console.WriteLine("Write your response:");
                    string response = Console.ReadLine();

                    Entry newEntry = new Entry
                    {
                        Date = DateTime.Now,
                        Prompt = chosenPrompt,
                        Response = response
                    };

                    journal.AddEntry(newEntry);
                }
                else if (userChoice == 2)
                {
                    journal.DisplayEntries();
                }
                else if (userChoice == 3)
                {
                    Console.WriteLine("Enter a filename to save journal:");
                    string filename = Console.ReadLine();
                    journal.SaveJournal(filename);
                }
                else if (userChoice == 4)
                {
                    Console.WriteLine("Enter a filename to load journal:");
                    string filename = Console.ReadLine();
                    journal.LoadJournal(filename);
                }
                else if (userChoice == 5)
                {
                    break;
                }
                else
                {
                    Console.WriteLine("Invalid choice. Try again.");
                }
            }
        }
    }

    class Entry
    {
        public DateTime Date { get; set; }
        public string Prompt { get; set; }
        public string Response { get; set; }
    }

    class Journal
    {
        private List<string> prompts = new List<string>
        {
            "What did you learn today?",
            "What are you grateful for?",
            "What did you accomplish today?",
            "What challenges did you face today?",
            "What are your goals for tomorrow?"
        };
        private List<Entry> entries = new List<Entry>();

        public string GetRandomPrompt()
        {
            Random rand = new Random();
            int index = rand.Next(prompts.Count);
            return prompts[index];
        }

        public void AddEntry(Entry entry)
        {
            entries.Add(entry);
        }

        public void DisplayEntries()
        {
            Console.WriteLine("Journal Entries:");
            foreach (Entry entry in entries)
            {
                Console.WriteLine("Date: " + entry.Date);
                Console.WriteLine("Prompt: " + entry.Prompt);
                Console.WriteLine("Response: " + entry.Response);
                Console.WriteLine("----------------");
            }
        }

        public void SaveJournal(string filename)
        {
            using (Stream stream = File.Open(filename, FileMode.Create))
            {
                BinaryFormatter binaryFormatter = new BinaryFormatter();
                binaryFormatter.Serialize(stream, entries);
            }
        }

        public void LoadJournal(string filename)
        {
            if (File.Exists(filename))
            {
                using (Stream stream = File.Open(filename, FileMode.Open))
                {
                    BinaryFormatter binaryFormatter = new BinaryFormatter();
                    entries = (List<Entry>)binaryFormatter.Deserialize(stream);
                }
            }
            else
            {
                Console.WriteLine("File does not exist.");
            }
        }
    }
}

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.