473,508 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 11577

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

94
13770
by: John Bailo | last post by:
The c# *return* statement has been bothering me the past few months. I don't like the fact that you can have different code paths in a method and have multiple return statements. To me, it...
14
2001
by: Gama Franco | last post by:
Hi, I'm designing an interface for a shared library, and I would like to know if there is a standard about how to return an object to the user. I will use exceptions to report errors, so there...
5
3029
by: Neal Coombes | last post by:
Posted to comp.lang.c++.moderated with little response. Hoping for better from the unmoderated groups: -------- Original Message -------- Subject: Return appropriately by value, (smart)...
4
6935
by: msolem | last post by:
I have some code where there are a set of functions that return pointers to each other. I'm having a bit of a hard time figuring out the correct type to use to do that. The code below works but...
18
4024
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
13
13608
by: markn | last post by:
Running some code through static analysis, I noticed that gcc will generate a warning if a function returns an aggregate, controlled with this flag (from the gcc manual): -Waggregate-return...
80
41019
by: xicloid | last post by:
I'm making a function that checks the input integer and returns the value if it is a prime number. If the integer is not a prime number, then the function should return nothing. Problem is, I...
33
10279
by: jayapal | last post by:
Hi all, Is there any way to return two values from a function .................... Thanks, jayapal
1
5833
by: anu29dolly | last post by:
error:return makes integer from pointer without a cast.. following is my code ..plz help to track the bug ......... #include <stdio.h> #include <stdlib.h> #include <sys/stat.h> #include...
0
7231
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7133
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7405
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7066
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5643
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4724
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3214
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3198
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.