Solved

c# custom control add a bool to property

Posted on 2011-02-17
7
704 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
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:Mamtha1982
Mamtha1982 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Two different visual studio versions 3 23
Change Format on Data when exporting to Excel 4 23
Variable Event ? 3 23
ASP.NET MVC 2 6
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now