Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
| Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old August 5th, 2003, 03:28 PM
Registered User
Join Date: Jul 2003
Location: Calgary, AB, Canada.
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to megabytes
Default Help with Functions

Here is the my code but I get an error saying "An object reference is required for the nonstatic field, method, or property Getname.function1(string, string" HELP!!!! :)


class Getname

            public static void Main(string[] args)

                  Console.WriteLine("Please enter your first and last name with one space");
                  string userName = Console.ReadLine();

                  Console.WriteLine(" ");

                  char[] separator = {' '};
                  string[] fullName;

                  fullName = userName.Split(separator);

                  string firstName = fullName[0];
                  string lastName = fullName[1];

                  char[] firstLetter = firstName.ToCharArray();

                  function1 (fullName[1], fullName[0]);
                  function2 (firstLetter[0], fullName[1]);


            public void function1 (string lastName, string firstName)
                  Console.WriteLine("{0}", lastName + ", " + firstName);

            public void function2( char firstLetter, string lastName)
                  Console.WriteLine("{0}", firstLetter + "." + " " + lastName);



  #2 (permalink)  
Old August 7th, 2003, 12:48 PM
Authorized User
Join Date: Jun 2003
Location: Gainesville, FL, USA.
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts

Quick $0.02:
Maybe you should have a constructor for Getname, and have your Main call it
( Getname gn = new Getname(); in the Main routine).
I think the problem is that function1 is a Method (i.e. a member) of Getname, and in truth it is a non-static member. Thus, you are able to call function1 (and function2) only if you have an instance of Getname. So
 Getname gn = new Getname();
 gn.function1("foo", "bar");
should work.


Similar Threads
Thread Thread Starter Forum Replies Last Post
C functions...??? ethantinder C# 2 April 25th, 2008 02:25 AM
Functions LTello BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 3 September 15th, 2006 11:15 AM
Functions czambran BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 13 July 11th, 2006 06:33 PM
functions xelepi PHP How-To 1 March 10th, 2006 02:59 AM
Functions Meg Intro Programming 1 January 23rd, 2006 01:03 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.