7 Replies - 1199 Views - Last Post: 31 January 2011 - 01:00 PM Rate Topic: -----

#1 Eqric   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 31-January 11

c to c# , need help

Posted 31 January 2011 - 10:54 AM

Hi, started yesterday to learn csharp on my free time, i know some C and SML so i am kinda trying to converting to c# and i need some help.

Whats the access level on public? If i make something "public", will that function be accessible for my hole project?

I also notice sometimes that the program, right after using the Console.ReadLine() and then come back to it by maybe a while loop, it skips to use ReadLine() one times then the next time it work again.

Last question, why doesn't this work? It seems to be logical right for me , what am i missing?
The output when i run this is:
Menu
A to chose ...
B to chose ...
input: A
Enter the name:
Enter the sec-num:

It seems that it skips my Read commands at line 68...

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;



class ProgramMain
{
    public static void Main()
    {
        DatabaseProgram db = new DatabaseProgram();
        db.menu();
    }
}

 class DatabaseProgram
{
    int count = 0;

    struct database
    {
        public string name;
        public long number;
    }

    database[] db = new database[50];
    public void menu()
    {
        char choise = 'm';
        while (choise != 'Q')
        {

            Console.WriteLine("Menu");
            Console.WriteLine("A to add a new person into the database");
            Console.WriteLine("B to view the database");
            Console.WriteLine("Q to quit");
            choise = (char) Console.Read();
            switch (choise)
            {
                case 'A':
                    addPerson();
                    break;
                case 'B':
                    break;
                case 'Q':
                    break;
                default:
                    Console.WriteLine("It can be tricky to read the instructions sometimes :)/> ");
                    break;


            }
        }

    }

    public void viewDb()
    {
        for (int a = 0; a != count; a++)
        {
            Console.WriteLine("{0}: Name {1}  Sec-num: {2}", (a + 1), db[a].name, db[a].number);
        }
    }
    public void addPerson()
    {
        Console.WriteLine("Enter the name: ");
        db[count].name = Console.ReadLine();
        Console.WriteLine("Enter the sec-num");
        db[count].number = Console.Read();
        count++;
    }

}



Is This A Good Question/Topic? 0
  • +

Replies To: c to c# , need help

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: c to c# , need help

Posted 31 January 2011 - 11:02 AM

I am going to guess that you are trying to teach yourself C# without much guidance, a decent book or without knowing where to look. Sometimes just knowing where to look can make all the difference. Google is your friend.
Search with either "C#" or "MSDN" as the first word: "MSDN Picturebox", "C# Custom Events", "MSDN timer" etc.

But honestly, just typing away and seeing what pops up in Intellisense is going to make your self-education take 20 years. You can learn by trying to reverse engineer the language through banging on the keyboard - or you can learn by doing the tutorials and following a good "How to learn C#" book.

May I suggest picking up a basic C# introductory book? It's not that people here don't want to be helpful, but there is a certain amount of basic learning work that one should really take upon themselves before asking for help. There are so many great "How do I build my first application" tutorials on the web... There are dozens of "Learn C# in 21 days", "My first C# program" books at your local book seller or even public library... Asking a forum, any forum, to hand-hold you through it is just redundant. In many ways it disrespects the people who have invested dozens of hours in the on-line tutorials and those that spent thousands of hours in authoring books. C# resources. Start here

Build a Program Now! in Visual C# by Microsoft Press, ISBN 0-7356-2542-5
is a terrific book that has you build a Windows Forms application, a WPF app, a database application, your own web browser.

C# Cookbooks
Are a great place to get good code, broken down by need, written by coding professionals. You can use the code as-is, but take the time to actually study it. These professionals write in a certain style for a reason developed by years of experience and heartache.

Microsoft Visual Studio Tips, 251 ways to improve your productivity, Microsoft press, ISBN 0-7356-2640-5
Has many, many great, real-world tips that I use all the time.

The tutorials below walk through making an application including inheritance, custom events and custom controls.
Quick and easy custom events
Bulding an application - Part 1
Building an application - Part 2
Debugging tutorial
Working with environmental variables

Writing a text file is always one of the first things people want to do, in order to store data like high-scores, preferences and so on
Writing a text file tutorial.

These are just good every-day references to put in your bookmarks.
MSDN C# Developers Center with tutorials
Welcome to Visual Studio

