Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > BOOK: Professional C# 2005
|
BOOK: Professional C# 2005
This is the forum to discuss the Wrox book Professional C# 2005 by Christian Nagel, Bill Evjen, Jay Glynn, Karli Watson, Morgan Skinner, Allen Jones; ISBN: 9780764575341
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional C# 2005 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
 
Old April 1st, 2010, 02:56 PM
Authorized User
 
Join Date: Dec 2007
Posts: 48
Thanks: 4
Thanked 0 Times in 0 Posts
Default Getting an error about a non static field that is instantiated as a Hastable

I've tried everything I can think of, but it still comes back with following error:

Error 1 An object reference is required for the non-static field, method, or property 'MortimerPhonesEmployees.TestHarness.employees' C:\Users\Tom Magaro\Documents\Visual Studio 2008\Projects\Collections\MortimerPhonesEmployees\ TestHarness.cs 47 29 MortimerPhonesEmployees.

Here is the code. I've bolded and underlined the problem field.

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace MortimerPhonesEmployees
{
classTestHarness
{
Hashtable employees = newHashtable(31);
publicstaticvoid Run()
{
EmployeeID idMortimer = newEmployeeID("B001");
EmployeeData mortimer = newEmployeeData(idMortimer, "Mortimer",
100000.00M);
EmployeeID idArabel = newEmployeeID("W234");
EmployeeData arabel = newEmployeeData(idArabel, "Arabel Jones",
10000.00M);
while (true)
{
try
{
Console.Write("Enter employee ID (format:A999, X to exit)>");
string userinput = Console.ReadLine();
userinput = userinput.ToUpper();
if (userinput == "X")
return;
EmployeeID id = newEmployeeID(userinput);
DisplayData(id);
}
catch (Exception e)
{
Console.WriteLine("Exception occurred. Did you use the correct" +
"format for the employee ID?");
Console.WriteLine(e.Message);
Console.WriteLine();
}
Console.WriteLine();
}
}
privatestaticvoid DisplayData(EmployeeID id)
{
object empobj = employees[id];
if (empobj != null)
{
EmployeeData employee = (EmployeeData)empobj;
Console.WriteLine("Employee: " + employee.ToString());
}
else
Console.WriteLine("Employee not found: ID = " + id);
}
}
}

I am running on vs2008 but I can't afford the book for it.


any help would be appreciated.

Thanx,

Tom
__________________
Thomas G Magaro

Last edited by flashmanTom; April 1st, 2010 at 02:59 PM..
 
Old April 1st, 2010, 04:49 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

The 'employees' field isn't initialized because you aren't constructing an instance of the 'TestHarness' type.

1. Either make 'employees' a static field (the CLR will allocate memory for the field when the type is loaded), or

2. Make your Run() and DisplayData() methods instance methods:

public void Run()
private void DisplayData(EmployeeID id)

and construct an instance of 'TestHarness', leaving 'employess' declared as an instance field (the CLR will allocate memory for the non-static field when it allocates an instance of the type).

TestHarness th = new TestHarness();
th.Run();

If you don't explicitly declare a constructor for 'TestHarness', the compiler will emit a public, parameterless constructor for you.

Sort of like:

// Field
Hashtable employees;

// .ctor
public TestHarness()
{
// Field initialization;
this.employees = new Hashtable(31);
}

You can leave the Run() and DisplayData() methods as static methods, but then they would require a parameter of type 'TestHarness' so you could pass them a 'TestHarness' instance, then do the field access through the 'TestHarness' instance variable.

TestHarness.Run(new TestHarness());
...
public static void Run(TestHarness th)
{
...
TestHarness.DisplayData(th, id);
...
}
...
public static void DisplayData(TestHarness th, EmployeeID id)
{
object empobj = th.employees[id];
if (empobj != null)
{...}
}

Pretty ugly...

P.S.

object empobj = employees[id];
if (empobj != null)

will always evaluate to null given what you have so far.

Anyway, easiest solution:

staticHashtable employees = newHashtable(31);
 
Old April 1st, 2010, 05:09 PM
Authorized User
 
Join Date: Dec 2007
Posts: 48
Thanks: 4
Thanked 0 Times in 0 Posts
Thumbs up Many thanks

Hi Bob,

I'm kind of embarassed that it was that simple. I'll get the hang of it one of these days!

I appreciate that rapid response.


Thanks alot,

Tom
__________________
Thomas G Magaro





Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Static variables in Static Class JoinTTT BOOK: Professional ASP.NET 3.5 : in C# and VB ISBN: 978-0-470-18757-9 4 March 29th, 2009 05:08 PM
Can't intialize a Hastable in Constructor chobo2 C# 2008 aka C# 3.0 2 October 8th, 2008 02:08 PM
Question on hastable code petfrog BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 2 February 22nd, 2008 05:15 PM
non-static reports to static html files miamikk ASP.NET 2.0 Basics 0 June 4th, 2007 01:48 PM
static field with class instance prakash.suthar Java Basics 2 July 3rd, 2006 01:41 AM





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