471,319 Members | 1,931 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

how can i return null when returning a struct

I have the following function that returns a struct

public struct layout
{
public string str;
public int i;
}

function in some class where i loop through an arraylist of structs

public layout getLayout(string s)
{
foreach ( layout l in layoutArray)
if ( l.str == s ) return l;
}

but this does not compile because when i can't find l.str == s
what should i return then?
Jan 18 '06 #1
3 10409

"Digital Fart" <pr*********@angelfire.com> wrote in message
news:0t********************************@4ax.com...
I have the following function that returns a struct

public struct layout
{
public string str;
public int i;
}

function in some class where i loop through an arraylist of structs

public layout getLayout(string s)
{
foreach ( layout l in layoutArray)
if ( l.str == s ) return l;
}

but this does not compile because when i can't find l.str == s
what should i return then?


1) return a default layout
or
2) throw an exception
or
3) make it a class and return null
Jan 18 '06 #2
In 2.0 you could return "layout?" (i.e. Nullable<layout>), but I'm not sure
its a good anwser.

Could throw an exception?

Could refactor as:

public bool TryGetLayout(string s, out layout item) {
foreach(layout l in layoutArray) {
if(l.str == s) {
item= l;
return true;
}
}
item = default(layout); // or new layout();
return false;
}

Perhaps

Marc
Jan 18 '06 #3
If you're using C# 2.0 you could use nullable types and return a 'layout?'
type (lookup nullable types and System.Nullable in the documentation)

If not, you can't have such a thing as a null struct. You would have to do
something like return an object type instead and have the caller unbox, have
a special return struct value meaning "null" or return as a ref param and
have an error code as the return of the function.

"Digital Fart" <pr*********@angelfire.com> wrote in message
news:0t********************************@4ax.com...
I have the following function that returns a struct

public struct layout
{
public string str;
public int i;
}

function in some class where i loop through an arraylist of structs

public layout getLayout(string s)
{
foreach ( layout l in layoutArray)
if ( l.str == s ) return l;
}

but this does not compile because when i can't find l.str == s
what should i return then?

Jan 18 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

94 posts views Thread by John Bailo | last post: by
14 posts views Thread by Gama Franco | last post: by
4 posts views Thread by msolem | last post: by
18 posts views Thread by Pedro Pinto | last post: by
13 posts views Thread by markn | last post: by
80 posts views Thread by xicloid | last post: by
33 posts views Thread by jayapal | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.