Nullable strings as parameters

M

Mark Rae

Hi,

In v1.1 I used to simulate nullable datatypes with structs. e.g.

public struct NullableString
{
private readonly String pstrValue;
private readonly bool blnHasValue;
public NullableString(String strValue)
{
this.pstrValue = strValue;
this.blnHasValue = true;
}
public String Value {get{return this.pstrValue;}}
public bool HasValue {get{return blnHasValue;}}
};

However, now that we have these natively in v2, I'm trying to use them as
part of my database abstraction layer. I have a class which begins:

public class CSale
{
private String? pstrTransactionID;
public String? strTransactionID {get{return
pstrTransactionID;}set{pstrTransactionID = value;}}

private DateTime? pdtmPayment;
public DateTime? dtmPayment{get{return pdtmPayment;}set{pdtmPayment =
value;}}

private int? pintItems;
public int? intItems {get{return pintItems;}set{pintItems = value;}}

private decimal? pcurFee;
public decimal? curFee{get{return pcurFee;}set{pcurFee = value;}}

// etc
}

However, this doesn't compile. Instead, the compiler generates the following
error:
The type 'string' must be a non-nullable value type in order to use it as
parameter 'T' in the generic type or method 'System.Nullable<T>'

IntelliSense still works, though, i.e. if I type a period after
strTransactionID, it drops down the list of properties and methods e.g.
HasValue, Value etc.

If I comment out the two lines relating to the String parameter, the class
compiles OK.

Any assistance gratefully received.

Mark
 
P

Paul Henderson

However, this doesn't compile. Instead, the compiler generates the following
error:
The type 'string' must be a non-nullable value type in order to use it as
parameter 'T' in the generic type or method 'System.Nullable<T>'

Only value types (i.e. structs) can be made nullable (as these cannot
otherwise be set to 'null'); it is rather meaningless to use string? as
string is a reference type anyway, so you can just set a string object
to null...so, just don't make your string members nullable explicitly,
and you'll still be able to assign them to null as you require.
 
M

Mark Rae

Only value types (i.e. structs) can be made nullable (as these cannot
otherwise be set to 'null'); it is rather meaningless to use string? as
string is a reference type anyway, so you can just set a string object
to null...so, just don't make your string members nullable explicitly,
and you'll still be able to assign them to null as you require.

I see.

For the other various datatypes, I use variations of the following code to
build up a SqlParameters collection:

mobjSqlParameter = new SqlParameter("@pdtmSale", SqlDbType.SmallDateTime);
mobjSqlParameter.IsNullable = true;
mobjSqlParameter.Value = dtmSale.HasValue ? dtmSale.Value :
(object)DBNull.Value;
maobjSqlParameters.Add(mobjSqlParameter);

Am I able to do something similar with strings?
 
P

Paul Henderson

For the other various datatypes, I use variations of the following code to
build up a SqlParameters collection:

mobjSqlParameter = new SqlParameter("@pdtmSale", SqlDbType.SmallDateTime);
mobjSqlParameter.IsNullable = true;
mobjSqlParameter.Value = dtmSale.HasValue ? dtmSale.Value :
(object)DBNull.Value;
maobjSqlParameters.Add(mobjSqlParameter);

Am I able to do something similar with strings?

Yes; for the third line you should just be able to use:
mobjSqlParameter.Value = myStr == null ? (object)DBNull.Value :
myStr;
 
M

Mark Rae

Yes; for the third line you should just be able to use:
mobjSqlParameter.Value = myStr == null ? (object)DBNull.Value :
myStr;

Cool! Thanks a lot for your assistance.
 

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

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top