|
Few comments...
Classes with abstract methods have to be marked abstract. Think about it. You can't instantiate one because it doesn't know how to implement the abstract method, so the class becomes abstract.
You can never define two functions with the same name that take the same parameters but return different types. This class would never compile (has nothing to do with abstract classes):
<div class="code" style="font-family:Courier New;font-size:10pt;background-color:#FFFFFF;">
<span style="color:#0000FF;">public class </span><span style="color:#000000;">test<br>
{<br>
</span><span style="color:#0000FF;">string </span><span style="color:#000000;">Fred()<br>
{<br>
</span><span style="color:#0000FF;">return </span><span style="color:#000000;">"one";<br>
}<br>
<br>
</span><span style="color:#0000FF;">int </span><span style="color:#000000;">Fred()<br>
{<br>
</span><span style="color:#0000FF;">return </span><span style="color:#000000;">1;<br>
}<br>
}<br>
</span>
</div>
You probably know, but the second class should look like this:
<div class="code" style="font-family:Courier New;font-size:10pt;background-color:#FFFFFF;">
<span style="color:#0000FF;">public class </span><span style="color:#000000;">ChildClass : BaseClass<br>
{<br>
</span><span style="color:#0000FF;">private int </span><span style="color:#000000;">_MyInt = 6;<br>
<br>
</span><span style="color:#0000FF;">protected override string </span><span style="color:#000000;">MyMethod()<br>
{<br>
</span><span style="color:#0000FF;">return </span><span style="color:#000000;">_MyInt.ToString();<br>
}<br>
} <br>
</span>
</div>
Is there something else I'm missing on this?
I, for one, do not think the problem was that the band was down. I think that the problem may have been that there was a Stonehenge monument on the stage that was in danger of being crushed by a dwarf.
-David St. Hubbins
|
|
|
|
|
I was getting at the fact that you can do this with interfaces, so why not abstract classes?
Seems like it should be logical, an abstract class is just like a regular class with the abstract methods comming from an interface, except it has the abstract keyword.
public interface BaseInterface
{
string MyMethod();
}
public class ChildClass : BaseInterface
{
private int _MyInt = 6;
string BaseInterface.MyMethod()
{
return MyMethod().ToString(); // this calls int MyMethod()
}
public int MyMethod()
{
return _MyInt;
}
}
|
|
|
|
|
I see what you're saying now.
In order for you to call the ChildClass's "version" of the method (not the one that he implements to fulfill his derivation from the abstract class), you'd have to have some sort of syntax for specifying which version you were calling.
In the case of the interfaces, it's really a function of casting to the inteface and calling the method. It doesn't make sense to cast an instance of the the derived class to the derived class to get the derived one's "personal" version.
I'm sure this syntactical stuff (which is fairly nasty) was purposely avoided. That's one good reason to not allow multiple inheritance (which is a design descision MS made with C#).
I, for one, do not think the problem was that the band was down. I think that the problem may have been that there was a Stonehenge monument on the stage that was in danger of being crushed by a dwarf.
-David St. Hubbins
|
|
|
|
|
Ok, well this is pretty much exactly what i want to do with an abstract method.
public class DataBase
{
...
}
public class BaseClass
{
protected abstract DataBase DoMethod();
}
public class DataChild : DataBase
{
...
}
public class ChildClass : BaseClass
{
private int _MyInt = 6;
protected override DataBase BaseClass.DoMethod()
{
return DoMethod();
}
public DataChild DoMethod()
{
return new DataChild();
}
}
I am returning an object in my child's method that derives from the object being returned in my base's method.
So, this would not have any problems that I can see.
I don't see how this has anything to do with multiple inheritance, because I would still only be inheriting from one class. There is no reason why the return value can't be derived from the abstract method's return type.
I can not see in any way this conflicting with any multiple inheritance, or anything.
That way when the object is casted to its base class, the proper return type is still received.
What am I missing for this not to work?
|
|
|
|
|
I only mention multiple inheritance because having syntax to specify exactly whose version of a function you're calling is something that you HAVE to have once your language implements multiple inheritance. There's many an interview question on C++ based upon this type of crap . If you only have single inheritance, you can call your parent's implementation with a keyword like "base".
One thing to realize is it doesn't matter if the methods return different types at all. Two functions can't have the same name and take the same parameters unless we can use a specific "vtable" to call each one.
That said, think about this:
namespace Test
{
public interface I
{
void MethodA();
}
public class C : I
{
void Test.I.MethodA()
{
}
public void MethodA()
{
}
}
}
Given this code, instantiate an instance of C and call the I interface version of MethodA without first casting C to an I. AFAIK, it can't be done.
So the mechanism we use to differentiate between which version we're calling is a cast to the specific class. Casting allows us to specify whose "vtable" (a table that points to functions) we want to use.
Now in the case of a derived class, you can't do something like:
DerivedClass d = new DerivedClass();
((DerivedClass)d).MethodA();
because casting something that's already a given class doesn't really do anything.
Casting something to a base class or an interface gives you the "vtable" of that class, which allows us to "specify" a particular method. The problem comes into the fact that the base class's vtable might have a pointer to the derived class in the case the method gets overridden. This is what happens to virtual functions that are redefined with an override. Abstract methods are automatically virtual, they just have no "default" implementation. You are required to supply one in the case of abstract methods.
If we can't use casting to "filter" our vtable and specify which method we're calling, we would need some other method of specifying. C++ ends up using the "::" operator to do this. If you want to call a specific parent class version, you just call it with something like:
ParentClass::MethodA()
It becomes a real mess...
Does any of this help at all? I'm probably not doing this discussion justice. Have you done any C++ in the past?
I, for one, do not think the problem was that the band was down. I think that the problem may have been that there was a Stonehenge monument on the stage that was in danger of being crushed by a dwarf.
-David St. Hubbins
|
|
|
|
|
Hello all,
I have create a Backup application, but for now I have a little problem. I can't copy a file who was already in use. It was a big problem for me, cause I never know when the file will be in use. What can I do to bypass this problem. To make my program I have use this function that I have found on code project:
public static void copyDirectory(string Src,string Dst)
{
String[] Files;
if(Dst[Dst.Length-1]!=Path.DirectorySeparatorChar)
Dst+=Path.DirectorySeparatorChar;
if(!Directory.Exists(Dst)) Directory.CreateDirectory(Dst);
Files=Directory.GetFileSystemEntries(Src);
foreach(string Element in Files)
{
// Sub directories
if(Directory.Exists(Element))
copyDirectory(Element,Dst+Path.GetFileName(Element));
// Files in directory
else
File.Copy(Element,Dst+Path.GetFileName(Element),true);
}
}
If somebody can help me it will be nice.
Demo
|
|
|
|
|
This is far too low-level for .NET. It requires direct file system access since Windows denies access to open file handles. Most backup software will skip open files (which is why many companies disconnect their servers during backups). Backup Exec has a separate module for open file handles and you have to pay quite highly for it. Hopefully you've got the idea that you can't just solve this by setting a flag somewhere.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thank you for your answer,
but is it possible to see the state of a file. I Means can I check if the file I want to copy was open, and if it is delayed my copy.
Thanks
Demo
|
|
|
|
|
Thank you for your answer,
but is it possible to see the state of a file. I Means can I check if the file I want to copy was open, and if it is delayed my copy.
Thanks
Demo
|
|
|
|
|
Well, an exception is being thrown, right? Use that to your advantage.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Does anyone know of an API for controlling the output of a surround sound eg. 5.1 system?
I want to be able to say: play this wave file through the left rear speaker only.
Thanks
-Matt
|
|
|
|
|
DirectX[^] - as an added bonus, 9.0 introduced managed assemblies for easy access for .NET languages.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Yeah, I know. I tried using DirectSound under DirectX 9.0 but AFAIK you can't do what I want - localise a speaker.
The best you can do is set a sound source to be from an xyz position - but that won't localise a speaker.
-Matt
|
|
|
|
|
Hi there
Can anyone tell me how i can change the action of the
close button on the control box of the form.
i want to hide the form rather than to close it when
the close button is pressed.
VisionTec
|
|
|
|
|
Handle the Form.Closing event (or override the related method for derived classes), cancel it, and do something else:
public class MyForm : Form
{
protected override void OnClosing(CancelEventArgs e)
{
this.Hide();
e.Cancel = true;
base.OnClosing(e);
}
}
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi,
I am wondering if you guys are binding controls directly to your win forms with datasets? The reason why I am asking is because back in VS 6 in good old VB, it was frowned upon to bind recordsets to controls. One it had an impact on performance. Two it bloated your app with MS behind the covers code. Is this still happening with VS .net now or has it got alot better? Are alot of you binding your controls directly to datasets?
Thanks,
JJ
|
|
|
|
|
We do it all the time and it's fine. Because of the nature of data-binding (such as discovery and the binding itself), it's bound to be a little slower than a tightly-coupled solution, but this can be improved greatly by using strongly-typed DataSet classes, which you can make using VS.NET with a DataSet item (right-click on project or project folder, select Add->Add New Item...->DataSet), or by creating an XML schema and using xsd.exe to generate a strongly-typed DataSet class from the schema (this is basically what VS.NET does, only it uses add-in code that works much the same way, if not used by xsd.exe). A big reason for this is because when you refer to columns by name, a lot of look-ups are performed (more than you might think - use ildasm.exe to view the IL for the System.Data assembly and the relevent classes). When you create a strongly-typed DataSet , those column indexes are known at run-time and binding is faster.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I'm using Strong typed datasets but I'm not binding them to controls other than a datagrid or treeview. So I should wire all controls on form.
Lets say 20 to 25 at most and this should be ok with performance of 25 to 50 users using the app?
Design spec:
10 Winforms - 20 to 25 controls on each
25 to 50 users
JJ
|
|
|
|
|
It would work well for me, but whether or not it works well for your requirements is something only you can judge.
Also, 25-50 people won't be using the app. 1 person will be using one application. 25 people will be using 25 separate instances of an application. Those instances querying against the RDBMS will work just fine on a decent DB machine so long as your queries are written correctly and take locking into account when necessary. That has nothing to do with bindings, which is done per instance of the application.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hello all,
I am working on a project that requires the use of .NET remoting, however I am experiencing some strange issues and I wonder if anyone can help me.
I am exposing a preexisting object through the use of RemotingServices.Marshal
After setting up all of the appropriate ChannelServices, I expose the object via:
<br />
RemotingServices.Marshal(ObjName,"RemoteObjName");<br />
This object however has public properties which can be gotten or set. However, when I execute the following code:
<br />
ObjType remInt = (ObjType)Activator.GetObject(typeof(ObjType) ,"tcp://localhost:9555/RemoteObjName");<br />
<br />
remInt.p1 = 15<br />
Console.WriteLine(remInt.p1)<br />
In this case 15 is printed, just as one would expect, however to all other remoting clients and the process that has marshaled the remote object p1 does not change. It maintains the value that it held previously.
If anyone has experienced a similar problem and can provide me any assistance, I would greately appreciate it.
Thanks
|
|
|
|
|
Hi there
I am having problems using the XmlDocument class when editing an xml file.
This is what my xml file looks like :
<VTs_InfoKeeper>
<WebPwds>
<WebPwd>
<website>Code Project</website>
<uid>visiontec</uid>
<pwd>somefakepwd</pwd>
<description>gr8 website. c# fourms and more.</description>
</WebPwd>
</WebPwds>
</VTs_InfoKeeper>
This is the way i am using XmlDocument :
private bool SaveXml( string WebsiteName,
string UserId,
string Pwd,
string Description )
{
XmlDocument doc = new XmlDocument();
try
{
doc.Load("vtinfokeeper.xml");
}
catch(Exception)
{
return false;
}
XmlElement newWebPwd = doc.CreateElement("WebPwd");
XmlElement newWebsite = doc.CreateElement("website");
newWebsite.InnerText = WebsiteName;
newWebPwd.AppendChild(newWebsite);
XmlElement newUID = doc.CreateElement("uid");
newUID.InnerText = UserId;
newWebPwd.AppendChild(newUID);
XmlElement newPwd = doc.CreateElement("pwd");
newPwd.InnerText = Pwd;
newWebPwd.AppendChild(newPwd);
XmlElement newDescr = doc.CreateElement("description");
newDescr.InnerText = Description;
newWebPwd.AppendChild(newDescr);
doc.DocumentElement.AppendChild(newWebPwd);
XmlTextWriter tr = new XmlTextWriter("vtinfokeeper.xml",null);
tr.Formatting = Formatting.Indented;
try
{
doc.WriteContentTo(tr);
}
catch(Exception)
{
return false;
}
tr.Close();
return true;
}
Now when i run the above code i get this result :
<VTs_InfoKeeper>
<WebPwds>
<WebPwd>
<website>Code Project</website>
<uid>visiontec</uid>
<pwd>vtlives</pwd>
<description>gr8 website. c# fourms and more.</description>
</WebPwd>
</WebPwds>
<WebPwd>
<website>somwwebsite</website>
<uid>somewuid</uid>
<pwd>somwpwd</pwd>
<description>somedescr</description>
</WebPwd>
</VTs_InfoKeeper>
Whereas i wish to accomplish the result below :
<VTs_InfoKeeper>
<WebPwds>
<WebPwd>
<website>Code Project</website>
<uid>visiontec</uid>
<pwd>vtlives</pwd>
<description>gr8 website. c# fourms and more.</description>
</WebPwd>
<WebPwd>
<website>somwwebsite</website>
<uid>somewuid</uid>
<pwd>somwpwd</pwd>
<description>somedescr</description>
</WebPwd>
</WebPwds>
</VTs_InfoKeeper>
What am i doing wrong?
Can anyone plz point out the correct way that i should use
the XmlDocument class to get my desired result.
And as u can see that the pwd field is in clear text,
which way of encryption should i use to secure the pwds?
VisionTec
|
|
|
|
|
You loaded the document, then you told the document to create a new node. It doesn't know you want it inside the WebPwds element, so it just adds it to the end of its children (that means it adds it after the WebPwds element).
You need to call "AppendChild" or something similar from the XmlNode you want to add to. You could for instance do something like:
XmlNode webPwdsNode = doc.FirstChild;
webPwds.AppendChild(newPwdNode);
where newPwdNode is an XmlNode you created that contains all of the information you want.
I personally normally load in a document (I usually validate with a schema too when I load stuff in) and create classes based upon what I read (I do this by hand instead of using serialization). When it's time to write it out, I then create a XmlTextWriter and write out the document from scratch.
I, for one, do not think the problem was that the band was down. I think that the problem may have been that there was a Stonehenge monument on the stage that was in danger of being crushed by a dwarf.
-David St. Hubbins
|
|
|
|
|
<br />
using System;<br />
using System.Threading;<br />
<br />
namespace ConsoleApplication<br />
{<br />
class Page<br />
{<br />
private int _page;<br />
<br />
public Page()<br />
{<br />
_page = 0;<br />
}<br />
<br />
public int CurrentPage<br />
{<br />
get<br />
{<br />
lock(this)<br />
{<br />
_page++;<br />
return _page;<br />
}<br />
}<br />
}<br />
}<br />
<br />
class Class1<br />
{<br />
static bool stop = false;<br />
static Page page = new Page();<br />
<br />
[STAThread]<br />
static void Main(string[] args)<br />
{<br />
Thread[] threads = new Thread[5];<br />
<br />
for (int i = 0; i < threads.Length; i++)<br />
{<br />
threads[i] = new Thread(new ThreadStart(ThreadProc));<br />
threads[i].Name = Convert.ToString(i);<br />
threads[i].Start();<br />
}<br />
Console.ReadLine();<br />
}<br />
<br />
static void ThreadProc()<br />
{<br />
int i;<br />
while (!stop)<br />
{<br />
i = page.CurrentPage;<br />
Console.WriteLine("Thread {1}: Current page number: {0}", i, Thread.CurrentThread.Name);<br />
Console.WriteLine("Thread {1}: Downloading page {0}", i, Thread.CurrentThread.Name);<br />
Console.WriteLine("Thread {1}: Processing page {0}", i, Thread.CurrentThread.Name);<br />
if (i == 10) stop = true;<br />
}<br />
}<br />
}<br />
}<br />
Of course instead of the Console.WriteLine statements, there will actually be functions that perform the action.
What I'm worried about is the following: Since each thread is working with the same ThreadProc, isn't this at all going to affect the local variable (integer) i that refers to the page?
When I actually ran this code, I didn't get the statements to execute in order. Is that okay under the code circumstances? Again, what I mean is when one thread is executing, say downloading, it should then start processing the downloaded page. If that doesn't happen, and another thread, instead, starts downloading it's own page, wouldn't that affect the page processing of the first thread? I guess this is just another method of asking the same question: is anything going to happen to the local variable i in between thread calls?
Am I doing the right thing here, or should I use another method to do what I'm trying to do: Download 5 pages at a time. For each downloaded page: process it, and store information. When reaching 100 pages, stop.
Sammy
"A good friend, is like a good book: the inside is better than the cover..."
|
|
|
|
|
The thread proc is the method that's being executed in a separate thread, so no, the local variable i will NOT be affected. It's only when all the threads access a shared resource - like if you declared i as a field in the class that contains the thread proc. In this case, you can simply use the lock keyword against a shared object (typically in this case, you can lock against typeof(MyClass) so that only one thread can access that object at a time:
public class MyClass
{
private int i;
public MyClass()
{
for (int j=0; j<5; j++)
new Thread(new ThreadStart(this.Increment)).Start();
}
private void Increment()
{
lock(this)
i++;
}
}
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hey,
I receive always following error :
{"Could not bind to the new value member.\r\nParameter name: value" }
System.ArgumentException
I don't find the problem in my source. Can someone have a look to it.
I use the property "Omschrijving" for the displaymember and that is not a problem. Why I have a problem with the property Id? I also check the whole array and there are no null-values in Id.
tkx,
Jac
My code :
..
this.Cmb01.DataSource = Codes.Splitsingen.Items;
this.Cmb01.DisplayMember = "Omschrijving";
this.Cmb01.ValueMember = "Id";
..
In an other class :
public class Splitsingen
{
public static Splitsing Jaarlijks = new Splitsing (1,"1",1,12,"Jaarlijks","Annuel","Yearly");
public static Splitsing Halfjaarlijks = new Splitsing (2,"2",2,6,"Halfjaarlijks","Semestriel","6months");
public static Splitsing[] Items = new Splitsing[5]{Jaarlijks,Halfjaarlijks,Trimestrieel,Maandelijks,Eenmalig};
}
public class Splitsing : CodeX
{
public int AantalPeriodesPerJaar;
public int AantalMaandenPerPeriode;
internal Splitsing(int id, string code, int aantalPeriodesPerJaar, int aantalMaandenPerPeriode, string omschrijvingN, string omschrijvingF, string omschrijvingE) : base(id, code, omschrijvingN, omschrijvingF, omschrijvingE)
{
AantalPeriodesPerJaar = aantalPeriodesPerJaar;
AantalMaandenPerPeriode = aantalMaandenPerPeriode;
}
public Splitsing()
{
}
}
public abstract class CodeX
{
public int Id;
private string OmschrijvingN;
public string Code;
public CodeX()
{
}
internal CodeX(int id, string code, string omschrijvingN)
{
Id = id;
Code = code;
OmschrijvingN = omschrijvingN;
}
public string Omschrijving
{
get
{
if (OmgevingParameter.Taal == Talen.Ned)
{
return OmschrijvingN;
}
}
set
{
}
}
}
|
|
|
|
|