Interface passing

T

tshad

I am just trying to get my head around passing objects as interfaces or
classes.

If I have the following 3 files:
**********************************
File: IStatusDisplay.cs

public interface IStatusDisplay
{
string Status { get; set; }
string StatusBar { get; set; }
}

File: MapSetup.cs

namespace MapSetup
{
public partial class MapSetup : Form, IStatusDisplay
{

private void ChecktFilesThread()
{
Maintenance.CheckFiles(this);
}

string IStatusDisplay.Status
{
get{return Status.Text;}
set{Status.Text = value;}
}

string IStatusDisplay.StatusBar
{
get{ return toolStripStatusLabel1.Text;}
set{toolStripStatusLabel1.Text = value;}
}
}

Form: Maintenance.cs

public class Maintenance
{

public delegate string FixTablesDelegate(TableFieldList tableFields,
UnmappedNamesCollection unmappedNames,
DataView dv, IStatusDisplay display);

public static void CheckFiles(IStatusDisplay display)
{
string[] strFiles = null;
}
}
*************************************

I find that this is valid whether I have "display" as an:

1) "IStatusDisplay" type, which would only allow me access to "Status" and
"StatusBar", which are a subset of the MapSetup class and would not let me
have access to the Form objects of the MapSetup class.

2) "Form" type, which would let me have access to all the methods and
properties of the Form objects in the MapSetup class which is also a subset
of the MapSetup class and would not let me have access to the "Status" and
"StatusBar" properties of the MapSetup class.

This I understand.

But I can't change the CheckFiles statement to:

public static void CheckFiles(MapStatus display)

I get an error here. But I can't get access to the whole MapStatus object
which consists of both the Form and IStatusDisplay.

Why is that?

It is a type like the interface, isn't it?

By passing "this", I can get access to both the Form and IStatusDisplay
separately. Is there a way to get access to both?

Thanks,

Tom
 
P

Peter Duniho

[...]
But I can't change the CheckFiles statement to:

public static void CheckFiles(MapStatus display)

That's not a statement. It's a method declaration.
I get an error here. But I can't get access to the whole MapStatus
object
which consists of both the Form and IStatusDisplay.

Define "can't". What exactly doesn't work? And did you mean "MapSetup"
instead of "MapStatus"?

One possible reason it might not work is that your Maintenance.cs file
doesn't have access to the MapSetup type. But if you don't provide the
specific details, it's not possible to know for sure.
Why is that?

It is a type like the interface, isn't it?

By passing "this", I can get access to both the Form and IStatusDisplay
separately. Is there a way to get access to both?

Not the way you've declared MapSetup in the code you posted. You are
using "explicit implementation" for the interface, which means the
implementation is visible _only_ through the interface type, not the class
type itself. So even if you resolved the question of using "MapSetup" in
your method declaration, you still wouldn't be able to access the
interface implementation except through a variable of that type (of
course, you can always assign the value of a variable having the type
"MapSetup" to a variable having the type "IStatusDisplay"...the type of
the method argument isn't what's important, it's the type of the object
that matters; the type of the argument only affects _how_ you would have
to assign the argument value to some other variable).

If that's a problem for you, then just use "implicit implementation" for
the interface, by not qualifying with the name of the interface the names
of the members implementing the interface.

Pete
 
T

tshad

Ok, let's try again.

I built a project with these files and it does compile. I don't care that
I am not doing any work with it. Just want to see what I am missing in
handling interfaces and classes.

MapSetup.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MapSetup
{
public partial class MapSetup: Form, IStatusDisplay
{
public MapSetup()
{
InitializeComponent();
}
private void ChecktFilesThread()
{
Maintenance.CheckFiles(this);
}

string IStatusDisplay.Status
{
get { return Status.Text; }
set { Status.Text = value; }
}

string IStatusDisplay.StatusBar
{
get { return toolStripStatusLabel1.Text; }
set { toolStripStatusLabel1.Text = value; }
}
}
}

IStatusDisplay.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace MapSetup
{
public interface IStatusDisplay
{
string Status { get; set; }
string StatusBar { get; set; }
}
}


Maintenance.cs

using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Text;

namespace MapSetup
{
public class Maintenance
{
public static void CheckFiles(IStatusDisplay display) //
<=====
{
display.StatusBar = "test";
}
}
}

This works fine and if I do display. intellisense shows me Status and
StatusBar plus other normal object stuff like ToString().

If I change the CheckFiles line to:

public static void CheckFiles(Form display)

Intellisense shows me all the objects of the form that is public (not
toolStripStatusLabel1 or Status which are both textboxes).

But if I want both the Form and the IStatusDisplay, how would I change it?

public static void CheckFiles(MapSetup display)

This time it works as far as no errors. But when I set it to MapSetup, I
see all the Form stuff just like when it was Form. But it doesn't show me
Status or StatusBar.

If MapSetup inherits IStatusDisplay, why doesn't it give me those properties
as well?

Thanks,

Tom

Peter Duniho said:
[...]
But I can't change the CheckFiles statement to:

public static void CheckFiles(MapStatus display)

That's not a statement. It's a method declaration.
I get an error here. But I can't get access to the whole MapStatus
object
which consists of both the Form and IStatusDisplay.

Define "can't". What exactly doesn't work? And did you mean "MapSetup"
instead of "MapStatus"?

One possible reason it might not work is that your Maintenance.cs file
doesn't have access to the MapSetup type. But if you don't provide the
specific details, it's not possible to know for sure.
Why is that?

It is a type like the interface, isn't it?

By passing "this", I can get access to both the Form and IStatusDisplay
separately. Is there a way to get access to both?

Not the way you've declared MapSetup in the code you posted. You are
using "explicit implementation" for the interface, which means the
implementation is visible _only_ through the interface type, not the class
type itself. So even if you resolved the question of using "MapSetup" in
your method declaration, you still wouldn't be able to access the
interface implementation except through a variable of that type (of
course, you can always assign the value of a variable having the type
"MapSetup" to a variable having the type "IStatusDisplay"...the type of
the method argument isn't what's important, it's the type of the object
that matters; the type of the argument only affects _how_ you would have
to assign the argument value to some other variable).

If that's a problem for you, then just use "implicit implementation" for
the interface, by not qualifying with the name of the interface the names
of the members implementing the interface.

Pete
 
P

Peter Duniho

[...]
But if I want both the Form and the IStatusDisplay, how would I change
it?

public static void CheckFiles(MapSetup display)

This time it works as far as no errors. But when I set it to MapSetup, I
see all the Form stuff just like when it was Form. But it doesn't show
me
Status or StatusBar.

If MapSetup inherits IStatusDisplay, why doesn't it give me those
properties
as well?

Please read the previous post I wrote, which you quoted in your most
recent post. Answers to all of the above are included in that post.

Pete
 
K

kndg

tshad said:
[...]
public static void CheckFiles(MapSetup display)

This time it works as far as no errors. But when I set it to MapSetup, I
see all the Form stuff just like when it was Form. But it doesn't show me
Status or StatusBar.

If MapSetup inherits IStatusDisplay, why doesn't it give me those properties
as well?

As what Pete say previously, you are using explicit interface
implementation. What you need is implicit interface implementation.

public partial class MapSetup: Form, IStatusDisplay
{
...

public string Status
{
get { return Status.Text; }
set { Status.Text = value; }
}

public string StatusBar
{
get { return toolStripStatusLabel1.Text; }
set { toolStripStatusLabel1.Text = value; }
}
}

Regards.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top