Page 1 of 2 12 LastLast
Results 1 to 10 of 17
  1. #1
    Join Date
    Jan 2010
    Location
    Maumee, Ohio
    Posts
    55

    Putting it all together in C#?

    Anybody ever had a hard time putting all the C# that you learn on the tutorials and putting it all together in your programming, i think i am going to have that problem.

    Cid

  2. #2
    Join Date
    Jun 2012
    Location
    probably at a PC
    Posts
    309
    At first, I'm sure everybody has that problem.

    Just like most other programmers will tell you, I'd have to say; The only thing to get past it, is practice, practice, practice. Just keep doing small projects and if you can't think of anything yourself, just ask, we can always try to suggest little projects to do. It took me a while before I truly started to not just understand what is written somewhere, but actually start using it.

    Keep in mind, with programming the only way to get good at it is; Keep doing it. The exercise of typing code is the most important. Understanding and applying new concepts is a natural result of actively programming, not the learning/lessons part of it. Experience, makes the programmer, not just knowledge. Eventually there will be a click in your head and you'll understand something better than you did when you first learned it. That's when you start using it.

    Just keep at it. It will all fall into place after a while, even if it may not seem that way.


    PS: If there's something specific you're scratching your head over, or want to practice how to use, you can also post about that. I'm sure we'll be able to find some great exercises for you or help clarify something you're struggling with.
    Last edited by nGAGE; 07-31-2013 at 10:50 PM.

  3. #3
    Join Date
    Jan 2010
    Location
    Maumee, Ohio
    Posts
    55
    I was thinking on my first project is create an AddressBook that can hold in memory and able to keep adding people to it, if this is to hard for me and should try something smaller please let me know... How small should i go?

  4. #4
    Join Date
    Jun 2013
    Posts
    36
    Creating an address book is alot more difficult than you might imagine.
    Before you begin the project you need to ask a few questions,
    1. Will this be a multi user project, meaning others can access the address book.
    2. How do i want to handle the data? msql ?, actual file, XML, (Any numerous other method here) ?
    3. If this is a multi user project how will the users access updated data recently entered by other users ?
    4. If i handle my data with mysql, then i can always make the project multi user later should i?

    These questions can go on, i just hit the first roadblock, if i were you and wanted to eventually write
    a database type program, i would learn all i can about xml first.

    So maybe your first project should be an xml parser ?
    just my nickels worth.

  5. #5
    Join Date
    Jun 2012
    Location
    probably at a PC
    Posts
    309
    Considering that he's was saying 'hold in memory', I will boldly assume that he just wants to write a little console-program with a menu to add/remove and view contact-details, but not necessarily save anything to disk or database. Which would mean; that after you exit the program, the next time you run the program, all the data (contact-details from last time) would be gone, just to be clear!

    Before I jump to more conclusions here though, it might be good to ask what your current progress with the studies/tutorials is. Which tutorials/classes have you gone through? And what have you done so far?

    If you could quickly get us an overview on those, we'll be better equipped in suggesting how to proceed.

  6. #6
    Join Date
    Jan 2010
    Location
    Maumee, Ohio
    Posts
    55
    going through introduction in cannon 2 , right now i am at the video ListGenericClass.

  7. #7
    Join Date
    Jun 2012
    Location
    probably at a PC
    Posts
    309
    Meh... I never did those videos, sry. So not entirely sure what's been covered so far. Anybody else familiar with the content in those vids?

    Well,... in the meantime; Have you ever dealt with open/reading/saving files? Would be nice if you could start such a project with at least a very basic means of reading/writing text-files, so you could at least store/load some contacts from/to file.

    If not, I'm sure we could quickly find one of the videos that covers it. That is, if you would like to be able to save/load contacts.

  8. #8
    Join Date
    Jun 2013
    Posts
    36
    Hey again !! I think for a first timer, saving data in a XML format is the best thing to learn, its not all that difficult
    and, as you learn more about xml, you can get really creative.

    Below is a quick code example showing how to use xml, in a very easy manner

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    using System.Diagnostics;
    using System.IO;
    
    namespace XmlStuff
    {
        public static class XMLManager
        {
            // Get This process
            private static Process CurrentProcess = Process.GetCurrentProcess();
            // get the path that we want to save and read our xml from.
            private static string Config = Path.Combine(Path.GetDirectoryName(CurrentProcess.MainModule.FileName), "AddressBook.xml");
    
            /// <summary>
            /// We try and initialize our xml file, meaning
            /// in a try/catch block so we dont crash the program, 
            /// we attempt to load our addressbook.xml, if for some reason
            /// the file doesnt exsist, because we put it in a try/catch
            /// block, instead of crashing, it will drop down to the 
            /// catch{} area, and execute the code there.
            /// Depending on where this executable is run from, 
            /// you may need admin privliges to write.
            /// (Dont run this from C:\Program Files\*)
            /// </summary>
            public static void Initialize()
            {
                try
                {
                    XmlDocument Doc = new XmlDocument();
                    Doc.Load(Config);
                }
                catch
                {
                    // We dont have a addressbook.xml, lets create it
                    CreateXML();
                }
            }
            /// <summary>
            /// Writes a very basic empty AddressBook.xml
            /// </summary>
            static void CreateXML()
            {
                XDocument doc = new XDocument(
                    new XDeclaration("1.0", "utf-8", "yes"),
                    new XComment("AddressBook"),
                    new XElement("ROOT",
                        new XElement("Entries")));
                // Save it
                doc.Save(Config);
            }
            /// <summary>
            /// Adds a entry with firstname, lastname and address to 
            /// our xml file
            /// </summary>
            /// <param name="firstName"></param>
            /// <param name="lastName"></param>
            /// <param name="address"></param>
            /// <returns></returns>
            public static bool AddEntry(string firstName, string lastName, string address)
            {
    
                try
                {
                        XDocument doc = XDocument.Load(Config);
                        var feed = doc.Root.Element("Entries");
                        feed.Add(new XElement("Entry",
                            new XElement("Firstname", firstName),
                            new XElement("Lastname", lastName),
                            new XElement("Address", address)));
                        doc.Save(Config);
                        return true;
                }
                catch
                {
                    return false;
                }
            }
            /// <summary>
            /// Removes a entry from our xml file
            /// </summary>
            /// <param name="firstName"></param>
            /// <param name="lastName"></param>
            /// <param name="address"></param>
            /// <returns></returns>
            public static bool RemoveEntry(string firstName, string lastName, string address)
            {
                try
                {
                    XDocument doc = XDocument.Load(Config);
                    XElement toRemove = doc.Descendants("Entries")
                     .Descendants("Entry")
                     .Where(x => x.Element("Firstname").Value == firstName)
                     .Where(x => x.Element("Lastname").Value == lastName)
                     .Where(x => x.Element("Address").Value == address)
                     .First<XElement>();
                    toRemove.Remove();
                    doc.Save(Config);
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }
    }
    So if you wanted to use this code, first you use
    XMLManager.Initialize();

    and now the file exsits to write to, even if it did not before.

    Then to add a entry
    if (XMLManager.AddEntry("MyFirstName","MyLastName", "MyFullAddress"))
    {
    // It worked !!
    }

  9. #9
    Join Date
    Mar 2013
    Location
    Australia
    Posts
    217
    Hi cid, I know what your feeling as i went through it to, I had a passion for programming and didn't get any official training. I have to teach myself. I started with a could of books but i find it hard to finish books. Something about it being a several day process that makes me drift away from it. I also started looking at video tutorials because i found i could understand the concepts quicker and easier. The one resource that helped me a huge amount was this training http://www.pluralsight.com. There programming video's are amazing, they take the time to explain the concepts before walking you through a demo. Its important that you try to walk through it with them and that's the handy thing about video's, you can pause and rewind them.

    Start with their beginners c# tutorials and i think you'll go a long way in understanding it. The you could start targeting specific technology like WPF for windows applications. After this you could try to improve your programing skills, they have a great section on design patterns and best practices. These will help you right better code, the first application i wrote was when i was learning and it was designed so badly it became very difficult to manage and add to, its probably going to happen to you but its a great learning process. As the other's mentioned it does come down to practice. Don't give up hope, i now work as a .net developer, if i can do it so can you.

  10. #10
    Join Date
    Mar 2013
    Location
    Australia
    Posts
    217
    Quote Originally Posted by Pactor View Post
    Hey again !! I think for a first timer, saving data in a XML format is the best thing to learn, its not all that difficult
    and, as you learn more about xml, you can get really creative.
    I don't know about that, i think there's a bit of complexity for someone trying to wrap there head around the language let alone learning parts of the framework.

    I would recommend using something like Json.net to serialize a plane old object into a string and then write it to a file.

    1. Firstly install the NuGet extension for visual studio (you will find this tool very useful), it helps you download and manage third party references. http://docs.nuget.org/docs/start-here/installing-nuget
    2. Right click the project in the solution explorer and select manage NuGet packages. Search online for JSON.net and install (http://docs.nuget.org/docs/start-her...ing-the-dialog). The project and documentation can be found here http://json.codeplex.com/

    now you have Json.net reference you can start to use it. here is an example of how to save and read from the hard disk by storing the data in JSON format. I tried to keep it simple.

    Code:
    using System.Collections.Generic;
    using System.IO;
    using Newtonsoft.Json; //make sure you tell c# you want to use the Json.Net reference we added
    namespace ConsoleApplication4
    {
        class Program
        {
            public static  List<Contact> ContactList { get; set; }
            static void Main(string[] args)
            {
                ContactList = new List<Contact>(); //Initialise the List of contacts
    
                AddContacts();
                SaveContactList(); //Save the contacts list
                ContactList.Clear(); //clear the contacts list
                ContactList = ReadContactList(); //load the contacts list from the hard drive.
            }
    
            private static void AddContacts()
            {
                //the curly braces after newing an object is just shorthand for initialising the properties of the object
                var contactOne = new Contact() {FirstName = "Ryan", LastName = "Smith"};
                var contactTwo = new Contact() { FirstName = "Bob", LastName = "Jones" };
                ContactList.Add(contactOne);
                ContactList.Add(contactTwo);
            }
    
            private static List<Contact> ReadContactList()
            {
                //Read the text from the file
                var jsonText = File.ReadAllText(@"C:\Temp\ContactList.txt");
                //convert the text back into an object, specify the type of object, in this case a List of Contact classes
                var contactList = JsonConvert.DeserializeObject<List<Contact>>(jsonText);
               
                return contactList;
            }
    
            private static void SaveContactList()
            {
                //Convert the list of contacts into formated text (the format is called JSON, JavaScript Object Notation)
                var jsonText = JsonConvert.SerializeObject(ContactList);
    
                //write the text to a file.
                //Note: this will through an exception if the path does not exist or there are access problems. To keep it simple i won't handle these conditions
                File.WriteAllText(@"C:\Temp\ContactList.txt", jsonText);
            }
        }
    
        public class Contact
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
        }
    }
    Now try to expand this. Write it to the console then try to create a GUI for it.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •