By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,483 Members | 1,054 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,483 IT Pros & Developers. It's quick & easy.

classes with static members

P: n/a
I have a class that I want to make static but it uses some objects that are
instance objects. I keep getting a compiler error saying something about
using instance objects in a static class or method is not allowed. How do
you do this if you really need a static class but also have to use these
instance objects in them? If you need a simple example of what I am trying
to do, it is below:
imports Data.EternityRecordsEntities

namespace News
public static class News
EternityRecordsEntities NewsContext as new EternityRecordsEntities()

'*** A simple method to show what I am looking for. Also see above code.
public static function CreateNews(Title as string, Description as string,
Body as string) as integer
'*** Do whatever required to validate Title, Description and Body.

'*** Now use the entity framework model to insert the values above.
if (NewsContext.InsertNewsArticle(Title, Description, Body) = 0) '***
Creating news article succeeded.
'*** Log the action somehow.
Log.WriteEntry("Created news article '"+Title+"'.")
else
'*** Things didn't quite work out...

Log.WriteEntry("Failed to create news article '"+Title+"'.")
end if
end class
end namespace


Nov 11 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On 2008-11-11, Andy B <a_*****@sbcglobal.netwrote:
I have a class that I want to make static but it uses some objects that are
instance objects. I keep getting a compiler error saying something about
using instance objects in a static class or method is not allowed. How do
you do this if you really need a static class but also have to use these
instance objects in them? If you need a simple example of what I am trying
to do, it is below:
imports Data.EternityRecordsEntities

namespace News
public static class News
EternityRecordsEntities NewsContext as new EternityRecordsEntities()

'*** A simple method to show what I am looking for. Also see above code.
public static function CreateNews(Title as string, Description as string,
Body as string) as integer
'*** Do whatever required to validate Title, Description and Body.

'*** Now use the entity framework model to insert the values above.
if (NewsContext.InsertNewsArticle(Title, Description, Body) = 0) '***
Creating news article succeeded.
'*** Log the action somehow.
Log.WriteEntry("Created news article '"+Title+"'.")
else
'*** Things didn't quite work out...

Log.WriteEntry("Failed to create news article '"+Title+"'.")
end if
end class
end namespace

Your message is a bit confusing - the code you are showing seems to be a
combination of C# and VB :)

When you say static class, I assume you are refering to the C# construct:

public static class AStaticClass
{
....
}

Where all members of the class have to be static. VB uses the keyword Shared
rather then static for these sorts of methods. In VB Static applies to local
variables that keep their value between calls to the method

Another point, static classes do NOT have instance members because, there
are not instances of the class. So, members of a static class are refered to
as "class members", since the belong to the class and not a particular
instance.

Now that we have some of that cleared up - lets dive in... The equivalent of
the C# static class in VB.NET is a module.

NameSpace MyNameSpace
Public Module MyModule
Private _newsContext As New EntityRecordsEntity()

Public Sub DoStuff()
_newsContext.DoCoolStuff()
End Sub
End Module
End NameSpace

HTH
--
Tom Shelton
Nov 11 '08 #2

P: n/a
Your message is a bit confusing - the code you are showing seems to be a
combination of C# and VB :)
Oops! Sorry. I came from a C# background and I guess some of it still forces
me to think that way...grin.
When you say static class, I assume you are refering to the C# construct:

public static class AStaticClass
{
....
}
That's what I meant. A C# static class.
Where all members of the class have to be static. VB uses the keyword
Shared
rather then static for these sorts of methods. In VB Static applies to
local
variables that keep their value between calls to the method
Sounds a little confusing here. Have a simple example of how shared works?
Another point, static classes do NOT have instance members because,
thereare not instances of the class. So, members of a static class are
refered to as "class
members", since the belong to the class and not a particular instance.
OK. So the code I gave would have failed if it was converted to C# because
you can't have instances of classes in a static class. Once static, always
static...
Now that we have some of that cleared up - lets dive in... The equivalent
of
the C# static class in VB.NET is a module.
Can you give an example of how to actually use the below in simple code?
NameSpace MyNameSpace
Public Module MyModule
Private _newsContext As New EntityRecordsEntity()
I didn't think you could have the line above in a "static class".
Public Sub DoStuff()
_newsContext.DoCoolStuff()
End Sub
End Module
End NameSpace


Nov 11 '08 #3

