|
You can implement the ICheck interface in any class/struct you like. You can then use the same method an any instances of any of those classes.
|
|
|
|
|
You can only derive a class from a single other class.
For example
public partial class frmMain : Form
{
...
}
But you can inherit as many Interfaces as you need.
For example
public partial class frmMain : Form, ICloneable, IComparable, IDisposable
{
...
}
(Not that I am suggesting that is a sensible Interface list for a form, it's just an example)
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
In your example, there's very little advantage. Where the use of the interface comes into its own is when you want to perform a specific method, e.g. Dispose an object. Rather than having to use reflection to determine whether or not an object contains that method, you can use the interface to do the action. Consider the following snippet:
Data obj = new Data();
ICheck check = obj as ICheck;
if (check != null)
check.Fun(); Now, if Data doesn't implement this interface, the code still compiles and behaves as you'd expect. This really comes into its own if you are using something like Inversion of Control where you'd use the interface to retrieve the object, e.g.:
ICheck obj = MyIocContainer.Resolve(
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Onyx
modified on Saturday, September 11, 2010 8:14 AM
|
|
|
|
|
Pete O'Hanlon wrote: Now, if Data doesn't implement this interface, the code still compiles and behaves as you'd expect.
Oh my god, no! If Data does not implement ICheck the code would not compile, at least in the statically typed polymorphic world!
|
|
|
|
|
I checked the code I typed in and realised I'd forgotten to put as ICheck on the relevant line. I've amended it, and the code works exactly as I described. Thanks for bringing this to my attention.
|
|
|
|
|
Very little with a local variable -- very much with a parameter or property.
|
|
|
|
|
An important one - the using () pattern wouldn't work without the use of IDisposable .
|
|
|
|
|
Go and google for Dependency inversion. Basically, you use interfaces to loosen dependencies between components. Consider:
public class Data : IData {
private IDataChecker mDataChecker;
public Data(IDataChecker pDataChecker) {
mDataChecker = pDataChecker;
}
}
Your concrete Data depends only on IDataChecker interface, not on its implementation. The consequence is that you can use many different implementations of IDataChecker without changing Data .
|
|
|
|
|
I've written multithreaded code using MFC and understand that well. I'm working on a C# code in which I want to fill in an ArrayList with some data, using a separate thread, and then use the ArrayList data to populate a ListBox on my Form.
Can someone point me to a good example of how to pass data back from a thread in C#? I've found some examples using BackgroundWorker ..., but I'm not sure if that is the only way, or the best way, etc. None of my C# books have anything but very simple threading examples.
Thanks,
Tom
|
|
|
|
|
This article[^] will tell you how your thread can access the GUI parts. And yes a BackgroundWorker often is the best way to achieve this.
|
|
|
|
|
I'm new to online forums discussion, but I try to be clear.
I'm developing a basic conversion from VB to C#. This portion of the code is a user input, ex..inter a number click - print out Is it a number: true or Is it a number: false.
If user input anything other than a number, print out will be Is it a number: false
I have coverted, but getting error: Return error "expected class, delegae, enum, interface, or struct"
I have a red line under void.
I tried the web site that was mentioned on a similar question, but still got red line under void.
Protected Sub btnStep2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim bTest As Boolean
Dim iNum As Integer
'Integer.TryParse is used to see if the compiler can convert
' the string to an Integer
' It returns a Boolean and if successful
' iNum will hold the numeric result.
bTest = Integer.TryParse(txtStep2.Text, iNum)
lblData.Text = "Is it a number: " & bTest.ToString()
End Sub
protected void btnStep2_Click(object sender , System.EventArgs e )
bool bTest = false;
int iNum = Integer;
{
bTest = Integer.TryParse(txtStep2.Text, iNum);
lblData.Text = "Is it a number: " & bTest.ToString();
}
modified on Wednesday, September 8, 2010 11:58 PM
|
|
|
|
|
1.
I don't see any question. Do you have one?
If there are compile errors or run-time exceptions, you should post them, and indicate what line they apply to.
2.
please use PRE tags when publishing multi-line code snippets, they improve readability, and hence the quality of the replies.
3.
TryParse works at run-time, it does not help the compiler at all.
4.
I think it is int.TryParse(...) .
Furthermore the method returns a boolean flag to indicate result; only when true is returned, the parsing succeeded, so you should not claim "It is a number" when false gets returned.
|
|
|
|
|
Your opening braces are two lines too low.
|
|
|
|
|
A few things are wrong here. First of all, you are trying to declare two variables, bTest and iNum, outside of the method body... you can't do that. Put them inside the curly braces. Second, you are trying to initialize iNum to "Integer", which is a datatype, not a value; that will not work. Instead, assign it a value (e.g., 0). Third, I'm pretty sure TryParse expects the second parameter to be passed either by reference or as an output variable. In C#, you must explicity indicate that, as with this example:
Integer.TryParse("0", ref iNum);
|
|
|
|
|
I try to hide/show panels with controls depending on what TreeView node the user clicks (for large data inputs), but it doesn't work and I have no idea why not.
This is my code for the TreeView's AfterSelect event:
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
{
if (treeView1.SelectedNode == null)
{
this.Text = "null";
}
else
{
if (treeView1.SelectedNode.Name == "Node1")
{
panel2.Visible = false;
panel1.Visible = true;
this.Text = "panel1 + | panel2 -";
}
else if (treeView1.SelectedNode.Name == "Node2")
{
panel1.Visible = false;
panel2.Visible = true;
this.Text = "panel1 - | panel2 +";
}
}
}
By changing the form text I confirmed that the event gets properly thrown and by stepping through the code I found out that if Node2 is selected, the panel2.Visible = true line does absolutely nothing and afterwards Visible is still false .
Why does that happen? I'm really confused.
Thanks.
|
|
|
|
|
I created a test project with a TreeView, some Nodes, and two Panels; your code works fine for me.
|
|
|
|
|
This stuff would work just fine, provided the controls and panels are layed out properly.
|
|
|
|
|
Sounds like Panel2 is hidden behind something else, or possibly not even added to the form correctly
|
|
|
|
|
Apparently it didn't work because I had one panel over the other.
But that is the entire point of this.
Instead of putting a ton of controls next to each other or in TapControls, I want them to appear and disappear at the same location.
How do I do that?
|
|
|
|
|
Megidolaon wrote: Apparently it didn't work because I had one panel over the other.
It's not that at all. Let me run through the sequence of events you went through to construct this:
1) create two panels, one above the other. (could be side by side, I dunno).
2) Put controls on each panel.
3) Drag one panel over the other so they are in the same place.
Now, when you make one visible and one invisible, you see one panel.
When you make that panel invisibile, and the other visible, you see no panels.
This is because when you dragged one panel onto the other it became a child of the panel you dropped it on. So when you make the master panel invisible, you hide all it's children - including the other panel.
Leave them separate, and move them with your code in the form load event. It'll work now...
Or better, use a TabControl instead of panels...it is a lot easier to maintain in the designer!
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
as Griff said, you probably have one of the panels containing the other.
There are many ways not to do that, here is one I use sometimes:
- inside Visual Designer, add first panel, smaller than intended;
- now add second panel next to the first one, also smaller than intended;
- so far you made sure they both belong to the Form directly;
- now select one, look for the Properties pane, and adjust the Location and Size properties by editing the numbers (watch the result each time you leave the property's value field);
- then select the second one (if not visible, since overlapped by the other one, use the combobox in the properties pane), and give it the appropriate (maybe the same) values for Location and Size.
The net result is you only see one of the panels in Designer, however you will see one or the other at run-time.
|
|
|
|
|
Thanks.
I didn't notice I had made panel2 the child of panel1 cause a single mm off and they were still independent.
But changing locations works great. In the Visual Designer I have the panels next to each other and just overlay them at runtime like this:
panel2.Location = new Point(panel1.Location.X, panel1.Location.Y);
I still need to test how this works with the many different controls needed on other forms.
After all the point of this is to switch from a huge cluster**** of nested tab controls to a more intuitive and easier usable design like for example VS options.
|
|
|
|
|
If the Form is going to show mostly text, I tend to create the structured parts by run-time code, i.e. code I write myself, as opposed to me doing repetitive sequences of point-and-click operations inside Visual Designer.
You could easily write a method that adds a Panel holding some TextBoxes/ListBoxes/TreeViews/whatever, positioning them, setting their Anchors/Docks, etc. Then call that method over and over.
Some advantages: it becomes easier to change your mind about layout details; and you could fetch the data from some external store (file, database, ...) and make it much more dynamic.
|
|
|
|
|
Nah, it's not that much to do.
But I tested it now with a form that has many controls for data input and it works like a charm.
In the designer it's a huge form with all panels side by side for easy editing and the first thing I do when the form is constructed is change the size and hide all panels.
Then I display the appropriate panel when selecting a node and hide all others.
|
|
|
|
|
Hi
i have created a media player i have problem in making 10 band equalizer using DirectX 9.0 in C# .net and also have a doubt that how to play .MP4 files.
I will be thankful if i get the solution.
Thank you,
NISHAd
|
|
|
|
|