Calling class in App_Code

S

sck10

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;
}
 
Joined
Aug 11, 2006
Messages
1
Reaction score
0
You have two possible solutions.

1) You need to instantiate a new class_general object, e.g.:
int MonthIndex = 4;
class_General clsGen = new class_General();
string MyMonth = clsGen.MonthIndexToString(MonthIndex);

2) You don't have to instantiate a new object if you're accessing a static function, so you can redefine the MonthIndexToString function as:
static public string MonthIndexToString(int MyMonth)
{
blah, blah, blah
}
The body of you content page does not need to change with this option.


Personally, when I'm writing utility classes like this, I prefer option 2, but that's just me...
 
N

neilmcguigan

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/netframework/programming/classlibraries/namingconventions/
 
C

Cowboy \(Gregory A. Beamer\)

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!
*************************************************
 
S

Steven Cheng[MSFT]

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.
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top