Click here to Skip to main content
15,899,754 members
Home / Discussions / C#
   

C#

 
QuestionHow to check object==null for objects without Constructor? Pin
artisticcheese4-Nov-05 6:05
artisticcheese4-Nov-05 6:05 
AnswerRe: How to check object==null for objects without Constructor? Pin
whizzs4-Nov-05 7:27
whizzs4-Nov-05 7:27 
artisticcheese wrote:
Hello,

I need to do following. If object was not initialized then assign variable to it like code below.
Right now it throws an error becouse MyCert is not initialized when compared to null. How do I do what I need to do?



X509ChainElement MyCert;
foreach (X509ChainElement cert in chain.ChainElements)
{
if ( MyCert == null)
{
MyCert = (X509ChainElement) cert;
continue;
}
else
{
.....;
}
}


The quickest way is to initialize your object at the beginning. I am assuming you will be doing some processing between your declaration and your foreach loop, otherwise it makes no sense to check for a null because you know it is uninitialized. So it would look like this:

X509ChainElement MyCert = null;

// do some processing and other things.....

foreach (X509ChainElement cert in chain.ChainElements)
{
if ( MyCert == null) //has not been initialized somewhere before the loop
{
MyCert = (X509ChainElement) cert;
continue;
}
else
{
.....;
}
}

But what I don't understand about your code is that only the first element will ever be assigned to MyCert. Of course I only get to see a small snippet and this may be the functionality you want.
QuestionLoading Files Pin
zaboboa4-Nov-05 5:53
zaboboa4-Nov-05 5:53 
AnswerRe: Loading Files Pin
User 66584-Nov-05 7:32
User 66584-Nov-05 7:32 
GeneralRe: Loading Files Pin
zaboboa4-Nov-05 8:13
zaboboa4-Nov-05 8:13 
AnswerRe: Loading Files Pin
whizzs4-Nov-05 7:37
whizzs4-Nov-05 7:37 
AnswerRe: Loading Files Pin
Jon Rista4-Nov-05 16:16
Jon Rista4-Nov-05 16:16 
Questionget GC to collect unused memory Pin
Stefan Fohringer4-Nov-05 5:06
Stefan Fohringer4-Nov-05 5:06 
AnswerRe: get GC to collect unused memory Pin
Dan Neely4-Nov-05 5:32
Dan Neely4-Nov-05 5:32 
AnswerRe: get GC to collect unused memory Pin
Dave Kreskowiak4-Nov-05 5:52
mveDave Kreskowiak4-Nov-05 5:52 
GeneralRe: get GC to collect unused memory Pin
Jon Rista4-Nov-05 16:33
Jon Rista4-Nov-05 16:33 
GeneralRe: get GC to collect unused memory Pin
Dave Kreskowiak5-Nov-05 2:49
mveDave Kreskowiak5-Nov-05 2:49 
GeneralRe: get GC to collect unused memory Pin
Jon Rista5-Nov-05 9:57
Jon Rista5-Nov-05 9:57 
QuestionMost elegant way to query 400 computers in C#? Pin
artisticcheese4-Nov-05 4:44
artisticcheese4-Nov-05 4:44 
AnswerRe: Most elegant way to query 400 computers in C#? Pin
Rob Graham4-Nov-05 5:14
Rob Graham4-Nov-05 5:14 
GeneralRe: Most elegant way to query 400 computers in C#? Pin
artisticcheese4-Nov-05 5:17
artisticcheese4-Nov-05 5:17 
AnswerRe: Most elegant way to query 400 computers in C#? Pin
S. Senthil Kumar4-Nov-05 21:39
S. Senthil Kumar4-Nov-05 21:39 
GeneralRe: Most elegant way to query 400 computers in C#? Pin
artisticcheese5-Nov-05 2:31
artisticcheese5-Nov-05 2:31 
GeneralRe: Most elegant way to query 400 computers in C#? Pin
S. Senthil Kumar5-Nov-05 3:17
S. Senthil Kumar5-Nov-05 3:17 
QuestionRemoting - recovering communication Pin
duff794-Nov-05 4:37
duff794-Nov-05 4:37 
QuestionAny good C# 2.0 books out yet? Pin
Ed K4-Nov-05 3:46
Ed K4-Nov-05 3:46 
QuestionUnTrapping of Mouse Events Pin
wheelerbarry4-Nov-05 3:38
wheelerbarry4-Nov-05 3:38 
AnswerRe: UnTrapping of Mouse Events Pin
Dave Kreskowiak4-Nov-05 5:47
mveDave Kreskowiak4-Nov-05 5:47 
QuestionRichtextbox Pin
PaulaM4-Nov-05 2:38
PaulaM4-Nov-05 2:38 
AnswerRe: Richtextbox Pin
mav.northwind4-Nov-05 2:49
mav.northwind4-Nov-05 2:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.