HighTechTalks DotNet Forums  

Is there a difference of this two ways?

CSharp microsoft.public.dotnet.languages.csharp


Discuss Is there a difference of this two ways? in the CSharp forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Chua Wen Ching
 
Posts: n/a

Default Is there a difference of this two ways? - 07-01-2004 , 02:21 AM






I believe each ways below will produce the same results. But may i know which one will be better and encourage to be used.

Sample1:

class Animal
{
public void GetWeight()
{

}
}

class ClientApp
{
Animal anim = new Animal();
anim.GetWeight();
}

Sample2:

class Animal
{
private static Animal weight;

public static Animal GetWeight()
{
if (weight == null)
{
weight = new Animal();
}

return weight;
}
}

class ClientApp
{
Animal anim = Animal.GetLength();
}

Any help? Thanks.
--
Regards,
Chua Wen Ching

Reply With Quote
  #2  
Old   
Sameeksha
 
Posts: n/a

Default RE: Is there a difference of this two ways? - 07-01-2004 , 04:51 AM






I believe the GetWeight() method is supposed to return weight of an animal object. (The return type void is a little confusing -- should it be float ?). Basically the concept of static variables in a class is that if one has any piece of information which is common to all objects of a class, it should be stored as a static data member. Now, if weight has common meaning of weight of an animal, it will be different for different animal objects, so it should be stored in an instance variable rather than a static variable, with a property defined to get and set its value.
So the first approach is a better one, even though it's not clear what will be the logic of GetWeight() method.

You can try something like this
class Animal
{
float m_weight;
public float Weight
{
get { return m_weight; }
set { if (value > 0) m_weight = value; }
}
//other code
}

class Client
{
Animal first;
Client() { first = new Animal(); }
public void SetWeight(float w) { first.Weight = w; }
public float GetWeight() { return first.Weight; }
//other methods and properties
}

Hope this helps.
--
Sameeksha,
MCP (.NET)


"Chua Wen Ching" wrote:

Quote:
I believe each ways below will produce the same results. But may i know which one will be better and encourage to be used.

Sample1:

class Animal
{
public void GetWeight()
{

}
}

class ClientApp
{
Animal anim = new Animal();
anim.GetWeight();
}

Sample2:

class Animal
{
private static Animal weight;

public static Animal GetWeight()
{
if (weight == null)
{
weight = new Animal();
}

return weight;
}
}

class ClientApp
{
Animal anim = Animal.GetLength();
}

Any help? Thanks.
--
Regards,
Chua Wen Ching

Reply With Quote
  #3  
Old   
Chua Wen Ching
 
Posts: n/a

Default RE: Is there a difference of this two ways? - 07-01-2004 , 04:57 AM



Thanks for the reply Sameeksha.

--
Regards,
Chua Wen Ching


"Sameeksha" wrote:

Quote:
I believe the GetWeight() method is supposed to return weight of an animal object. (The return type void is a little confusing -- should it be float ?). Basically the concept of static variables in a class is that if one has any piece of information which is common to all objects of a class, it should be stored as a static data member. Now, if weight has common meaning of weight of an animal, it will be different for different animal objects, so it should be stored in an instance variable rather than a static variable, with a property defined to get and set its value.
So the first approach is a better one, even though it's not clear what will be the logic of GetWeight() method.

You can try something like this
class Animal
{
float m_weight;
public float Weight
{
get { return m_weight; }
set { if (value > 0) m_weight = value; }
}
//other code
}

class Client
{
Animal first;
Client() { first = new Animal(); }
public void SetWeight(float w) { first.Weight = w; }
public float GetWeight() { return first.Weight; }
//other methods and properties
}

Hope this helps.
--
Sameeksha,
MCP (.NET)


"Chua Wen Ching" wrote:

I believe each ways below will produce the same results. But may i know which one will be better and encourage to be used.

Sample1:

class Animal
{
public void GetWeight()
{

}
}

class ClientApp
{
Animal anim = new Animal();
anim.GetWeight();
}

Sample2:

class Animal
{
private static Animal weight;

public static Animal GetWeight()
{
if (weight == null)
{
weight = new Animal();
}

return weight;
}
}

class ClientApp
{
Animal anim = Animal.GetLength();
}

Any help? Thanks.
--
Regards,
Chua Wen Ching

Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.