Solved

c# custom control add a bool to property

Posted on 2011-02-17
7
751 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
Technology Partners: 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

Certified OpenStack Administrator Course

We just refreshed our COA course based on the Newton exam.  With 14 labs, this course goes over the different OpenStack services that are part of the certification: Dashboard, Identity Service, Image Service, Networking, Compute, Object Storage, Block Storage, and Orchestration.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

630 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