473,804 Members | 3,029 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

object error

I am getting the following error for:

C:\VSProjects\C lassLibrary4\Ne wHire.cs(72): An object reference is required
for the nonstatic field, method, or property 'MyFunctions.Ne wHire.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("Persi st Security Info=False;Data
Source=Venus;In itial Catalog=f;User ID=xx;Password= jj;");
SqlDataReader dbReader;

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

parameters[0].Value = 241;

dbReader = myDbObject.RunP rocedure("GetNe wHire", parameters);
if (dbReader.Read( ))
{
firstName = (string)dbReade r["FirstName"]; <--
lastName = (string)dbReade r["LastName"];

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

Thanks,

Tom
Mar 6 '06 #1
6 1330
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" <ts**********@f tsolutions.com> wrote in message
news:u5******** ******@TK2MSFTN GP15.phx.gbl...
I am getting the following error for:

C:\VSProjects\C lassLibrary4\Ne wHire.cs(72): An object reference is
required for the nonstatic field, method, or property
'MyFunctions.Ne wHire.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("Persi st Security Info=False;Data
Source=Venus;In itial Catalog=f;User ID=xx;Password= jj;");
SqlDataReader dbReader;

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

parameters[0].Value = 241;

dbReader = myDbObject.RunP rocedure("GetNe wHire", parameters);
if (dbReader.Read( ))
{
firstName = (string)dbReade r["FirstName"]; <--
lastName = (string)dbReade r["LastName"];

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

Thanks,

Tom

Mar 6 '06 #2
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
Mar 6 '06 #3
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" <ts**********@f tsolutions.com> wrote in message
news:er******** ******@tk2msftn gp13.phx.gbl...
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" <ts**********@f tsolutions.com> wrote in message
news:u5******** ******@TK2MSFTN GP15.phx.gbl...
I am getting the following error for:

C:\VSProjects\C lassLibrary4\Ne wHire.cs(72): An object reference is
required for the nonstatic field, method, or property
'MyFunctions.Ne wHire.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("Persi st Security Info=False;Data
Source=Venus;In itial Catalog=f;User ID=xx;Password= jj;");
SqlDataReader dbReader;

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

parameters[0].Value = 241;

dbReader = myDbObject.RunP rocedure("GetNe wHire", parameters);
if (dbReader.Read( ))
{
firstName = (string)dbReade r["FirstName"]; <--
lastName = (string)dbReade r["LastName"];

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

Thanks,

Tom



Mar 6 '06 #4
tshad wrote:
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" <ts**********@f tsolutions.com> wrote in message
news:u5******** ******@TK2MSFTN GP15.phx.gbl...
I am getting the following error for:

C:\VSProjects\C lassLibrary4\Ne wHire.cs(72): An object reference is
required for the nonstatic field, method, or property
'MyFunctions.Ne wHire.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("Persi st Security Info=False;Data
Source=Venus;In itial Catalog=f;User ID=xx;Password= jj;");
SqlDataReader dbReader;

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

parameters[0].Value = 241;

dbReader = myDbObject.RunP rocedure("GetNe wHire", parameters);
if (dbReader.Read( ))
{
firstName = (string)dbReade r["FirstName"]; <--
lastName = (string)dbReade r["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...
Mar 6 '06 #5
"Tom Spink" <s0******@sms.e d.ac.uk> wrote in message
news:du******** **@scotsman.ed. ac.uk...
tshad wrote:
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" <ts**********@f tsolutions.com> wrote in message
news:u5******** ******@TK2MSFTN GP15.phx.gbl...
I am getting the following error for:

C:\VSProjects\C lassLibrary4\Ne wHire.cs(72): An object reference is
required for the nonstatic field, method, or property
'MyFunctions.Ne wHire.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("Persi st Security Info=False;Data
Source=Venus;In itial Catalog=f;User ID=xx;Password= jj;");
SqlDataReader dbReader;

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

parameters[0].Value = 241;

dbReader = myDbObject.RunP rocedure("GetNe wHire", parameters);
if (dbReader.Read( ))
{
firstName = (string)dbReade r["FirstName"]; <--
lastName = (string)dbReade r["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 Hope this helps!

-- Tom
OrElse what...

Mar 6 '06 #6
tshad wrote:
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.
--
Tom Porterfield

Mar 6 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
10551
by: Pkpatel | last post by:
Hi, I keep getting this error every time I try to load crystalreportviewer on a webform with a dataset. Here is the error: -------------------------------------------------------- Server Error in '/Cr_Dataset' Application. ----------------------------------------------------------- ---------------------
2
2420
by: Nithi Gurusamy | last post by:
Dear Group: I have a COM object developed in VB. It makes ADODB calls. When it fails it Raise Error. I am using the COM object in my ASP using Server.CreateObject. Whenever a function call fails I wanted the system to catch the 500-100 error and redirect to the configured page in IIS. But nothing happens. I don't have "on error resume next" in my COM object. If I create ADO objects directly in my ASP code using Server.CreateObject it...
9
8609
by: Keith Rowe | last post by:
Hello, I am trying to reference a Shockwave Flash Object on a vb code behind page in an ASP.NET project and I receive the following error: Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx). On the aspx page I have the object tag as follows:
8
3998
by: mcmg | last post by:
Hi, I have an asp app that works fine on a windows xp machine but does not work on a windows 2000 server. I have the following code in my global.asa: <OBJECT RUNAT=Server SCOPE=SESSION ID=MyID
2
2404
by: Roby Eisenbraun Martins | last post by:
Hi, My name is Roby Eisenbraun Martins, I am a C++, VB and NET developer. I am working with a NET 2002 project right now and I am receiving this uncommon "OutOfMemory" error message when I try to load a form object ( new frmMain() ). In debug mode, the "Load" form method is executed but it crashes when it tries to set a DataTable from a DataSet in a local variable. Actually the object value in debug mode is equal to nothing.
0
2669
by: Dirk Försterling | last post by:
Hi all, a few days ago, I upgraded from PostgreSQL 7.2.1 to 7.4, following the instructions in the INSTALL file, including dump and restore. All this worked fine without any error (message). Since then, I found lots of the following in the postmaster output: 2003-11-29 15:19:54 ERROR: large object 4838779 does not exist 2003-11-29 15:20:11 ERROR: large object 4838779 does not exist
0
2132
by: Roman | last post by:
I'm trying to create the form which would allow data entry to the Client table, as well as modification and deletion of existing data rows. For some reason the DataGrid part of functionality stops working when I include data entry fields to the form: I click on Delete or Edit inside of DataGrid and get this error: "Error: Object doesn't support this property or method" If I remove data entry fields from the form - DataGrid allows to...
6
6128
by: blash | last post by:
Can someone help me? I really don't have a clue. My company staff told me they often got such error: "Object reference not set to an instance of an object." when they are in search result page then tried to access 2nd, or 3rd, etc page. The problem is it happens sometimes - sometimes when they clicked refresh button, then everything is ok. Now they told me it happens more frequently. but I have tried by myself many times and never got...
1
5555
by: J. Askey | last post by:
I am implementing a web service and thought it may be a good idea to return a more complex class (which I have called 'ServiceResponse') in order to wrap the original return value along with two other properties... bool error; string lastError; My whole class looks like this... using System;
2
4943
by: Moses | last post by:
Hi All, Is is possible to catch the error of an undefined element while creating an object for it. Consider we are not having an element with id indicator but we are trying to make the object for it indicator = document.getElementById('indicator');
0
10571
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10326
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10317
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10075
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7615
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5520
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4295
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.