How to use enum in if statement? - asp.net

I have ASP.NET application and want to use enum in if statement.
I get variable in this way:
string choice = (string)Session["export_choice"];
if(choice == <here goes enum>)
{
}
else
{
}
enum can have 2 string values only.

this is normally where I would use a switch:
myEnumType myEnumChoice;
if (Enum.TryParse(choice, out myEnumChoice))
{
switch(myEnumChoice)
{
case myEnumType.FirstEnum:
//doSomething();
break;
case myEnumType.SecondEnum:
//doSomethingElse();
break;
}
}
else
throw new ArgumentException(string.Format("Unexpected enum value: {0}", choice));

use
EnumChoice choice = (EnumChoice) Enum.Parse(typeof(ChumChoice), (string)Session["export_choice"] , true);
EnumChoice is enum type
you can use it like
if(choice== EnumChoice.X)
{
}
else
{
}

Related

How do you pass different collections through the same parameter?

I am creating a method that will take a collection of different types (divs, spans..) and search it. I can't find a parameter to pass different collection types though. I tried IElementContainer and ElementCollections but I can't cast DivCollection to either. Is there another way?
The method that does the search:
private static ElementCollection searchCollections(IElementContainer ec, WACore.compStringInfo info)
{
if (info.componentIDName == WACore.componentIDs.Id.ToString())
{
return ec.Elements.Filter(Find.ById(info.componentIDValue));
}
else if (info.componentIDName == WACore.componentIDs.Name.ToString())
{
return ec.Elements.Filter(Find.ByName(info.componentIDValue));
}
else if (info.componentIDName == WACore.componentIDs.Title.ToString())
{
return ec.Elements.Filter(Find.ByTitle(info.componentIDValue));
}
else if (info.componentIDName == WACore.componentIDs.OuterText.ToString())
{
String str = info.componentIDValue.Substring(1, 6);
return ec.Elements.Filter(Find.ByText(new Regex(str)));
}
else
{
return null;
}
}
The caller(s):
return searchCollections((IElementContainer)doc.TextFields, info);
return searchCollections((IElementContainer)(doc.Divs), info);
return searchCollections((IElementContainer)doc.Spans, info);

How to show intro text only in news website (asp.net mvc)

I have a function in my view:
#helper truncate(string input, int length)
{
if (input.Length<=length)
{
#input;
}
else
{
#input.Substring(0,length)#:...
}
}
If i write #truncate("some word",50) => it worked
But i write #truncate(item.Description, 50) => it error: Object reference not set to an instance of an object.
Please help me to fix my problem or show me another way to show intro text only in my site
Thanks!
if input is null, then you get error input.Length and input.Substring lines. You should check, if it is not null.
#helper truncate(string input, int length)
{
if(input == null)
{
//
}
else
{
if (input.Length <= length)
{
#input;
}
else
{
#input.Substring(0,length)#:...
}
}
}
EDIT (after comment)
you can use:
input = Regex.Replace(input , "<.*?>", string.Empty);
NOTE: Html.Raw returns markup that is not HTML encoded.

Convert controls dynamically from strings

I want to disable some controls on my asp page from a ControlCollection.
This is my code.
foreach (System.Web.UI.Control c in ControlCollection)
{
if (c.GetType().FullName.Equals("System.Web.UI.WebControls.Table"))
{
TableRow t = (TableRow)c;
t.Enabled = false;
}
else if (c.GetType().FullName.Equals("System.Web.UI.WebControls.TextBox"))
{
TextBox t = (TextBox)c;
t.Enabled = false;
}
.......
......
///Like this I do for all controls
}
I need a better approach at this. I searched on Internet but didn't find any solution.
You can use the .OfType<> extension like this in order to have more elegant code:
collection.OfType<Table>().ToList().ForEach(c => c.Enabled = false);
collection.OfType<TextBox>().ToList().ForEach(c => c.Enabled = false)
Do all controls in your list inherit from System.Web.UI.WebControl? If so, than this code may help. (Didn't test it myself)
Type wc = new System.Web.UI.WebControls.WebControl(HtmlTextWriterTag.A).GetType();
foreach (System.Web.UI.Control c in ControlCollection)
{
if (c.GetType().IsSubclassOf(wc))
{
((System.Web.UI.WebControls.WebControl)c).Enabled = false;
}
}
And even more elegant (thanx to Shadow Wizard )
ControlCollection.OfType<System.Web.UI.WebControls.WebControl>().ToList().ForEach(c => c.Enabled = false);
Try to use is.
if (c is Table)
{
}
else if (c is TextBox)
{
}
Or consider doing a switch statement on the type name.
switch (c.GetType().Name.ToLower())
{
case "table":
break;
case "textbox":
break;
}

QMapIterator declaration of several lists problem

i'm having 3 types of lists, and i'm trying to print them according to attribute_type.
so according attribute_type the iterator is chosen.
the problem is if for example attribute_type == "pis", the if condition is executed but when finished it returns to the upper decleration: QMapIterator it(pit.item_parameters), so this way the iterator is not valid...
how should i declare it properly ?
QMapIterator<QString, IP> it(pit.item_parameters);
if (attribute_type == "ips") {
QMapIterator<QString, IP> it(pit.item_parameters);
if (pit.item_parameters.isEmpty()) { return; }
}
else if (attribute_type == "variables") {
QMapIterator<QString, Variable> it(pit.variables);
if (pit.variables.isEmpty()) { return; }
}
else if (attribute_type == "pis") {
QMapIterator<QString, PI> it(pit.pis);
if (pit.pis.isEmpty()) { return; }
}
while(it.hasNext())
{
it.next();
text += it.key();
text += it.value().type;
}
If your types IP, PI and Variable derive from the same class ParentClass having type as public member, you can declare it that way:
QMapIterator<QString, ParentClass> it;

how can we set the property of Viewstate?

I am using a enum
public enum WatchUsageMode
{
Watch = 1,
EmailPreferences = 2
}
i want to set the property of that enum in my view state in such a way that whenever view state is null return Watch else EmailPreference.how can i get and set the property?
Create a property to encapsulate this
public WatchUsageMode WatchUsageModeValue
{
get
{
if(ViewState["WatchUsageModeValue"] != null &&
ViewState["WatchUsageModeValue"] is WatchUsageMode)
return (WatchUsageMode)ViewState["WatchUsageModeValue"];
else
return null;
}
set
{
ViewState["WatchUsageModeValue"] = value;
}
}
Setting:
ViewState["KeyString"] = WatchUsageMode.EmailPreferences
Getting From View State
WatchUsageMode get()
{
if(ViewState["KeyString"]!=null)
return (WatchUsageMode)ViewState["KeyString"];
return WatchUsageMode.Watch;
}

Resources