473,395 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 11541

"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
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
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
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
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
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
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
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
by: jayapal | last post by:
Hi all, Is there any way to return two values from a function .................... Thanks, jayapal
1
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...
0
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
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,...

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.