473,404 Members | 2,178 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,404 software developers and data experts.

Calling class in App_Code

Hello, I am trying to get the Month from the following using "switch" in my
App_Code file. The error that I am getting is:
An object reference is required for the nonstatic field, method, or property
'class_General.MonthIndexToString(int)'

In my content page I am using the following syntax to call my class:
int MonthIndex = 4;
string MyMonth = class_General.MonthIndexToString(MonthIndex);
App_Code
------------
public class class_General
{
public class_General()
{
//
//
}

public string MonthIndexToString(int MyMonth)
{
//Get the month from the integer value for the month (January: 1)
string strMyDate = "Non Selected";
switch (MyMonth)
{
case 1: strMyDate = "January";
break;
case 2: strMyDate = "February";
break;
case 3: strMyDate="March";
break;
case 4: strMyDate = "April";
break;
case 5: strMyDate = "May";
break;
case 6: strMyDate = "June";
break;
case 7: strMyDate = "July";
break;
case 8: strMyDate = "August";
break;
case 9: strMyDate = "September";
break;
case 10: strMyDate = "October";
break;
case 11: strMyDate = "November";
break;
case 12: strMyDate = "December";
break;
default: strMyDate = "Non Selected";
break;
} // end switch
return strMyDate;
}
Aug 11 '06 #1
4 2045
you either have to instantiate the object, like this:

class_General object_General = new class_General();

string myMonth = object_General.MonthIndexToString(MonthIndex);

OR

you have to declare the method as static:

public class class_General
{
public static string MonthIndexToString(int monthIndex) { ... }
}

i'd recommend reading microsoft's documentation on good naming
convention practice too:

http://msdn.microsoft.com/netframewo...ngconventions/
sck10 wrote:
Hello, I am trying to get the Month from the following using "switch" in my
App_Code file. The error that I am getting is:
An object reference is required for the nonstatic field, method, or property
'class_General.MonthIndexToString(int)'

In my content page I am using the following syntax to call my class:
int MonthIndex = 4;
string MyMonth = class_General.MonthIndexToString(MonthIndex);
App_Code
------------
public class class_General
{
public class_General()
{
//
//
}

public string MonthIndexToString(int MyMonth)
{
//Get the month from the integer value for the month (January: 1)
string strMyDate = "Non Selected";
switch (MyMonth)
{
case 1: strMyDate = "January";
break;
case 2: strMyDate = "February";
break;
case 3: strMyDate="March";
break;
case 4: strMyDate = "April";
break;
case 5: strMyDate = "May";
break;
case 6: strMyDate = "June";
break;
case 7: strMyDate = "July";
break;
case 8: strMyDate = "August";
break;
case 9: strMyDate = "September";
break;
case 10: strMyDate = "October";
break;
case 11: strMyDate = "November";
break;
case 12: strMyDate = "December";
break;
default: strMyDate = "Non Selected";
break;
} // end switch
return strMyDate;
}
Aug 11 '06 #2
Another method to doing this is to create an enum with the month names. You
can then get the name or number using native methods of the enum. As long as
the enum is compiled in with the library, it is much easier than using a
method and generally gives a slight perf boost, as well.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

*************************************************
Think outside of the box!
*************************************************
"sck10" <sc***@online.nospamwrote in message
news:uz**************@TK2MSFTNGP02.phx.gbl...
Hello, I am trying to get the Month from the following using "switch" in
my App_Code file. The error that I am getting is:
An object reference is required for the nonstatic field, method, or
property 'class_General.MonthIndexToString(int)'

In my content page I am using the following syntax to call my class:
int MonthIndex = 4;
string MyMonth = class_General.MonthIndexToString(MonthIndex);
App_Code
------------
public class class_General
{
public class_General()
{
//
//
}

public string MonthIndexToString(int MyMonth)
{
//Get the month from the integer value for the month (January: 1)
string strMyDate = "Non Selected";
switch (MyMonth)
{
case 1: strMyDate = "January";
break;
case 2: strMyDate = "February";
break;
case 3: strMyDate="March";
break;
case 4: strMyDate = "April";
break;
case 5: strMyDate = "May";
break;
case 6: strMyDate = "June";
break;
case 7: strMyDate = "July";
break;
case 8: strMyDate = "August";
break;
case 9: strMyDate = "September";
break;
case 10: strMyDate = "October";
break;
case 11: strMyDate = "November";
break;
case 12: strMyDate = "December";
break;
default: strMyDate = "Non Selected";
break;
} // end switch
return strMyDate;
}

Aug 11 '06 #3
Thanks for Gregory and neilmcguigan's informative inputs.

Hello Steve,

I just found your another thread about "Month Name in c# (August)", is the
function you used here also related to that requirement. I saw some
community members have posted some good suggestion in that thread on
getting the month name string from a given datetime object. You can also
have a look there to see whethe the information is helpful to resolve your
problem.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.

Aug 14 '06 #4
Thanks Steven,

"Steven Cheng[MSFT]" <st*****@online.microsoft.comwrote in message
news:m8**************@TK2MSFTNGXA01.phx.gbl...
Thanks for Gregory and neilmcguigan's informative inputs.

Hello Steve,

I just found your another thread about "Month Name in c# (August)", is the
function you used here also related to that requirement. I saw some
community members have posted some good suggestion in that thread on
getting the month name string from a given datetime object. You can also
have a look there to see whethe the information is helpful to resolve your
problem.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no
rights.

Aug 14 '06 #5

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

Similar topics

5
by: sck10 | last post by:
Hello, I am converting a class in the App_Code folder from c# to vb and am having problems calling the sub procedure. I have created a Sub called HidePanels in the class "General". When I try...
2
by: pradeep_TP | last post by:
Hello, I am trying to use APP_CODE folder for all my class files under VS 2005. After adding APP_CODE in the solution explorer, I added a new web page by right clicking project and selecting add...
2
by: sck10 | last post by:
Hello, How do you reference an array in the App_Code section? I tried using SystemColors, but got the error: 'System.Drawing.SystemColors' is a 'type' but is used like a 'variable'. Thanks,...
8
by: shapper | last post by:
Hello, I am working with VS 2008 and created a Web Application Project. I added a class but whatever I do the class is not visible to my aspx pages or anywhere else. I then changed the...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
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,...
0
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...
0
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,...
0
agi2029
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 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.