Properties In C#

D

Daniel Mihaita

Hello there,
I need to code an object with two properties that binds dynamicaly,
something like bind comboboxes on web pages.
So my object needs to gave two properties Prop1 and Prop2. I need
Prop1 to take values from an enum (let's say value1 and value2), and
also Prop2 , depending on the enum selected (at DESIGN TIME) in Prop1,
to take values from two different enums. So if value1 is selected in
Prop1, Prop2 to have a combobox with values (1, 2, 3).But if value2 is
selected in Prop1, then you'll have to choose for Prop2 from values (4,
5, 6).
I'm kind a new with C#, so I'll just want some directions, please.


Regards,
Daniel
 
S

Saso Zagoranski

Here is how you write a property in C#:

class MyComboBox : ComboBox
{
private YourEnum prop1;

public YourEnum Prop1
{
get { return prop1; }
set { prop1 = value; }
}

// do the same for prop2
}

If you add this new combobox to the toolbar and then add the combobox to the
form you will
see those new properties in the Property window.

Note that the notation "prop1" for variable and "Prop1" for the property is
not neccesary.
Also note that "value" is a keyword used in properties whenever you set a
value.


saso
 
G

Guest

Hi Daniel,

I met that way with properties binding.

Let me show you how it works:

Main enum: Vehicles
The good way is to declare *dependant* enums as the same type
e.g. int: Cars, Motorcycles, Bicycles.

public Vehicles Prop1 {
get {
// return "vehicle";
}
set {
// set "vehicle" and set Prop2 on first or default value
}
}

public int Prop2 {
get {
// return "prop2";
}
set {
// if "value" is out of right Enum then throw
// ArgumentException
// set "prop2" to "value"
}
}

private Type GetProp2Type() {
// return type of dependant enum, e.g. by switch statement
}

HTH
Marcin
 
D

Daniel Mihaita

Hello Marcin,
I'm not sure I follow you. What I m meant :

public enum Prop1 {Option1, Option2};
public enum Prop2_1:int {opt1_1 , opt1_2};
public enum Prop2_2:int {opt2_1 , opt2_2};

private Prop1 _MainProp;
public Prop1 MainProp
{
get ....
set ....
}

private ????? _SecProp;
public ????? SecProp
{
get ....
set ....
}


At design time, if I choose Option1 for MainProp, then I want that
SecProp let me choose between opt1_1 and opt1_2. And the same, if I
choose Option2, SecProp to let me choose between opt2_1 and opt2_2.
I don't see from you code how to acomplish that.

Regards,
Daniel Mihaita
 
G

Guest

Hi Daniel,
Hello Marcin,
I'm not sure I follow you. What I m meant :

public enum Prop1 {Option1, Option2};
public enum Prop2_1:int {opt1_1 , opt1_2};
public enum Prop2_2:int {opt2_1 , opt2_2};

private Prop1 _MainProp;
public Prop1 MainProp
{
get ....
set ....
}

It look good till here.

Now i change some of your code:

private int _SecProp;

public int SecProp
{
get ....
set ....
}

So, if you want to set "SecProp" you shoud cast it to "int",
and check the enum type by "MainProp" using:

private Type GetSecPropType() {
// return type of dependant enum, e.g. by switch statement
}

e.g.

private bool IsSecPropValue(int val) {
int[] enumValues=(int[]) Enum.GetValues(GetSecPropType());
for(int i=0; i<enumValues.Length; i++) {
if( enumValues==val ) {
return true;
}
}
return false;
}


At design time, if I choose Option1 for MainProp, then I want that
SecProp let me choose between opt1_1 and opt1_2. And the same, if I
choose Option2, SecProp to let me choose between opt2_1 and opt2_2.
I don't see from you code how to acomplish that.

I'm afraid that this is impossible to switch those enums
at design time. Only solution is to write your own TypeConverter
or something like that, but it will need much more time.

HTH
Marcin
 

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