Have you seen the MSDN Code Samples? They spent a lot of time creating samples and demos. It seems a shame to not use them.

  • Anonymous Delegates: Demonstrates the use of unnamed delegates to reduce application complexity.
  • Arrays: Shows how to use arrays.
  • Attributes: Shows how to create custom attribute classes, use them in code, and query them through reflection.
  • Collection Classes: Shows how to make non-generic collection classes that can be used with the foreach statement.
  • COM Interop Part I: Shows how to use C# to interoperate with COM objects.
  • COM Interop Part II: Shows how to a use a C# server together with a C++ COM client.
  • Commandline: Demonstrates simple command-line processing and array indexing.
  • Condiational Methods: Demonstrates conditional methods, which provide a powerful mechanism by which calls to methods can be included or omitted depending on whether a symbol is defined.
  • Delegates: Shows how delegates are declared, mapped to static and instance methods, and combined into multicast delegates.
  • Events: Shows how to declare, invoke, and configure events in C#.
  • Explicit Interface: Demonstrates how to explicitly implement interface members and how to access those members from interface instances.
  • Generics: Shows how to make generic collection classes that can be used with the foreach statement.
  • Hello World: A Hello World application.
  • Indexers Part I: Shows how C# classes can declare indexers to provide array-like access to objects.
  • Indexers Part II: Shows how to implement a class that uses indexed properties. Indexed properties enable you to use a class that represents an array-like collection.
  • Libraries: Shows how to use compiler options to create a DLL from multiple source files; also, how to use the library in other programs
  • Named and Optional (C# 4.0): Demonstrates Named and Optional parameters, an alternative to method overloads
  • Nullable: Demonstrates value types, such as double and bool, that can be set to null
  • Office Sample (C# 4.0): Demonstrates how Dynamic and COM Interop make it easy to call Microsoft Office in C# 4.0
  • OLEDB: Demonstrates how to use a Microsoft Access database from C# by creating a dataset and adding tables to it.
  • Operator Overloading: Shows how user-defined classes can overload operators
  • Partial Types: Demonstrates how classes and structures can be defined in multiple C# source-code files
  • PInvoke: Shows how to call exported DLL functions from C#
  • Properties: Shows how properties are declared and used; also demonstrates abstract properties
  • Python Sample (C# 4.0): Learn how to call a Python script by using the Dynamic feature in C# 4.0
  • Security: Discusses .NET Framework security and shows how to modify security permissions in C# by using permission classes and permission attributes
  • Simple Variance (C# 4.0): See how Covariance and Contravariance are supported in generic interfaces and delegates
  • Structs: Shows how to use structs in C#.
  • Threading: Demonstrates various thread activities such as creating and executing a thread, synchronizing threads, interacting between threads, and using a thread pool
  • Unsafe: Shows how to use unmanaged code (code that uses pointers) in C#
  • User Conversions: Shows how to define conversions to and from user-defined types
  • Versioning: Demonstrates versioning in C# by using the override and new keywords
  • XML Documents: Shows how to document code by using XML
  • Yield: Demonstrates how to use the yield keyword to filter items in a collection

Was This Post Helpful? 1
  • +
  • -

#3 Eqric   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 31-January 11

Re: c to c# , need help

Posted 31 January 2011 - 11:13 AM

Thanks for the reply , but i am not just typing and see what pops up , i am atm using this tutorial http://www.csharp-st.../Tutorials.aspx , but thanks for the good links, i will take a look at them!
Was This Post Helpful? 1
  • +
  • -

#4 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: c to c# , need help

Posted 31 January 2011 - 11:19 AM

View PostEqric, on 31 January 2011 - 11:13 AM, said:

Thanks for the reply , but i am not just typing and see what pops up , i am atm using this tutorial http://www.csharp-st.../Tutorials.aspx , but thanks for the good links, i will take a look at them!


Looks like a really good intro tutorial. Thanks for the reference. I'm going to add it to my 'canned' reference list for new C# students.
Was This Post Helpful? 0
  • +
  • -

#5 Eqric   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 31-January 11

Re: c to c# , need help

Posted 31 January 2011 - 11:58 AM

But why doesn't my ReadLine work? I just cant get it ...

This post has been edited by Eqric: 31 January 2011 - 11:58 AM

Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5104
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: c to c# , need help

Posted 31 January 2011 - 12:44 PM

Console.Read

Quote

The Read method blocks its return while you type input characters; it terminates when you press the Enter key. Pressing Enter appends a platform-dependent line termination sequence to your input (for example, Windows appends a carriage return-linefeed sequence). Subsequent calls to the Read method retrieve your input one character at a time. After the final character is retrieved, Read blocks its return again and the cycle repeats.

...

The ReadLine method, or the KeyAvailable property and ReadKey method are preferable to using the Read method.


So what's happening here is that you're filling up a buffer, with a '\n' at the end of it. And each subsequent Read/ReadLine reads from that buffer until it's gone.

So when you Read, you push 'a', then 'enter'. So the buffer has "a\n" in it. Your Read statement returns the first character from the buffer: 'a'. Your buffer now has "\n" in it. Your next ReadLine statement reads from the buffer, because there's something in it. It will read all the way up to a newline...which happens to be the first character there. So ReadLine returns an empty string, since there was nothing behind the newline.

Avoid this by using ReadKey and ReadLine. ReadKey is much, much better for getting single characters from the console, since it doesn't open a buffer. It returns as soon as any key is pressed.

ReadLine won't return until you press enter, but it'll read in everything you typed until then.
Was This Post Helpful? 1
  • +
  • -

#7 Eqric   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 31-January 11

Re: c to c# , need help

Posted 31 January 2011 - 12:57 PM

Ok i think i got it, i am going try to modify the code and see if i can get it to work.

Thanks for the help!

Edit: Its working now :)

This post has been edited by Eqric: 31 January 2011 - 12:59 PM

Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5104
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: c to c# , need help

Posted 31 January 2011 - 01:00 PM

Glad to hear it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1