Solved

c# custom control add a bool to property

Posted on 2011-02-17
7
715 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

803 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