C# CSharp Reset Struct Class Memory

S

Sheikko

Hi,
I have a class, cerated like a struc for some reasons, and I want to
reset all values in it.

public class MyClass
{
public byte Channel;
public byte SatelliteID;
public byte SyncFlags;
public byte PhaseErrorCount;
}

After filling the class with some values, I want to reset all values
to the default, like the first time it was created.
I can do this filling every parameter, but I have some classes with a
lot of parameteres.
there is a way to reset this class? For example clear the memory
occupied by the class.
I know that I can make assigning a NULL to the class and recreate it,
but I search another way.

THank you very much
 
N

Nicholas Paldino [.NET/C# MVP]

Sheikko,

Well, to be quite honest, I think letting the old class go and creating
a new instance is the best option. You stated that you wanted to reset all
the values to the default like when they are constructed, so why not use
what the language offers you? What are you hoping to gain by resetting the
fields to all the values as opposed to just creating a new instance of the
class?
 
J

Jon Skeet [C# MVP]

Sheikko said:
I have a class, cerated like a struc for some reasons, and I want to
reset all values in it.

public class MyClass
{
public byte Channel;
public byte SatelliteID;
public byte SyncFlags;
public byte PhaseErrorCount;
}

After filling the class with some values, I want to reset all values
to the default, like the first time it was created.
I can do this filling every parameter, but I have some classes with a
lot of parameteres.
there is a way to reset this class? For example clear the memory
occupied by the class.
I know that I can make assigning a NULL to the class and recreate it,
but I search another way.

As Nicholas said, that's a fairly odd thing to do. You could use
reflection, although it wouldn't be terribly fast. You also need to
consider what you want to happen if there's inheritance involved.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top