473,761 Members | 2,824 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

classes with static members

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.EternityRe cordsEntities

namespace News
public static class News
EternityRecords Entities NewsContext as new EternityRecords Entities()

'*** A simple method to show what I am looking for. Also see above code.
public static function CreateNews(Titl e 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.In sertNewsArticle (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
5 2665
On 2008-11-11, Andy B <a_*****@sbcglo bal.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.EternityRe cordsEntities

namespace News
public static class News
EternityRecords Entities NewsContext as new EternityRecords Entities()

'*** A simple method to show what I am looking for. Also see above code.
public static function CreateNews(Titl e 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.In sertNewsArticle (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 EntityRecordsEn tity()

Public Sub DoStuff()
_newsContext.Do CoolStuff()
End Sub
End Module
End NameSpace

HTH
--
Tom Shelton
Nov 11 '08 #2
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 EntityRecordsEn tity()
I didn't think you could have the line above in a "static class".
Public Sub DoStuff()
_newsContext.Do CoolStuff()
End Sub
End Module
End NameSpace


Nov 11 '08 #3
On 2008-11-11, Andy B <a_*****@sbcglo bal.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.WriteLi ne (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.Insta nce.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 EntityRecordsEn tity()
I didn't think you could have the line above in a "static class".
Sure you can...
>Public Sub DoStuff()
_newsContext.D oCoolStuff()
End Sub
End Module
End NameSpace
Usage:
MyNamespace.MyM odule.DoStuff ()

--
Tom Shelton
Nov 11 '08 #4
>NameSpace MyNameSpace
Public Module MyModule
Private _newsContext As New EntityRecordsEn tity()
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
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_*****@sbcglo bal.neta écrit dans le message de groupe de
discussion : OG************* *@TK2MSFTNGP03. phx.gbl...
>>NameSpace MyNameSpace
Public Module MyModule
Private _newsContext As New EntityRecordsEn tity()
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2407
by: Erik Bongers | last post by:
Hi, Nested classes only seem to be able to access static members of the surrounding class : class SurroundingClass { public: class InnerClass { public:
45
3618
by: Steven T. Hatton | last post by:
This is a purely *hypothetical* question. That means, it's /pretend/, CP. ;-) If you were forced at gunpoint to put all your code in classes, rather than in namespace scope (obviously classes themselves are an exception to this), and 'bootstrap' your program by instantiating a single application object in main(), would that place any limitations on what you could accomplish with your program? Are there any benefits to doing things that...
5
14436
by: kuvpatel | last post by:
Hi I want to refer a class called LogEvent, and use one of its methods called WriteMessage without actually having to create an instance of Logevent. I have tried using the word sealed with the class and this works but I would also like to know of other ways to do this. Also are there any performance implacations of using sealed?
11
3843
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of ways to do this through prototyping and other techniques, but these behaviors need to be static and...
4
1493
by: JC | last post by:
Suppose an ASP.Net project contains a public static class with public methods and members that are used throughout the application. Of course being static, there is only copy of the class within the application. Now suppose two users access the Web site simultaneously. Does each user see his/her own single copy of the static class, or do they share the class, thus creating a problem that can only be solved if one use blocks the other...
16
1856
by: chameleon | last post by:
I have 2 classes with exactly the same members (all static except dtor/ctor). Classes have different implementantion in only one static member function and first class has one more member function. How can I write this code with templates? First of all: Thought to write code with templates is correct? members of classes are static because refer to devices. There is no reason to be non-static.
3
3898
by: puzzlecracker | last post by:
Would you quickly remind me the difference between, regular class, static class, and nested class? Thanks
0
9554
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10136
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9989
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9925
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7358
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6640
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3913
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.