|
|
Hi all
I’m looking for some assientance on auto initialising all public fields in a class.
I have a class with about 50+ fields. When an instance of the class is created and a value is assigned to the fields, it appends to the existing static value.
MyClass obj = new MyClass();
obj.Example = “THIS_STRING”; // This would therefore return STATIC_BEGINING_OF_STRINGTHIS_STRING which is desired.
My problem is that in obj.allRecords will only contain “STATIC_BEGINING_OF_STRINGTHIS_ STRING” and not “ANOTHER_STATIC_BEGINING_OF_STRING” plus the other 50 fields as they have not been initialised to their initial default value. I dont want to do it manually in the object as the point is that they are in the list as their default value even if unchanged.
Thanks for any help.
class MyClass {
public MyClass() { }
public List<string> allRecords = new List<string>();
private string _example = "STATIC_BEGINING_OF_STRING";
public string Example
{
get { return _example; }
set
{
_example += value;
allRecords.Add(_example);
}
}
private string _recordEnd = "ANOTHER_STATIC_BEGINING_OF_STRING";
public string RecordEnd
{
get { return _recordEnd; }
set
{
_recordEnd += value;
allRecords.Add(_recordEnd);
}
}
}
|
|
|
|
|
I am not exactly sure that I understand what you mean when you say:
tig2810 wrote: I dont want to do it manually in the object
If that means that you don't want to type in the code to add each field to allRecords , then I don't know how to help you.
Otherwise you might consider a static constructor .
Also you might want to consider changing the type of allRecords . Using List<string> means that each time recordEnd is modified, a new entry will be added to allRecords , You might consider a generic Hashtable or Dictionary .
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
tig2810 wrote:
I dont want to do it manually in the object
-
If that means that you don't want to type in the code to add each field to allRecords, then I don't know how to help you.
This is my point. I want to type it in the class so it's automattically initialised in the object. I dont want to have to initialise all the fields in the object, but all the values to be already available.
|
|
|
|
|
In that case a static constructor sounds like it would fit the bill.
This is the MS documentation[^]
and
This gives a few more examples[^]
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Ok great, it sounds like that's the right direction for me. I've been looking and trying it out for a while but i dont see to be able to get it to work. Would you be able to help my showing me an example for the first field?
Thanks for helping. I'm still learning.
|
|
|
|
|
1. You could initialize the properties instead of initializing the fields.
class MyClass
{
public MyClass()
{
Example = "...";
RecordEnd = "...";
}
}
2. You could use reflection in the class's constructor to grab the values of all fields. I don't know if the order of addition to allRecords matters though.
class MyClass
{
public MyClass()
{
var fields = this.GetType().GetFields(...);
foreach(var field in fields)
{
allRecords.Add(field.GetValue(this));
}
}
}
|
|
|
|
|
hi everybody
I want to bind datagridview in runtime and can make column a combobox and another column checkbox and so like in access db
how can i do that
please answer me
|
|
|
|
|
MS has already invested wasted a lot of money in preparing documentation. Why don't you read it?
Wasted, since there are many who just don't look at it.
modified on Sunday, May 31, 2009 2:54 PM
|
|
|
|
|
Hi
Here this code might help you.
DataGridViewColumn col;
col = new DataGridViewComboBoxColumn();
dataGridView1.Columns.Add(col);
col = new DataGridViewCheckBoxColumn();
dataGridView1.Columns.Add(col);
Do reply after trying this.
Satish Pai B
|
|
|
|
|
Hello!
I'm stumped! I've been tring to code up a routine that monitors a parallel port pin for status change, then reads the data coming across it. My unique requirement is that I need to monitor the port more frequently than provided by the usual Sleep or Timer utilities (which have millisecond accuracy). I've developed a microsecond-level Timer to solve that problem, using QueryPerformanceCounter/Frequency. The real problem I have is that by polling the parallel port, the program locks up: "Program Not Responding". I've looked into Port Interrupts, but haven't found much that's intelligible.
What's an easy way to implement what I'm trying to accomplish?
Glen
|
|
|
|
|
You could remove the Program Not Responding problem by doing the polling in another thread. I would be surprised if Windows didn't let you hook into the interrupt for the parallel port; that said, it's understandable why the designers don't want an application being able to sniff every byte being sent through such a port
Between the idea
And the reality
Between the motion
And the act
Falls the Shadow
|
|
|
|
|
Would BackgroundWorker() be appropriate to generate another thred? BTW, I'm using a while-loop to do the polling. With regard to hooking into an interrupt, I've been digging into the documentation, but haven't found anything really useful...
|
|
|
|
|
You could use a BackgroundWorker, yes. But you'd probably find instantiating a Thread would be a little simpler; BackgroundWorker does this behind the scenes anyway
Between the idea
And the reality
Between the motion
And the act
Falls the Shadow
|
|
|
|
|
Sorry it took so long to get back to you - been busy with my "other" job. If I'm polling the port pins [while(look for port pin change)] in a background thread, wouldn't that just still cause the system to not respond?
|
|
|
|
|
i have a algorithm which i need to code in C#. but im having difficulties translating it to a programming language. Does anyone know a good application that converts a math expression for C# for evaluating??
i tried to use MathML to C# but the software crashed once it is loaded
Any help please
|
|
|
|
|
|
|
Should the last equation be N(x,y)? It looks like the required equation for the conditional in B(x,y), since it evaluates to zero or one.
I(x,y) also seems to require a definition of I2(x,y), unless this is a notation convention I'm not familiar with.
There are three kinds of people in the world - those who can count and those who can't...
|
|
|
|
|
hi. is there any program to convert mathml to c or c++?
thankyou....
|
|
|
|
|
Dear All,
I am looking for a way where i can get newly inserted records or newly updated records or deleted records from access tables.
I mean if u look close to SQL profiler, we can get any queries exchanged with sql servers, i am looking for such an object in Microsoft Access.
More of you will ask me to post this into Database forum, I am coding such feature using c#, so i posted it in c# forum.
what i need is that end of the day i would like to know which table's data has been manipulated.
will access provide me such a feature?
I do not intend to add flag column into my tables.
Abdul Rahaman Hamidy
Database Developer
Kabul, Afghanistan
|
|
|
|
|
Hi,
why don't you add a timestamp field to each table? it stores the current datetime on each insert/update.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
There's no thing for Access databases. The Jet engine doesn't support Triggers either.
|
|
|
|
|
I wamt to add my application to menu(appear when right click at a folder or a file) and add it to Startup in System Configuration Utility (Run - > msconfig).Thank you very much!!!!!
|
|
|
|
|
i guess you need to communicate with windows API
i would love to know how u will do it!
|
|
|
|