Solved

c# custom control add a bool to property

Posted on 2011-02-17
7
740 Views
Last Modified: 2012-05-11
hi all,

im trying to add a true false to one of my custom properties

ive googled and i think adding an enum makes it work?
but im not getting nowhere just errors all over

can someone show me how its done?

Thanks
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Windows.Forms;

    public enum Text { true, false;} 
    [DisplayName("Text")]
    [Description("Adds text to the right of the button")]
    public Text { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Text == true)
        {
            InsertText = "working";
        }
    }

Open in new window

0
Comment
Question by:awilderbeast
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 3

Expert Comment

by:imaki06
ID: 34915226
You are missing variable name from the line 11.

change that to something like:
  public Text  mytext { get; set; }
(remember to change line 15 to contain that variable)
0
 
LVL 12

Assisted Solution

by:Praveen Kumar
Praveen Kumar earned 250 total points
ID: 34915231
Try this:

I changes where you declare property called Text.

public enum Text { true, false;}

    [DisplayName("Text")]
    [Description("Adds text to the right of the button")]
    public Text MyText { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (MyText == true)
        {
            InsertText = "working";
        }
    }
0
 
LVL 5

Expert Comment

by:makber
ID: 34915246
Hi,
Try the following code

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Windows.Forms;

    public enum TextEnum { True=1, False=2;} 
    [DisplayName("MyText")]
    [Description("Adds text to the right of the button")]
    public TextEnum MyText{ get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (MyText == TextEnum.True)
        {
            InsertText = "working";
        }
    }

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:chTeja
ID: 34915326
Hi,
Please find the answers to your queries below.

im trying to add a true false to one of my custom properties
Please note that 'true' and 'false' are compiler reserved words and you cannot have those as enumeration members. It is not supported by the compiler. Instead, you can have 'True' and 'False' as enum members and do a case insensitive comparison.

can someone show me how its done?
It cannot be done using 'true' and 'false' as enum members. You can have two work arounds.

1. The property name should be different from the enum if both belong to the same class.
Ex:
public enum BooleanValue
{
 True,
 False
}

public class UrControl
{
    [DisplayName("Text")]
    [Description("Adds text to the right of the button")]
    public BooleanValue Text { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.Text.ToString().ToLower() == BooleanValue.True.ToString().ToLower())
        {
            InsertText = "working";
        }
    }
}
2. Just Delete the enum and have Text as a boolean property.

[DisplayName("Text")]
    [Description("Adds text to the right of the button")]
    public bool Text { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.Text == true)
        {
            InsertText = "working";
        }
    }

Let me know if this helps.
0
 
LVL 2

Accepted Solution

by:
chTeja earned 250 total points
ID: 34915335
Hi,
Sorry ignore the first workaround in my previous comment.
Please find the answers to your queries below.

im trying to add a true false to one of my custom properties
Please note that 'true' and 'false' are compiler reserved words and you cannot have those as enumeration members. It is not supported by the compiler. Instead, you can have 'True' and 'False' as enum members and do a case insensitive comparison.

can someone show me how its done?
It cannot be done using 'true' and 'false' as enum members. You can have two work arounds.

1. The property name should be different from the enum if both belong to the same class.
Ex:
public enum BooleanValue
{
 True,
 False
}

public class UrControl
{
    [DisplayName("Text")]
    [Description("Adds text to the right of the button")]
    public BooleanValue Text { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.Text == BooleanValue.True)
        {
            InsertText = "working";
        }
    }
}
2. Just Delete the enum and have Text as a boolean property.



[DisplayName("Text")]
    [Description("Adds text to the right of the button")]
    public bool Text { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.Text == true)
        {
            InsertText = "working";
        }
    }

Let me know if this helps.
0
 
LVL 1

Author Comment

by:awilderbeast
ID: 34915454
ok im getting no errors in the code but for some reason its set to true for all my controls, even though ive only set
Text ="true" on two of the controls

is it by defauly set to true? can i set it to false by default?

Thanks
0
 
LVL 1

Author Closing Comment

by:awilderbeast
ID: 34916104
sorry error was my bad, outputting wrong variable

thanks guys
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need help for captcha 2 41
Best book for Internet security 4 52
Windows Universal App 22 58
Intermittent OleDbConnection Error 20 51
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question