object error

T

tshad

I am getting the following error for:

C:\VSProjects\ClassLibrary4\NewHire.cs(72): An object reference is required
for the nonstatic field, method, or property 'MyFunctions.NewHire.firstName'

My program looks something like:

public class NewHire
{
private string firstName = "";
private string lastName = "";
private string middleInitial = "";

....

private static void GetNewHire(ref string returnString)
{
DbObject myDbObject = new DbObject("Persist Security Info=False;Data
Source=Venus;Initial Catalog=f;User ID=xx;Password=jj;");
SqlDataReader dbReader;

SqlParameter[] parameters = {
new SqlParameter("@ApplicantID",SqlDbType.Int)};

parameters[0].Value = 241;

dbReader = myDbObject.RunProcedure("GetNewHire", parameters);
if (dbReader.Read())
{
firstName = (string)dbReader["FirstName"]; <--
lastName = (string)dbReader["LastName"];

The string (firstName) is valid so what is the compiler complaining about?

Thanks,

Tom
 
T

tshad

I found that if I change the declaration from:

private string firstName = "";

to
private static string firstName = "";

then it works.

Why do I have to define the variable as static to make this work?

Thanks,

Tom
 
V

Vadym Stetsyak

Hello, tshad!

t> private string firstName = "";

t> to
t> private static string firstName = "";

t> then it works.

t> Why do I have to define the variable as static to make this work?

Because you use instance variables from whithin static method. Static method can only operate with static class members...

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
 
T

Tom Porterfield

Because you have defined your method as static (private static void
GetNewHire). Since, by defining this method as static, you have stated that
it can be accessed outside of any instance of your NewHire class, that
method must not access any members of your class that are specific to an
instance of the class.

Tom Porterfield

tshad said:
I found that if I change the declaration from:

private string firstName = "";

to
private static string firstName = "";

then it works.

Why do I have to define the variable as static to make this work?

Thanks,

Tom

tshad said:
I am getting the following error for:

C:\VSProjects\ClassLibrary4\NewHire.cs(72): An object reference is
required for the nonstatic field, method, or property
'MyFunctions.NewHire.firstName'

My program looks something like:

public class NewHire
{
private string firstName = "";
private string lastName = "";
private string middleInitial = "";

...

private static void GetNewHire(ref string returnString)
{
DbObject myDbObject = new DbObject("Persist Security Info=False;Data
Source=Venus;Initial Catalog=f;User ID=xx;Password=jj;");
SqlDataReader dbReader;

SqlParameter[] parameters = {
new SqlParameter("@ApplicantID",SqlDbType.Int)};

parameters[0].Value = 241;

dbReader = myDbObject.RunProcedure("GetNewHire", parameters);
if (dbReader.Read())
{
firstName = (string)dbReader["FirstName"]; <--
lastName = (string)dbReader["LastName"];

The string (firstName) is valid so what is the compiler complaining
about?

Thanks,

Tom
 
T

Tom Spink

tshad said:
I found that if I change the declaration from:

private string firstName = "";

to
private static string firstName = "";

then it works.

Why do I have to define the variable as static to make this work?

Thanks,

Tom

tshad said:
I am getting the following error for:

C:\VSProjects\ClassLibrary4\NewHire.cs(72): An object reference is
required for the nonstatic field, method, or property
'MyFunctions.NewHire.firstName'

My program looks something like:

public class NewHire
{
private string firstName = "";
private string lastName = "";
private string middleInitial = "";

...

private static void GetNewHire(ref string returnString)
{
DbObject myDbObject = new DbObject("Persist Security Info=False;Data
Source=Venus;Initial Catalog=f;User ID=xx;Password=jj;");
SqlDataReader dbReader;

SqlParameter[] parameters = {
new SqlParameter("@ApplicantID",SqlDbType.Int)};

parameters[0].Value = 241;

dbReader = myDbObject.RunProcedure("GetNewHire", parameters);
if (dbReader.Read())
{
firstName = (string)dbReader["FirstName"]; <--
lastName = (string)dbReader["LastName"];

The string (firstName) is valid so what is the compiler complaining
about?

Thanks,

Tom

Hi Tom,

I fear that your solution will NOT provide you with the results you're
looking for.

A field/method/property defined as static within a class is accessible
without an instance of that class, i.e. you do not need to instantiate the
containing class to access the methods. So, you have your static method
'GetNewHire', which is trying to access the non-static fields firstName, et
al. Since originally, those fields are not defined as static, they are
only accessible when you instantiate the class, with the 'new' statement:

///
NewHire newHire = new NewHire();
///

When you assign a value to a static field, that value is the same wherever
you access the field. So, if you call 'GetNewHire' multiple times, the
firstName, and indeed any other fields defined as static, and then values
assigned to, will be overwritten.

Is this the behaviour you want? Or do you actually want instance-based
members?

Hope this helps!

-- Tom
OrElse what...
 
T

tshad

Tom Spink said:
tshad said:
I found that if I change the declaration from:

private string firstName = "";

to
private static string firstName = "";

then it works.

Why do I have to define the variable as static to make this work?

Thanks,

Tom

tshad said:
I am getting the following error for:

C:\VSProjects\ClassLibrary4\NewHire.cs(72): An object reference is
required for the nonstatic field, method, or property
'MyFunctions.NewHire.firstName'

My program looks something like:

public class NewHire
{
private string firstName = "";
private string lastName = "";
private string middleInitial = "";

...

private static void GetNewHire(ref string returnString)
{
DbObject myDbObject = new DbObject("Persist Security Info=False;Data
Source=Venus;Initial Catalog=f;User ID=xx;Password=jj;");
SqlDataReader dbReader;

SqlParameter[] parameters = {
new SqlParameter("@ApplicantID",SqlDbType.Int)};

parameters[0].Value = 241;

dbReader = myDbObject.RunProcedure("GetNewHire", parameters);
if (dbReader.Read())
{
firstName = (string)dbReader["FirstName"]; <--
lastName = (string)dbReader["LastName"];

The string (firstName) is valid so what is the compiler complaining
about?

Thanks,

Tom

Hi Tom,

I fear that your solution will NOT provide you with the results you're
looking for.

A field/method/property defined as static within a class is accessible
without an instance of that class, i.e. you do not need to instantiate the
containing class to access the methods. So, you have your static method
'GetNewHire', which is trying to access the non-static fields firstName,
et
al. Since originally, those fields are not defined as static, they are
only accessible when you instantiate the class, with the 'new' statement:

///
NewHire newHire = new NewHire();
///

When you assign a value to a static field, that value is the same wherever
you access the field. So, if you call 'GetNewHire' multiple times, the
firstName, and indeed any other fields defined as static, and then values
assigned to, will be overwritten.

Is this the behaviour you want? Or do you actually want instance-based
members?

No, it isn't.

I understand now from the other posts why I was having the problem, but I
hadn't thought about the problem you mentioned.

I assume that anything inside of GetNewHire (local variables) would be
separate and distinct for each call ( 5 people calling it at one time would
get 5 separate sets of local variables that have no connection to each
other).

With the static variables, there is only one instance of the variable (one
firstName, one lastName, etc). So if user 1 sets firstName to "Tom" it will
stay that way until user 2 sets it to "Larry". Then if user 1 accesses
firstName again - he will get "Larry".

Right?

Thanks,

Tom
 
T

Tom Porterfield

tshad said:
With the static variables, there is only one instance of the variable (one
firstName, one lastName, etc). So if user 1 sets firstName to "Tom" it
will stay that way until user 2 sets it to "Larry". Then if user 1
accesses firstName again - he will get "Larry".

Right?

That is correct. If you remove the static keyword from your method name as
well as your variables, then each user 1 and user 2 can each create their
own instance of the NewHire class and any changes user 1 makes will be
completely isolated from the change user 2 makes to the user 2 instance of
NewHire.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top