Converting Properties from VB to C#


Author
Message
Bill Cunnien
Bill Cunnien
StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)
Group: Forum Members
Posts: 785, Visits: 3.6K
I'd like to get the following code (from InheritedUI DevEx.TextEdit control) to work in C#:





.Private _ControlSourceUpdateMode As ControlUpdateMode = ControlUpdateMode.OnPropertyChanged

.Public Property ControlSourceUpdateMode() As System.Windows.Forms.ControlUpdateMode Implements IBusinessBindable.ControlSourceUpdateMode

. Get

. Return Me._ControlSourceUpdateMode

. End Get

. Set(ByVal value As System.Windows.Forms.ControlUpdateMode)

. Me._ControlSourceUpdateMode = value

. End Set

.End Property





Here is what I have, so far:





.private ControlUpdateMode _ControlSourceUpdateMode = ControlUpdateMode.OnPropertyChanged;

.public ControlUpdateMode IBusinessBindable.ControlSourceUpdateMode

.{

. get { return _ControlSourceUpdateMode; }

. set { _ControlSourceUpdateMode = value; }

.}





I think that Implements keyword in VB is throwing me off. Can anyone help me with this?



Thanks!!

Bill



P.S. How do I get the copied code to follow the proper indenting after I paste it? I know there must be some trick...I just cannot remember it.

Bill Cunnien
Bill Cunnien
StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)
Group: Forum Members
Posts: 785, Visits: 3.6K
By the way, the error that I am getting is "The modifier 'public' is not valid for this item."
Bill Cunnien
Bill Cunnien
StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)
Group: Forum Members
Posts: 785, Visits: 3.6K
To get past the error, I simply removed the "public" keyword.



Now, I would like to add this item to a group on the toolbox. How would I do that?
Greg McGuffey
Greg McGuffey
Strategic Support Team Member (3.3K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
In C#, the how interface thing works is a bit different. I still have some bruises on my forehead healing from figuring the differences out.



In VB, you use the whole Implements keyword thing. This allows you to rename a property/method from that in the interface.



In C#, you either implement an interface implicitly or explicitly. When doing it implicitly, you just create a property/method/event that matches the one in the interface and you're done. An interesting note is that the property/method/event just had to be implemented somewhere in the inheritance chain. I.e one property can be implemented in current class and another in a base class.



If you do it explicitly, then you use the interface within the name of the property or method, as you are dong here:



public ControlUpdateMode IBusinessBindable.ControlSourceUpdateMode




However, when you do this explicitly, the property is only available when the object is cast as the interface object. I.e. if the above is defined as a property in a class called MyTextbox and you have an instance of this control a form and it's called txtTest then:



-- This will not work, as ControlSourceUpdateMode is NOT a property of MyTextbox

ControlUpdateMode mode = this.txtTest.ControlSourceUpdateMode;



-- You have to cast...

ControlUpdateMode mode = ((IBusinessBindable)this.txtTest).ControlSourceUpdateMode;




And as you've also discovered, when you explicitly define an interface, you can't use modifiers. They are assumed public, because they are only available when the object is cast as an interface object. If this is confusing to anyone, you're understanding where the bruises on my forehead came from... Crazy



What you actually need to do to get what you likely want (a public property on the control that implements the ControlSourceUpdateMode that is accessable directly from the control without casting) is to:



- explicitly define that you want to implement IBusinessBindable on the subclassed control:

public class MyTextBox : sf.Textbox, IBusinessBindable)

- Get rid of the IBusinessBindable reference in the property definition.

- Add the public modifier back on it.



As mentioned earlier, C# can handle some of the interface being implemented in the current class and some of it in bases classes. It also seems to handle just fine the fact that you are re-implementing IBusinessBindable on your new class, even though the base class is already implementing it.



Does that make sense?



Bill Cunnien
Bill Cunnien
StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)
Group: Forum Members
Posts: 785, Visits: 3.6K
Thanks, Greg...that does help explain what is going on with the implements and interfaces that I am dealing with. I am actually reworking the source code from the InheritedUI for the DevEx TextEdit control. Since it is all in VB, I thought I would start by converting it to C#. Next I want to add the newly formed control to the toolbox. Alas! That does not seem to want to work. I'll keep plugging away. I am learning a lot of new things. That, in itself, is exciting and is definitely keeping me going.
Greg McGuffey
Greg McGuffey
Strategic Support Team Member (3.3K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
Oh, the toolbox thing, try adding this attribute to the class:



[ToolboxItem(true)]

public class MyTextBox



Not sure why, but sometimes you need to add this to get your controls/components to show up in toolbox (after a build of course).

Bill Cunnien
Bill Cunnien
StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)
Group: Forum Members
Posts: 785, Visits: 3.6K
The attribute exists...and a rebuild has occurred. The item is now in a group on the ToolBox. I can drag and drop it onto a form.



Now, I cannot get the new SF properties to show up (BindingField, BusinessObject, etc.). My BindingField property looks like this:





[Category(BusinessMod.EDITOR_CATEGORY),

Description(BusinessMod.EDITOR_BINDINGFIELD_DESC),

DefaultValue(""),

EditorAttribute(ControlConstants.EDIT_BindingFieldEditor, typeof(System.Drawing.Design.UITypeEditor))]

string IBusinessBindable.BindingField

{

get { return _BindingField; }

set { _BindingField = value; }

}





Basically copied from the source code. I must have missed something in the translation.

Greg McGuffey
Greg McGuffey
Strategic Support Team Member (3.3K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
This is related to interface implementation. This property:



string IBusinessBindable.BindingField { get; set;}




is not a property of the control, but of the interface the control implements because you've implemented it explicitly. Try doing this instead:



public string BindingField { get; set;}




and be sure that your class indicates your implementing IBusinessBindable.



Note that one of the IBusinessBindable properties is renamed. IBusinessBindable.DataSourceUpdateMode is renamed as BindingUpdateMode. If you want to do this in C# you'll need to do something like this:



//-- Implement BindingUpdateMode as normal property

private DataSourceUpdateMode _bindingUpdateMode;

public DataSourceUpdateMode BindingUpdateMode

{

  get { return _bindingUpdateMode }

  set{ _bindingUpdateMode = value }

}



//-- Implement DataSourceUpdateMode explicitly, just referencing our BindingUpdateMode property

DataSourceUpdateMode IBusinessBindable.DataSourceUpdateMode

{

  get { return _bindingUpdateMode }

  set{ _bindingUpdateMode = value }

}




Like I said, bruises to my forehead (due to high speed impacts with my desk).
Bill Cunnien
Bill Cunnien
StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)
Group: Forum Members
Posts: 785, Visits: 3.6K
Ok. Makes sense. I think. Sort of. Kinda.



Here is my adjusted code:





private string _BindingField = "";

[Category(BusinessMod.EDITOR_CATEGORY),

Description(BusinessMod.EDITOR_BINDINGFIELD_DESC),

DefaultValue(""),

EditorAttribute(ControlConstants.EDIT_BindingFieldEditor, typeof(System.Drawing.Design.UITypeEditor))]

public string BindingField

{

get { return _BindingField; }

set { _BindingField = value; }

}





I thought that by using the SF constants that I would be able to see these properties in the control, but they still do not show up. I believe I am missing the boat here somewhere. I haven't resorted to head-bashing (my own or others), but I can see how that might happen.



Bill
Bill Cunnien
Bill Cunnien
StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)
Group: Forum Members
Posts: 785, Visits: 3.6K
The properties are accessible from code but not in the designer. What is the trick?
GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search