CS0118 on string function

This seems like it should be such an easy thing to do, but no matter what I try I get this error message.
Compiler Error Message:CS0118: 'ASP.Global_asax.g' denotes a 'field' where a 'class' was expected

Line 31: Session ["UserSystemName"] = WindowsIdentity.GetCurrent().Name;
Line 32: Session ["Domain"] = g.getDomain (WindowsIdentity.GetCurrent().Name);
Line 33: Session ["UserName"] = g.getUser (WindowsIdentity.GetCurrent().Name);

Line: 32

This is for an ASP.NET page in the Global.asax file for Session_Start () and is using C#.

g is defined as: Toolkit.General g;

The entire contents of Toolkit.cs is


namespace Toolkit
{
using System;
using Microsoft.VisualBasic;

public class General
{

public static string getDomain (string str)
{
int pos;
pos = str.IndexOf (@"\");
if (pos == 0)
{
return "N/A";
}
else
{
return str.Substring (0, pos);
}
}

public static string getUser (string str)
{
int pos;
pos = str.IndexOf (@"\");
if (pos == 0)
{
return str;
}
else
{
return str.Substring (pos + 1, str.Length - pos - 1);
}
}
}
}



When I use
Session ["Domain"] = "abc";
It works just fine, also if I call this from a Visual Basic routine it has no problems.
What am I doing wrong?
[1715 byte] By [SNAFU] at [2007-12-25]
# 1

Hi, SNAFU.

The issue is that you have defined methods "getDomain(string)" and "getUser(string)" as static and you are attempting to invoke them like instance members. Static members can only be invoked on the declaring type. If you change these two lines from:

Session ["Domain"] = g.getDomain (WindowsIdentity.GetCurrent().Name);
Session ["UserName"] = g.getUser (WindowsIdentity.GetCurrent().Name);

...to:

Session ["Domain"] = Toolkit.General.getDomain (WindowsIdentity.GetCurrent().Name);
Session ["UserName"] = Toolkit.General.getUser (WindowsIdentity.GetCurrent().Name);

...it should fix the CS0118 error. An alternate solution is to remove the "static" keyword on your definitions of "getDomain(string)" and "getUser(string)".

Check out http://msdn2.microsoft.com/en-us/library/98f28cdx.aspx for more on the C# "static" keyword. There's plenty of information on the Internet regarding the advantages/disadvantages of declaring members as static versus instance. Start with a search on "static instance members." Also check out a little illustration at the end of my post on how to call static and instance methods.

Hope that helps,

Damon
Visual C#

Example:

class A
{
// static member
public static string B()
{
return "Hello, World!";
}

// instance member
public string C()
{
return "Hello, World!";
}
}

class Program
{
public static void Main()
{
// static member call
System.Console.WriteLine(A.B());

A a = new A();
// instance member call
System.Console.WriteLine(a.C());
}
}

DamonTivel-MSFT at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# Language...
# 2

Hello, I am getting roughly this same error message ('People' denotes a 'namespace' where a 'class' was expected) except I am getting it in client code that is using a collection class (People) of classes, adding two objects to it, and then displaying the Name (property) of each of those objects. The answer above didn't seem to fit my problem. I appreciate any help.

This is the client code:

using System;

//using System.Collections;

using People;

namespace Ch11N01

{

/// <summary>

/// Summary description for Class1.

/// </summary>

class Class1

{

[STAThread]

staticvoid Main(string[] args)

{

People myPeople = new People();

myPeople.Add(new Person("Bob",20));

myPeople.Add(new Person("Jack",30));

foreach (Person aPerson in myPeople)

{

Console.WriteLine(aPerson.ToString());

}

}

}

}

This is my class for the People collection class (in a class library, referenced in client code):

using System;

using System.Collections;

namespace People

{

publicclass People : CollectionBase

{

People myPeople = new People();

publicvoid Add (Person newPerson)

{

List.Add(newPerson);

}

publicvoid Remove (Person oldPerson)

{

List.Remove(oldPerson);

}

public People()

{

}

public Person this[int personIndex]

{

get

{

return (Person)List[personIndex];

}

set

{

List[personIndex] = value;

}

}

}

}

And this is the code for the Person class, which People uses (also in the class library):

using System;

namespace People

{

/// <summary>

/// Summary description for Class1.

/// </summary>

publicclass Person

{

privatestring name;

privateint age;

private Person()

{

}

public Person(string inName, int inAge)

{

name = inName;

age = inAge;

}

publicstring Name

{

get

{

return name;

}

set

{

name = value;

}

}

publicint Age

{

get

{

return age;

}

set

{

age = value;

}

}

publicoverridestring ToString()

{

return "I am " + name + " and I am " + age + " years old";

}

}

}

bruce027721 at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# Language...