Interface Question

R

Razzerroo

Hello all
I'm currently converting me and our code from VB.NET to C# for the
company I work for.

I have an VB.NET interface that has an excerpt like this:

Public Interface ISensorCameraSettings
..
..
..
Property Exposure(ByVal ChannelName As String) As Single
Property Gain(ByVal ChannelName As String) As Integer
Property BitRange(ByVal ChannelName As String) As Integer
..
..
..
End Interface

I can't figure the syntax to convert this to C#. . .
I've tried many iterations, I'm sure I just havent found the correct
thread on google or msdn?

something like:
public interface ICameraSettings
{
/// <summary>
/// Set the exposure for this sensor.
/// </summary>
void set_Exposure(string DataBufferName,float value);
float get_Exposure(string DataBuffername);
}
???

Please help.

Sincerely,

Kevin Christopher
 
A

Ajay Kalra

I have never used VB.Net but you seem to want is to declare an
interface in C# and expose get/set properties. Its something like this:

public interface ICameraSettings
{
float Exposure
{ get; set;
}
}
 
R

Razzerroo

Thank you for the response. You left out the arguments, and adding
them in like this

float Exposure(string DataBufferName){get;set;}

Results in an "Interface members cannot have a definition" error.

-Kevin
 
M

Mattias Sjögren

I can't figure the syntax to convert this to C#. . .

C# doesn't support non-default (i.e. non-indexer) parameterized
properties. So you can't write that in C#.



Mattias
 
A

Ajay Kalra

Thanks for pointing it out. I was kind of lost when I saw what VB is
allowing to do.

So, in this case, the best bet would be to simply write methods to
achieve the same? Correct?
 
R

Razzerroo

Thank you for the quick response guys, this is awesome. I finally
figured out what to ask for on the newsgroup ("parameterized
properties") and found results that confirm Mattias' response.

Bummer! That means that this interface cannot be converted without
breaking it, and all the apps that rely on it.

Instead of raising a rukus and starting a vb-c# debate, I'll back away
slowly . . . (c;

Thank you all for the expedient support.


Sincerely,

Kevin
 
R

Razzerroo

Yes, I guess that's what I'll have to do. Unfortunately, this will
break the interface; and all those relying on it.

Thanks for the response Ajay.

-Kevin
 
G

Guest

The C# equivalent is an indexer, which requires enumerating the values. In
essence, what you desire is the ability to do the following (VB.NET code):

MyClass.Exposure("channel1") = 1
MyClass.Exposure("channel2") = 2

In C#, the closest is something like:

MyClass.Exposure[1] = 1;
MyClass.Exposure[2] = 2;

This, however, means you have to trap the indexed values somewhere, like:

Hashtable lookup = new Hashtable();
lookup.Add("channel1", 1);
lookup.Add("channel2", 2);

You can then run against the object like so:


MyClass.Exposure[Convert.ToInt32(lookup["channel1"])] = 1;
MyClass.Exposure[Convert.ToInt32(lookup["channel2"])] ] = 2;

This is an architectural difference between C# and VB.NET. But, there is a
good reason. In VB.NET, this is perfectly legal:

Public Property Exposure(ByRef UnrelatedCrap1 As String, _
ByRef UnrelatedCrap2 As String, _
ByRef ObjectInitializationSettings As
Hashtable, _
ByRef DisposeMeNow As Boolean, _
ByRef DoAWebServiceLookup As Boolean, _
) As Single
If (UnrelatedCrap1 = "MakeTheObjectDance") Then
MakeTheObjectDance()
End If
End Property

And, believe it or not, there are people who have done this in apps (of
course, it meant I had to explain the bloodstains in my trunk ;->).

---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 

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