473,383 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

An object reference is required

I have class, which returns dataTable to populate my list box on some other
pages:

public class dataClass

{
private Int16 _lvlRights=1

public static DataTable dtAdv()
{
if (_lvlRights == 1)
{
//my code......
}

}

}

I get an error:

Error 1 An object reference is required for the nonstatic field, method, or
property 'dataClass._lvlRights'

If I remove static keyword from dtAdv method than it works, but than I can't
call my class from other pages:

lstBox.DataSource=dataClass.dtAdv()

How can I use variable _lvlRights in my static methods?

Regards,S
Aug 28 '06 #1
2 8555
Hi Simon,

You must make _lvlRights static.

private static Int16 _lvlRights = 1;
On Mon, 28 Aug 2006 10:10:22 +0200, simonZ
<si*********@studio-moderna.comwrote:
I have class, which returns dataTable to populate my list box on some
other
pages:

public class dataClass

{
private Int16 _lvlRights=1

public static DataTable dtAdv()
{
if (_lvlRights == 1)
{
//my code......
}

}

}

I get an error:

Error 1 An object reference is required for the nonstatic field, method,
or
property 'dataClass._lvlRights'

If I remove static keyword from dtAdv method than it works, but than I
can't
call my class from other pages:

lstBox.DataSource=dataClass.dtAdv()

How can I use variable _lvlRights in my static methods?

Regards,S



--
Happy Coding!
Morten Wennevik [C# MVP]
Aug 28 '06 #2
Hi ,

You can either make lvlRights static or remove the static from the method.
If you make static lvlRights you have to be careful with concurrency in case
that variable could be changed from an external property.

Additionally you could make your class a singleton.

Another route is declare your class as static (if you are using 2.0 ) this
will prevent you from declaring any instance member
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"simonZ" <si*********@studio-moderna.comwrote in message
news:eP**************@TK2MSFTNGP06.phx.gbl...
>I have class, which returns dataTable to populate my list box on some other
pages:

public class dataClass

{
private Int16 _lvlRights=1

public static DataTable dtAdv()
{
if (_lvlRights == 1)
{
//my code......
}

}

}

I get an error:

Error 1 An object reference is required for the nonstatic field, method,
or property 'dataClass._lvlRights'

If I remove static keyword from dtAdv method than it works, but than I
can't call my class from other pages:

lstBox.DataSource=dataClass.dtAdv()

How can I use variable _lvlRights in my static methods?

Regards,S


Aug 28 '06 #3

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

Similar topics

0
by: muralidharan | last post by:
WebForm1.aspx Code: <%@ Register TagPrefix="ComponentArt" Namespace="ComponentArt.Web.UI" Assembly="ComponentArt.Web.UI" %> <ComponentArt:TreeView id="TreeView1" Height="520"...
1
by: Bucko | last post by:
Hi guys, this may be an easy for someone... I have a situation like below. The "str" string variable (and any other variable that VS.net declares normally in this space) can be used fine in...
6
by: William Mild | last post by:
I must be getting brain fried. I can't see the error. Create a new web form with the following code begind: Public Class test Inherits System.Web.UI.Page Public Class ReportCardData ...
9
by: Remulac | last post by:
Hello, I'm trying to get the value out of a dropdown list box and assign it to a variable. When I click on the list box, I invoke this line of code. I get the error, "Object reference not set...
8
by: ST | last post by:
Hello everyone, Can anyone help me with this error above when I debug my web app project in vstudio.net?? I can't figure it out! It was working fine for months, and now all of a sudden it's not!!...
3
by: SAL | last post by:
I am getting the following ERROR in my WebApp on line 30: Server Error in '/TestWebApp' Application. -------------------------------------------------------------------------------- Object...
1
by: Don | last post by:
I'm getting the following exception displayed in the task list at design time for my project: "Code generation for property 'Controls' failed. Error was: 'Object reference not set to an...
4
by: cppquester | last post by:
I have a data set MMSDataAccess with: public partial class MMSDataSet : System.Data.DataSet { ... private TB_ACTHEATDATADataTable tableTB_ACTHEATDATA; ...
4
by: livmacca | last post by:
Hi, I am new to VB .Net programming and is trying to create a webpage. I encountered the following error and is totally clueless on how to make it work: ...
8
by: mrcw | last post by:
Hi I've added a textbox called tbAccel0 to a form. It shows up on the form with the correct name, but when I look at the code a red squigly line appears under tbAccel0 and an error appears in the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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 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.