Solved

c# custom control add a bool to property

Posted on 2011-02-17
7
689 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 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

12 Experts available now in Live!

Get 1:1 Help Now