473,471 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Calling non-static methods from within static methods.

Is the following legal/recommended? (It compiles w/o error or
warnings)

class MyClass()
{
public MyClass()
{
}

private string NonStaticMethod()
{
retrun "Something";
}

private static string StaticMethod()
{
MyClass mc = new MyClass();

return mc.NonStaticMethod();
}
}

Jul 18 '07 #1
3 1900
Yes. In short.

You should consider that MyClass might need proper Dispose after use. If you
will adhere to recommended practices.

However, "retrun" shouldn't compile. You don't get any errors?
"koredump" <ko******@interia.plwrote in message
news:11**********************@o11g2000prd.googlegr oups.com...
Is the following legal/recommended? (It compiles w/o error or
warnings)

class MyClass()
{
public MyClass()
{
}

private string NonStaticMethod()
{
retrun "Something";
}

private static string StaticMethod()
{
MyClass mc = new MyClass();

return mc.NonStaticMethod();
}
}

Jul 18 '07 #2

What you have looks similar to the Singleton design pattern where you have a
static method that everyone can call without having to create any objects.
This in turn returns a single existing instance of a resource that is shared
by everyone. Apart from that I don't see any other reason for you to make a
static method which instantiates the class it is in. You can read more about
the Singleton design pattern here:

http://www.dofactory.com/Patterns/PatternSingleton.aspx

Adrian.
--
[Please mark my answer if it was helpful to you]

"koredump" wrote:
Is the following legal/recommended? (It compiles w/o error or
warnings)

class MyClass()
{
public MyClass()
{
}

private string NonStaticMethod()
{
retrun "Something";
}

private static string StaticMethod()
{
MyClass mc = new MyClass();

return mc.NonStaticMethod();
}
}

Jul 18 '07 #3

"koredump" <ko******@interia.plwrote in message
news:11**********************@o11g2000prd.googlegr oups.com...
Is the following legal/recommended? (It compiles w/o error or
warnings)
Certainly legal. The only thing I see that is suspicious is locally
creating an instance to use, as typically the instance(s) will be passed in
via parameter. Operator overloads are required to work in this manner.
>
class MyClass()
{
public MyClass()
{
}

private string NonStaticMethod()
{
retrun "Something";
}

private static string StaticMethod()
{
MyClass mc = new MyClass();

return mc.NonStaticMethod();
}
}

Jul 19 '07 #4

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

Similar topics

6
by: komal | last post by:
hi all basically my problem is i have to write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function...
1
by: Ian Sedwell | last post by:
Hi guys Many thanks to all who replied to my original question. Actually, it's dead easy and the way I was doing it was correct the first time. You do indeed simply call the VBScript routine...
2
by: Giulio Belrango | last post by:
Hi I need someones help I'm working in an IBM 390 batch environment. What I'm trying to do is to call from a non DB2 COBOL program a DB2 COBOL program that will access a table and perform an...
0
by: monika.saxena | last post by:
Hi all, In one of my projects which is a web based application in asp.net, a third party tool - "Frontline Solver DLL" (It is an unmanaged DLL and ..NET is calling it using the PInvoke) is used....
11
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
3
by: Steven D'Aprano | last post by:
I have a class that has a distinct "empty" state. In the empty state, it shouldn't have any data attributes, but it should still have methods. The analogy is with a list: an empty list still has...
4
by: Jeronimo Bertran | last post by:
Hello, I have a WCF service that I am using to upload files to a server by using the streamed transfer method. I am currently calling the service from a WCF client. All I did was add a Service...
47
by: teju | last post by:
hi, i am trying 2 merge 2 projects into one project.One project is using c language and the other one is using c++ code. both are working very fine independently.But now i need to merge both...
10
by: sulekhasweety | last post by:
Hi, the following is the definition for calling convention ,which I have seen in a text book, can anyone give a more detailed explanation in terms of ANSI - C "the requirements that a...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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...
1
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...
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,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
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 ...
0
muto222
php
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.