Pass empty string

S

shapper

Hello,

I have a method:
Roles(CultureInfo culture, RoleType? type, bool? open, bool?
beginWithEmpty, string[] userRoles)

How can I pass an empty string as userRoles? I tried:
Roles(Thread.CurrentThread.CurrentCulture, RoleType.Administrator,
false, true, new string[])

and

Roles(Thread.CurrentThread.CurrentCulture, RoleType.Administrator,
false, true, new string[0])

and

Roles(Thread.CurrentThread.CurrentCulture, RoleType.Administrator,
false, true, null)

But nothing works.

Thanks,
Miguel
 
F

Family Tree Mike

It depends on how you are checking parameters in Roles, but how about:

Roles(Thread.CurrentThread.CurrentCulture, RoleType.Administrator,
false, true, new string[] {string.Empty})
 
S

shapper

It depends on how you are checking parameters in Roles, but how about:

Roles(Thread.CurrentThread.CurrentCulture, RoleType.Administrator,
  false, true, new string[] {string.Empty})


I have a method:
Roles(CultureInfo culture, RoleType? type, bool? open, bool?
beginWithEmpty, string[] userRoles)
How can I pass an empty string as userRoles? I tried:
Roles(Thread.CurrentThread.CurrentCulture, RoleType.Administrator,
false, true, new string[])

Roles(Thread.CurrentThread.CurrentCulture, RoleType.Administrator,
false, true, new string[0])

Roles(Thread.CurrentThread.CurrentCulture, RoleType.Administrator,
false, true, null)
But nothing works.
Thanks,
Miguel

In roles I am creating a list of Role, RoleList, and using this array
of string as follows:
RoleList.Where(r => (userRoles == null ||
userRoles.Contains(r.Type.ToString()))

So if userRoles is empty, nothing was passed, all records are used.
If not then from RoleList get only the items which r.Type is contained
in RoleList.

But now I am not sure if I can use:
userRoles == null

Because I think I can't pass the string array as null.
 
S

shapper

In roles I am creating a list of Role, RoleList, and using this array
of string as follows:
RoleList.Where(r => (userRoles == null ||
userRoles.Contains(r.Type.ToString()))
So if userRoles is empty, nothing was passed, all records are used.
If not then from RoleList get only the items which r.Type is contained
in RoleList.
But now I am not sure if I can use:
userRoles == null
Because I think I can't pass the string array as null.

A concise-but-complete code sample would be helpful.  Passing "null" or 
"new string[0]" should work fine, I think, at least given what you've  
showed us so far.  So if it doesn't work, there's probably something else  
going on.

But you need to show everything for us to figure out what that "something 
else" is.

Pete


Sorry, for the delay.
Basically, I have a class Role with Type, Description, Open, ...
properties that holds information on each of my application role.

Inside this class I have a method named List that returns the list of
roles to populate and input, checkboxes, etc ... So the format might
differ.
And sometimes I might need to filter the list ...

On my MVC view I want to call the method list but not directly ... I
prefer to call the a method on the View's ViewData object. So I have:

public static IList<Role> Roles(CultureInfo culture, RoleType?
type, bool? open, bool? beginWithEmpty, string[] userRoles) {
return Role.List(culture, type, open, beginWithEmpty,
userRoles);
}

public static IList<Role> List(CultureInfo culture, RoleType?
type, bool? open, bool? beginWithEmpty, string[] userRoles) {
IList<Role> roles = new List<Role>();
if (beginWithEmpty ?? false) roles.Add(new Role { Type = null,
Description = "---", Open = true });
switch (culture.TwoLetterISOLanguageName.ToLower()) {
case "pt":
roles.Add(new Role { Type = RoleType.Administrator,
Description = "Administrador", Open = false });
roles.Add(new Role { Type = RoleType.Collaborator,
Description = "Colaborador", Open = false });
// ...
break;
}
return roles.Where(r => (userRoles == null ||
userRoles.Contains(r.Type.ToString())) && (open == null || r.Open ==
open) && (type == null || r.Type == type)).OrderBy(r =>
r.Description).ToList();
} // List

And in my view I have:
<%foreach (Role role in
ViewData.Get<AccountBook>("Account").Roles(Thread.CurrentThread.CurrentCulture,
RoleType.Administrator, false, true, null)) {%>

And I am getting the error:
Member
'MyApp.Models.AccountBook.Roles(System.Globalization.CultureInfo,
MyApp.Security.Membership.RoleType?, bool?, bool?, string[])' cannot
be accessed with an instance reference; qualify it with a type name
instead

What am I doing wrong?

Thank You,
Miguel
 
S

shapper

[...]
And I am getting the error:
Member
'MyApp.Models.AccountBook.Roles(System.Globalization.CultureInfo,
MyApp.Security.Membership.RoleType?, bool?, bool?, string[])' cannot
be accessed with an instance reference; qualify it with a type name
instead
What am I doing wrong?

Well, the first mistake was to post a question that has nothing to do with  
your problem.  :p

The second mistake was to not post the error message you're getting.  
Especially since that error message clearly describes the problem: the  
Roles() method is a static method, but you are calling it as if it were an  
instance method.

The loop in which you call the Roles() method seems pointless to me, since  
you never use the "role" loop variable.  You just call the method in an 
identical way for each "role" instance you get.  But if you really  
intended to implement things that way, you need to just do what the error 
message says: use a type name instead of an instance reference for calling  
the Roles() method.

Pete

Thanks! I copied pasted my method I forgot to remove the static ...
then I was completely convinced that the problem was in the string
part because I had problems in it before ...

I didn't post the code inside the loop because I knew that wasn't the
problem ... about this one I was sure :p ... lol

Thanks,
Miguel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top