|
to elaborate on Mycroft's reply. This is often used when talking to the database. Suppose you have a column that contains a number, but the column can be null. In that case you would need to check if the value was DBNUll.Value and if so convert your variable (that maps to that column) to -1 or 0 or something. When using the ? construct you don't need to convert it and can directly map the variable to the database column. Some goes if you want to write a value to that column.
The ? is an 'override' so you can put null values in non-nullable variables (int, double, DateTime, ... but not strings, they can be null)
Hope this helps.
[EDIT]PS: don't be afraid to use it, but don't use it if you don't have to.[\EDIT]
V.
|
|
|
|
|
The ? after the variable type renders a variable that is nullable. What value that may have I haven't discovered, as I like my variables to have real values before I use them, but apparently someone has decided that a null integer is a good thing, and thoughtfully provided a way to declare such a beast. The ? operator can be used with any type, for what it's worth.
Will Rogers never met me.
|
|
|
|
|
Roger Wright wrote: The ? operator can be used with any type, for what it's worth.
No. It can only be used with value types (int, double, decimal, DateTime). It cannot be used with reference types. And it cannot be used with strings.
Don't forget to rate answer, that helped you. It will allow other people find their answers faster.
|
|
|
|
|
Part of the reason it can't be used with strings is that strings are nullable by default. They don't need special treatment.
|
|
|
|
|
I just checked, cause I wasn't sure... string is just a reference type, that acts like value type. So it can point to a null reference of course. My mistake
Don't forget to rate answer, that helped you. It will allow other people find their answers faster.
|
|
|
|
|
I didn't know about the reference type limitation, but then, I can't think of any value for the concept anyway and have never needed it. Strings are nullable by nature, so they don't count. Thanks for the info!
Will Rogers never met me.
|
|
|
|
|
? can be useful to determine if a choice has been made. To give an example, a person has 0 children because
1. they truely don't have any children or because
2. The value was never set?
Null is a way of explicitly determining if the person has no children. The old way was to set the default to -1. I think null is a better way of expressing that the value was not set.
"You get that on the big jobs."
|
|
|
|
|
To expand on the last reply, it is useful when a variable can validly take the whole range of the type in question, and can also have a 'not set' value. Typically we have either used an invalid value (e.g. testing getchar() for -1 in old school C; -1 is a common 'not set' value in ints still) or created another boolean variable to define whether or not we set a value:
bool customerLikedProduct;
bool customerResponded;
People have also solved that one with a three way enum ...
enum NullableBool { True, False, Maybe }
... but when you do that you lose the ability to use boolean logical operators, compatibility with external data sources or applications, etc.
In these cases, you would like to be able to represent the 'not set' value within one variable. That's what nullable types give you:
bool? customerLikedProduct;
As already mentioned, it's particularly useful for working with databases where null values are a common and standard thing.
For me, using -1 or double.NaN is fine, as long as that can never be a valid value for the variable in question. But there are times when the whole valid range for the natural data type is valid and you should use nullable types.
|
|
|
|
|
Please help me to do this.
|
|
|
|
|
Using VS 2010, right click on your project, click Add New Item, select the General menu item, and select Application Configuration File. I believe that VS 2008 works in a similar fashion.
Will Rogers never met me.
|
|
|
|
|
|
OK. I stumbled on your post and all I saw was a URL pointing to one question that was answered. Do you have some questions regarding that?
Good judgment comes from experience, and experience comes from bad judgment. Barry LePatner
|
|
|
|
|
A spammer, check other messages and click his personal flag.
The best things in life are not things.
|
|
|
|
|
bye bye advert!
// ♫ 99 little bugs in the code,
// 99 bugs in the code
// We fix a bug, compile it again
// 101 little bugs in the code ♫
|
|
|
|
|
|
I want to deny access to a datatable while an outside source is delivering information.
I check on changes to the datatable with a timed delayed method while I am requesting data, when I find that changes to the dt have stopped, then I wish to continue. I do not want to be able to read the dt while incoming info has been processed. When processing has stopped then I checked for changes again, until no changes then I can move on. If I am able to read it, the program may think nothing has changed while info is processed.
Can anyone point me in the right direction or tell me how to turn off access to the dt,
Then I can check it for access at timed intervals.
Thanks in advance.
Michael
|
|
|
|
|
I know that MySQL has commands for locking tables and even rows. Perhaps your database has that as well.
What database are you using?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
MAW30 wrote: I do not want to be able to read the dt while incoming info has been processed.
Why don't you put the data into an alternative table (or tables) then process it in place and then once you are done, post it all at once to the final tables?
That precludes error conditions that end up looking a dependent resource for long periods of time.
|
|
|
|
|
Hello everyone,
I'm working on a small hobby project. I'm making a web crawler for a particular website to extract some useful information from it. I've written information extraction algorithms but I'm completely new to HTTP response stuff. I'm using a HttpWebResponse class of .Net to get the source code of a webpage in form of StreamReader. Now, I'm wondering if I should process each line of stream or I should convert whole streamReader to a string and then process that. Let say, I'm looking for string that should be able to extract .mp3 links on webpage using regex then is it possible that a single link is on two differnt lines of StreamReader, look at the code below..
string url = "http://songs.pk";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream());
string line;
while ((line = sr.ReadLine()) != null)
{
Console.Read();
Console.WriteLine(line);
}
Now can I add some regex matching code here..will this code make sure that it will be able to extract all the .mp3 links from this website? Or should I convert the StreamReader to string and then use regex matching on that string?
I'm sorry, if I'm misunderstanding about StreamReader here, but I need some suggestions to parse the source code of a website. I've searched articles and Google but I'm unable to find something that could help me. Any articles, links or suggestions would be appreciated.
Thanks
Shivam Kalra
|
|
|
|
|
Your code is fine. You will read the HTML code line-by-line from the page and you can process the whole html page line by line. This is a pretty common scenario. You can use a regex to determine if the value from the line-field contains what you are searching for .
|
|
|
|
|
I would read the whole stream (to get the full page content) and use your regexes on that. The streaming is very useful for large downloads, but a web page is easily small enough to work on in memory. StreamReader.ReadToEnd is the easiest way to do that.
|
|
|
|
|
You could also use the HTMLDocument, IHTMLElementCollection, and IHTMLElement to read each tag found in the response and grab the innerHTML if it's a tag you're interested in.
|
|
|
|
|
Is it possible to call a Web Service from C# without first adding a reference to the WS in your project? All the examples I see require a reference. An example would be great.
Thanks
Everything makes sense in someone's mind
|
|
|
|
|
|
|
Hello,
Sorry, if its the wrong place to post. I have been doing Embedded Systems (lower level) development on Linux/Windows, in Generic C++ and Visual C++ for the last 5 years or so. I am trying to switch my focus to the new stuff as far as Web applications and latest stuff out there. I would like to do Web Services (middle and Back end not so much web site development). I have started learning C# (winforms) which isn't much different from C++, I guess I can pick up WPF as well. Currently, I am working in robotics and automations which is database driven development, may be its one step closer to what I need.
Can you please give me good tips of how you might approach this? and what topics to really focus on so I might need(may be ASP.net, WCF, DataBases...etc) that can make me a lot more marketable for employers?
Thanks
|
|
|
|