2

I wrote this class as part of a custom control in VB.Net. It lets me toggle the state of 3 linked labels, and set the text of each one using the Text property. That is all the user is allowed to do with it. I can convert the Enabled property and constructor without issue, however I am unsure as the best method to convert the Text property. A function would take 2 arguments, and an indexer would act on LabelExtender3, not on Text as it currently does in VB.Net. So what is the correct way to convert something like this?

Public Class LabelExtender3
    Private lblTemp(2) As Label

    Public WriteOnly Property Enabled As Boolean
        Set(value As Boolean)
            If value Then
                lblTemp(0).ForeColor = Color.MediumBlue
                lblTemp(1).ForeColor = Color.MediumBlue
                lblTemp(2).ForeColor = Color.MediumBlue
            Else
                lblTemp(0).ForeColor = Color.SteelBlue
                lblTemp(1).ForeColor = Color.SteelBlue
                lblTemp(2).ForeColor = Color.SteelBlue
            End If
        End Set
    End Property

    Public WriteOnly Property Text(ByVal index As Integer) As String
        Set(value As String)
            lblTemp(index).Text = value
        End Set
    End Property

    Friend Sub New(ByRef value1 As Label, ByRef value2 As Label, ByRef value3 As Label)
        lblTemp(0) = value1
        lblTemp(1) = value2
        lblTemp(2) = value3
    End Sub
End Class
  • possible duplicate of [Is it possible to make multi-parameter properties?](http://stackoverflow.com/questions/21190900/is-it-possible-to-make-multi-parameter-properties) – GSerg Jul 07 '15 at 15:25

3 Answers3

2

You've come across a feature that VB.NET has that C# does not: indexable properties.

More specifically, C# lacks the ability to declare them, but it is able to consume them (which was added in C# 4.0) and that might be limited to COM Interop use.

Your best bet it to just make a method in this case:

public void SetText(int index, string value)
{
    lblTemp[index].Text = value;
}
vcsjones
  • 138,677
  • 31
  • 291
  • 286
1

C# does not allow properties with parameters so there is no direct conversion of this but you can just create a method instead.

Try Using Instant C# I find it the best tool for conversions (I am in no way affiliated to this company):

//INSTANT C# NOTE: C# does not support parameterized properties - the following property has been rewritten as a 'set' method:
//ORIGINAL LINE: Public WriteOnly Property Text(ByVal index As Integer) As String
    public void set_Text(int index, string value)
    {
        lblTemp[index].Text = value;
    }
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
  • 1
    You are not using proper naming conventions. – fsacer Jul 07 '15 at 15:15
  • 2
    @fsacer People can use whatever naming conventions they want. Secondly, the code is from a conversation tool. The conversion tool is likely using the name `set_Text` because that is what the setter method is named in CIL when compiled by VB.NET. – vcsjones Jul 07 '15 at 15:19
0

In C# WriteOnly property equates to a C# property with only a set part. But you cannot pass the parameter to C# property unless it is an indexer so it becomes a method. So you would convert the code into:

public class LabelExtender3
{

    private Label[] lblTemp = new Label[3];
    public bool Enabled
    {
        set
        {
            if (value)
            {
                lblTemp[0].ForeColor = Color.MediumBlue;
                lblTemp[1].ForeColor = Color.MediumBlue;
                lblTemp[2].ForeColor = Color.MediumBlue;
            }
            else
            {
                lblTemp[0].ForeColor = Color.SteelBlue;
                lblTemp[1].ForeColor = Color.SteelBlue;
                lblTemp[2].ForeColor = Color.SteelBlue;
            }
        }
    }

    public string SetText(int index, string value)
    {
        lblTemp[index].Text = value;
    }

    internal LabelExtender3(ref Label value1, ref Label value2, ref Label value3)
    {
        lblTemp[0] = value1;
        lblTemp[1] = value2;
        lblTemp[2] = value3;
    }
}
fsacer
  • 1,382
  • 1
  • 15
  • 23