|
The question assumes an instance constructor. A static constructor is not meant to be called by any outside code or any other class (derived or not derived), it is called only once by the loader, so the issue of derivation is totally moot for static constructor.
|
|
|
|
|
And my answer applies to instance constructors -- in C#.
|
|
|
|
|
That just sounds wrong.
From the C# 5 lang spec: section 1.6.6.3
The ... constructor (which is like an instance method) ...
Because the constructor is an instance member, it is permitted to access both the ... instance field and the ... static field.
The constructor has access to all of the instance's fields and properties and even instance methods.
Section 1.6.7.1
Unlike other members, instance constructors are not inherited, and a class has no instance constructors other than those actually declared in the class. If no instance constructor is supplied for a class, then an empty one with no parameters is automatically provided.
Section 10.11.1:
If an instance constructor has no constructor initializer, a constructor initializer of the form base() is implicitly provided. Thus, an instance constructor declaration of the form
C(...) {...}
is exactly equivalent to
C(...): base() {...}
A positive attitude may not solve every problem, but it will annoy enough people to be worth the effort.
|
|
|
|
|
As I recall, the default base ctor is always called first, at least it is if the base class contains instance properties that need initialization.
|
|
|
|
|
CRobert456 wrote: The logic is based on the fact the a base constructor has to be called explicitly, meaning the compiler will not call it automatically That would mean that all objects (!) would inherit the default constructor. That'd be a bit impractical.
You can add code to call an inherited constructor in your class, even if there's a different signature.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
First, I have a button that calls 2 functions. First function (calculateMedieSim()) makes an operation with two number and the second one (selectData()) selects data from tabel. totalsim = 15 and totalsimyes = 8. The result should be 53,3 but it shows 0 Why?
private void button1_Click(object sender, EventArgs e)
{
selectData();
calculateMedieSim();
}
private void selectData()
{
string select = "SELECT * FROM accounts WHERE id=1";
using (SQLiteCommand cmd = new SQLiteCommand(select, Conexiune.getConnection()))
{
using (SQLiteDataReader read = cmd.ExecuteReader())
{
if (read.Read())
{
totalsim = (int)read["totalsim"];
totalsimyes = (int)read["totalsimyes"];
}
}
}
}
private void calculateMedieSim()
{
medie_sim = (totalsimyes / totalsim) * 100;
sansa_examen = Math.Round(medie_sim, 2);
Console.WriteLine(sansa_examen);
}
|
|
|
|
|
the only thing I see wrong is (and I presume you've defined the variables this way as well) ...
totalsim = (int)read...
totalsimyes = (int)read...
so, using integer math, whats going to happen when you divide 8 by 15, hmmmm ?
|
|
|
|
|
those 2 variables are defined by int type
|
|
|
|
|
defined by int type ? yes, because you've defined them as such - define them as double for example
|
|
|
|
|
Aaaa yes, there was the problem. Thanks
|
|
|
|
|
If I change code to:
medie_sim = (totalsim / totalsimyes) * 100;
medie_sim = Math.Round(medie_sim, 2);
Console.WriteLine(medie_sim);
I get 100
|
|
|
|
|
how is medie_sim defined ? let me guess - also an Int ?
|
|
|
|
|
|
It's the same problem as your other code - integer division.
15 / 8 = 1.875 , but since the variables are integers, only the integer part is used: 15 / 8 = 1 - You then multiply that result by
100 , which (unsurprisingly) gives a final result of 100 . - You then store that final result in a
double , but it's too late - you've already lost the fractional part at step 1.
You need to convert one of the values to a floating-point type before the division:
medie_sim = ((double)totalsim / totalsimyes) * 100;
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
May I know how should we extract the individual colour chart from GUI to display the result of RGB, HSI and Lab?
|
|
|
|
|
|
I want to insert an image in database. But if I don't choose any image, I receive : Empty path name is not legal . In database, image field is BLOB and is null. Where is my mistake cause I can't figure it out..I think that I should verify if textBox5 is null and if is, imageBt=null . I tried this but it didn't worked. This is the code that I'm using to insert the image:
byte[] imageBt = null;
FileStream fstream = new FileStream(this.textBox5.Text, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fstream);
imageBt = br.ReadBytes((int)fstream.Length);
SQLiteCommand com = new SQLiteCommand("insert into questions(image) values(@IMG)", Conexiune.getConnection());
com.Parameters.Add(new SQLiteParameter("@IMG", imageBt));
com.ExecuteNonQuery();
|
|
|
|
|
Try this instead of just checking for null:
if (!String.IsNullOrWhiteSpace(textBox5.Text))
{
}
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Where should I put this?..
|
|
|
|
|
"Around" your posted code. Or what Pete replied before your posted code (that if (!File.Exists(textBox5.Text)) that he posted also).
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Gosh it was so easy..I think I'm tired ) Sorry for this boring question. Thank you!
|
|
|
|
|
Hehe, alright
To cover that topic in a general statement: Always check your inputs before trusting them.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
|
First of all, you should name your textboxes with a more meaningful name. Then, you really want to test some validity like this:
if (string.IsNullOrWhitespace(textBox5.Text)) return;
if (!File.Exists(textBox5.Text)) return;
|
|
|
|
|
Where should I put this?..
|
|
|
|