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 6 1308
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**********@ftsolutions.com> wrote in message
news:u5**************@TK2MSFTNGP15.phx.gbl... 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
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
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**********@ftsolutions.com> wrote in message
news:er**************@tk2msftngp13.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**********@ftsolutions.com> wrote in message news:u5**************@TK2MSFTNGP15.phx.gbl...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
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**********@ftsolutions.com> wrote in message news:u5**************@TK2MSFTNGP15.phx.gbl...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...
"Tom Spink" <s0******@sms.ed.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**********@ftsolutions.com> wrote in message news:u5**************@TK2MSFTNGP15.phx.gbl...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 Hope this helps!
-- Tom OrElse what...
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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...
|
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...
|
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...
| |
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).
...
|
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...
|
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...
|
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...
|
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...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
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...
| |
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |