|
I always use the first method for the resons given already and also I may have several constuctors which may require a different initial value to be set e.g.
public class MyClass
{
public const int DefaultMyField = 123;
private int myField;
public MyClass()
: this(DefaultMyField)
{ }
public MyClass(int myField)
{
this.myField = myField;
}
}
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
I tend toward the first as well, for consistency.
public class X
{
int y ;
int z ;
public X
(
int y
)
{
this.y = y ;
this.z = 42 ;
return ;
}
}
z could be initialized where it's declared, but y can't, so I initialize z in the same place y is initialized.
|
|
|
|
|
The following part of the C# language spec may be of some interest:
§17.4.5 Variable initializers (skipped the example)
The default value initialization described in §17.4.3 occurs for all fields, including fields that have variable
initializers. Thus, when a class is initialized, all static fields in that class are first initialized to their default
values, and then the static field initializers are executed in textual order. Likewise, when an instance of a
class is created, all instance fields in that instance are first initialized to their default values, and then the
instance field initializers are executed in textual order. When there are field declarations in multiple partial
type declarations for the same type, the order of the parts is unspecified. However, within each part the field
initializers are executed in order.
All of this happens before any code in the ctor is run, as detailed in:
§17.4.5.2 Instance field initialization
The instance field variable initializers of a class correspond to a sequence of assignments that are executed
immediately upon entry to any one of the instance constructors (§17.10.2) of that class. The variable
initializers are executed in the textual order in which they appear in the class declaration. The class instance
creation and initialization process is described further in §17.10.
|
|
|
|
|
dear all
i am a newbe, this is a basic issue. i want to get a form's width height, and other property in another form. so how i can achive this point? thanks.
|
|
|
|
|
Hi,
Your question is not clear. Could you tell us what you've done so far to get what you want, and which issues you encountered ?
|
|
|
|
|
One of the ways is:
From Form1 get Height and Width of Form2 by:
Form2 frm2 = new Form2();
int height = frm2.Height;
int width = frm2.Width;
I Love T-SQL
"Don't torture yourself,let the life to do it for you."
If my post helps you kindly save my time by voting my post.
www.aktualiteti.com
|
|
|
|
|
Assuming you have a reference to the other form then you can access its properties.
It's time for a new signature.
|
|
|
|
|
thanks first.
if i have such dialogs
InspectionImageDialog m_ImgDlg = new InspectionImageDialog();
InspectionResultDialog m_RltDlg = new InspectionResultDialog();
Intel_Amd_InfoDialog intelamdDialog = new Intel_Amd_InfoDialog();
i would like to only new once and i can use this instance in anywhere of my project. how to do that? so far, i have 3 forms above and 1 mainDlg, so i want to control 3 dialogs in my mainDlg, also sometimes i want to get some property in other dialog. so how to new a instance and then i can use this instance freely and get its property and asign value or get value there.
|
|
|
|
|
|
Keep the references to your dialogs or forms in your main form class or make them global so that you can refer to them in any module.
It's time for a new signature.
|
|
|
|
|
how to keep the reference for dialog or forms in my main form class? could u give me an example?thanks
|
|
|
|
|
I don't quite understand your problem with this. Just add a variable of the correct form type to your main form, create the new form and save the reference. You could do this in your constructor or create a special method that does it on demand; either way your other forms will remain until you destroy them. Then you can use that from anywhere in your main form code.
It's time for a new signature.
|
|
|
|
|
If you want to access an object instance from anywhere, the Singleton pattern[^] is the recommended method. Are you sure you want to do this for three forms though? Static/Singletons have their place and uses but can often be the result of poor design, poor undestanding of OOP, lazyness. I'm not saying "don't", just be sure that it is actually required!
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
Using White in .net
how shall i get the DatagridView object in White
thanks in advance
|
|
|
|
|
Your question is not very clear.
What is 'White'?
Do you mean the color White, or are you referring to a third party library called White?
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.”
|
|
|
|
|
In .net we've got several timers, but all of them specify an interval to elapse rather than a specific time to come. I'd like to implement a timer which specifies a specific time, say to be triggered at the time of 10:25:15am (hour:minute:second). Using polling tech to determine if it's the right time to trigger is a very bad way and inefficient, I'd like to put the timer to sleep, then when the time is coming it receives a signal from the system to be woken up and then do its work.
One way to do this is still based on the Threading.Timer, convert how long to wait for the specific time into interval. But is there any mechanism provided by .net directly supporting this scenario to signal the thread at specific time?
Many thanks.
|
|
|
|
|
I'm not aware of any explicit support for a due time.
For a 1-second resolution I would just pick some timer (Windows.Forms/Threading/Timers depending on circumstances) and use DateTime and TimeSpan classes to calculate and set the delay.
For a finer resolution, I'd start with the above, then probably execute some polling loop within the final second, including a Thread.Sleep(ms) with a number of milliseconds fitting the resolution (assuming >= 50)
Whatever you do, keep in mind things could go wrong in several ways:
- your PC might get very busy while calculating and setting the due time, resulting in a late call;
- your PC might get very busy at the due time, so the requested action may actually run somewhat later;
- your PC might be switched off or crash in the mean time.
Depending on your requirements a Windows scheduled task may or may not be a better option.
|
|
|
|
|
Yep, Windows scheduled task may be a way. But it needs to be reused/consumed by other programs, so using Windows scheduled task is out of scope.
|
|
|
|
|
The best way to do what you have requested is to use Threading.Timer as you have already noted.
Timers aren't meant to be used this way though, that is to wake up at a specific time only. Why don't you use the Scheduled task if what you want is to run your service/process at a specific time.
|
|
|
|
|
Yeah, a Scheduled Task might be the way to go.
You could use a Timer, but it might not be very good for that purpose -- have it wait for half (or 90% or something) of the remaining time, check, repeat as necessary.
|
|
|
|
|
|
|
|
Xeno's dichotomy paradox[^] says you get tortoise kebab in the real world...
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
quartz.net has a number of timers, a cron type timer being amongst them - may be worth a look
'g'
|
|
|
|