Home All Groups Group Topic Archive Search About

TypeConverter and arrays

Author
15 Nov 2006 8:40 PM
Rog
Using asp.net1.1, I have a property on a control that is an enum.  I use an
accessor and set it in HTML view.  So:

public enum Permission
{
None = 0,
Admin = 1,
Member = 2, ...
}
....
public class Lock : System.Web.UI.Control
{
private Permission perm;

[Bindable(true),
Category("Security"),
DefaultValue("1")]
public Permission RequiredPermission
{
  get
  {
    return perm;
  }
  set
  {
    perm = value;
  }
}

Then in html view:
<cc1:Lock ... RequiredPermission="Admin" ... >

Now, I want to make the Permission property an array of permissions:

private Permission[] perm;

My question is, how can I implement this to allow me to still specify the
list of permissions in html view, like:
<cc1:Lock ... RequiredPermission="Admin,Member" ... >

I need to convert this list into an array, is there any way to do this
within an accessor?  If I try something like this, I get a parser error,
saying it can't create an object of type Permission[] from its string
representation.

public Camelot.Permission[] RequiredPermission
{
  get
  {
    return perm;
  }
  set
  {
    // break up comma delimited string in value, create an array of
permissions, assign it to perm
  }
}

Thanks!

Author
16 Nov 2006 10:03 AM
Vadym Stetsyak
Hello, Rog!

You can add another property that accepts coma delimited strings, and in that property
create Permission[] array.

smth like
"<cc1::Lock ... RequiredPermissionArray="Admin,Member" ... >

public string RequiredPermissionArray
{
       set { /*parse string here and create appropriate Permission array*/ }
}



R> Using asp.net1.1, I have a property on a control that is an enum.  I
R> use an
R> accessor and set it in HTML view.  So:

R> public enum Permission
R> {
R> None = 0,
R> Admin = 1,
R> Member = 2, ...
R> }
R> ...
R> public class Lock : System.Web.UI.Control
R> {
R> private Permission perm;

R> [Bindable(true),
R> Category("Security"),
R> DefaultValue("1")]
R> public Permission RequiredPermission
R> {
R>   get
R>   {
R>     return perm;
R>   }
R>   set
R>   {
R>     perm = value;
R>   }
R> }

R> Then in html view:
<cc1::Lock ... RequiredPermission="Admin" ... >

R> Now, I want to make the Permission property an array of permissions:

R> private Permission[] perm;

R> My question is, how can I implement this to allow me to still specify
R> the
R> list of permissions in html view, like:
<cc1::Lock ... RequiredPermission="Admin,Member" ... >

R> I need to convert this list into an array, is there any way to do
R> this
R> within an accessor?  If I try something like this, I get a parser
R> error,
R> saying it can't create an object of type Permission[] from its string
R> representation.

R> public Camelot.Permission[] RequiredPermission
R> {
R>   get
R>   {
R>     return perm;
R>   }
R>   set
R>   {
R>     // break up comma delimited string in value, create an array of
R> permissions, assign it to perm
R>   }
R> }

R> Thanks!

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Author
16 Nov 2006 4:49 PM
Dale
You would have to use a separate setter method to turn your string into a
Permission[].

I almost sent code to do it in an implicit cast but then I remembered you
can't create an implicit cast from an object to an array of another object -
only to an instance of the other object.

Alternatively, you may make your Permission[] property into an Object[]
property instead.  That would require casting each time you access it but
then you could pass the string as the value, convert it to an array of
Permissions, and set your private Permission[] variable to hold that array.

HTH

Dale

--
Dale Preston
MCAD C#
MCSE, MCDBA


Show quote
"Rog" wrote:

> Using asp.net1.1, I have a property on a control that is an enum.  I use an
> accessor and set it in HTML view.  So:
>
> public enum Permission
> {
> None = 0,
> Admin = 1,
> Member = 2, ...
> }
> ...
> public class Lock : System.Web.UI.Control
> {
> private Permission perm;
>
> [Bindable(true),
> Category("Security"),
> DefaultValue("1")]
> public Permission RequiredPermission
> {
>   get
>   {
>     return perm;
>   }
>   set
>   {
>     perm = value;
>   }
> }
>
> Then in html view:
> <cc1:Lock ... RequiredPermission="Admin" ... >
>
> Now, I want to make the Permission property an array of permissions:
>
> private Permission[] perm;
>
> My question is, how can I implement this to allow me to still specify the
> list of permissions in html view, like:
> <cc1:Lock ... RequiredPermission="Admin,Member" ... >
>
> I need to convert this list into an array, is there any way to do this
> within an accessor?  If I try something like this, I get a parser error,
> saying it can't create an object of type Permission[] from its string
> representation.
>
> public Camelot.Permission[] RequiredPermission
> {
>   get
>   {
>     return perm;
>   }
>   set
>   {
>     // break up comma delimited string in value, create an array of
> permissions, assign it to perm
>   }
> }
>
> Thanks!

AddThis Social Bookmark Button