P: n/a
On 2008-11-11, Andy B <a_*****@sbcglobal.netwrote:
>Your message is a bit confusing - the code you are showing seems to be a
combination of C# and VB :)
Oops! Sorry. I came from a C# background and I guess some of it still forces
me to think that way...grin.
I know what you mean - I am a C# programmer by day, so... :)
>When you say static class, I assume you are refering to the C# construct:

public static class AStaticClass
{
....
}
That's what I meant. A C# static class.
Ok... Then the VB.NET equivalent is a module.
>Where all members of the class have to be static. VB uses the keyword
Shared
rather then static for these sorts of methods. In VB Static applies to
local
variables that keep their value between calls to the method
Sounds a little confusing here. Have a simple example of how shared works?
Shared works like static in C# - only it can't be applied at the class level
(like C# v1).

Public Class MyClass

Public Shared Sub DoStuff ()
' Do Cool Stuff
End Sub
End Class

Calling code:

MyClass.DoStuff()

You call the method by a reference to the class name, not an instance of the
class. For instance:

Dim m As New MyClass()
m.DoStuff () ' compiler warning - though vb.net does allow you to do this

The usage of static in VB.NET might look like:

Public Function SomeSub() As Integer
Static i As Integer = 0
i += 1
Return i
End Function

In another method:

For i As Intege = 1 To 10
Console.WriteLine (SomeSub())
Next

1
2
3
4
5
6
7
8
9
10
>Another point, static classes do NOT have instance members because,
thereare not instances of the class. So, members of a static class are
refered to as "class
members", since the belong to the class and not a particular instance.
OK. So the code I gave would have failed if it was converted to C# because
you can't have instances of classes in a static class. Once static, always
static...
No, you can have instances of classes in a static class (or module). The
difference is that there is only one instance of a class variable, no matter
how many instances of the class are created... For instance, the
implementation of a singleton in C# often looks like:

public class Singleton
{
private static Singleton instance = new Singleton();

private Singleton(){}

public static Singleton Instance
{
get
{
return instance;
}
}

public void DoCoolStuff()
{
// do stuff
}
}

usi
usage:

Singleton.Instance.DoStuff();

The point is that if you declare a value as Shared in a class, then it is
exactly that - shared across all instances.
>Now that we have some of that cleared up - lets dive in... The equivalent
of
the C# static class in VB.NET is a module.
Can you give an example of how to actually use the below in simple code?
>NameSpace MyNameSpace
Public Module MyModule
Private _newsContext As New EntityRecordsEntity()
I didn't think you could have the line above in a "static class".
Sure you can...
>Public Sub DoStuff()
_newsContext.DoCoolStuff()
End Sub
End Module
End NameSpace
Usage:
MyNamespace.MyModule.DoStuff ()

--
Tom Shelton
Nov 11 '08 #4

P: n/a
>NameSpace MyNameSpace
Public Module MyModule
Private _newsContext As New EntityRecordsEntity()
I didn't think you could have the line above in a "static class".
Sure you can...
Ok. So From what I get, I have the following things:

1. Static (C#) and modules (VB) can have instances of objects that were
created inside them.
2. Shared members are members that stay the same no matter what instance of
a class happens to be created at the time (kind of like a pooled resource).
3. Modules and Static classes can't be turned into an instance.

Guess the old time C++ is stuck inside my head then. I remember not being
able to create instances of objects inside of static classes.

Nov 12 '08 #5

P: n/a
Also make sure to use the exact wording each time i.e. "creating an
instance", "having instances" and ealier "using instances" is not exacty the
same thing...

The basic rule is that a shared member can't refer to "me" or "this" (oas
they operate at the class level not at a particular instance level).

--
Patrice

"Andy B" <a_*****@sbcglobal.neta écrit dans le message de groupe de
discussion : OG**************@TK2MSFTNGP03.phx.gbl...
>>NameSpace MyNameSpace
Public Module MyModule
Private _newsContext As New EntityRecordsEntity()
I didn't think you could have the line above in a "static class".
Sure you can...

Ok. So From what I get, I have the following things:

1. Static (C#) and modules (VB) can have instances of objects that were
created inside them.
2. Shared members are members that stay the same no matter what instance
of a class happens to be created at the time (kind of like a pooled
resource).
3. Modules and Static classes can't be turned into an instance.

Guess the old time C++ is stuck inside my head then. I remember not being
able to create instances of objects inside of static classes.
Nov 12 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.