Self intializaed class

  • Thread starter Thread starter Tamir Khason
  • Start date Start date
T

Tamir Khason

Questing:
Following program. I want on request of property of some class if the class
not initialized - do it, but the following returns exception (...not set to
the instance of the object...) Why?
SomeClass class = GetThisClass.SomeClass();

//Class
class SomeClass
{
public SomeClass()
{}
}

class GetThisClass
{
SomeClass m_class;
public GetThisClass()
{}
public SomeClass SomeClass
{
get
{
if(m_class == null)
m_class=new SomeClass(); //If the class is null
Initialize
return m_class;
}
set
{
m_class = value;
}

}
}
 
The main problem is in the statement
SomeClass class = GetThisClass.SomeClass(); There are 3 problems:
(1) class is a keyword, cannot be used as an identifier.
(2) SomeClass is a property, so () should not be used.
(3) SomeClass is an instance property (not a static one), so it requires an instance of the class GetThisClass.

Replace the above statement with the following one:

SomeClass c = (new GetThisClass()).SomeClass;

It will work fine.
Hope this helps.
 
Thsnk you for response:
(1) class is a keyword, cannot be used as an identifier. Just for demonstatration
(2) SomeClass is a property, so () should not be used. Typo
(3) SomeClass is an instance property (not a static one), so it requires
an instance of the class GetThisClass.
This was the problem and got it :)

Tnx anyway

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "


Sameeksha said:
The main problem is in the statement
SomeClass class = GetThisClass.SomeClass(); There are 3 problems:
(1) class is a keyword, cannot be used as an identifier.
(2) SomeClass is a property, so () should not be used.
(3) SomeClass is an instance property (not a static one), so it requires
an instance of the class GetThisClass.
 
Back
Top