473,404 Members | 2,137 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,404 software developers and data experts.

Warning and how to optimize code

Hello all,

I have 5 warnings only but I want to know how to "optimize" my code so that
it is "clean."

The warning is this with all 5 the same
Warning 1 Variable 'strxxx' is used before it has been assigned a value. A
null reference exception could result at runtime.

Here is the code

Dim strxxxAs String

.....

.....

If ckxxx.Checked = True Then

strxxx = ckxxx.Text

End If

....

.....

objMail.Body = ....

strxxx& vbCrLf + _

The warnings are in the objMail.Body part of the code.

TIA

Aaron
Jan 19 '06 #1
2 1395
Aaron wrote:
Hello all,

I have 5 warnings only but I want to know how to "optimize" my code so that
it is "clean."

The warning is this with all 5 the same
Warning 1 Variable 'strxxx' is used before it has been assigned a value. A
null reference exception could result at runtime.

Here is the code

Dim strxxxAs String

....

....

If ckxxx.Checked = True Then

strxxx = ckxxx.Text

End If

...

....

objMail.Body = ....

strxxx& vbCrLf + _

The warnings are in the objMail.Body part of the code.

Well, what if ckxxx.Checked is false? strxxx is equal to Nothing at
this point, and that's what the compiler is warning you about. What
should the string be if ckxxx is false? An empty string? Then assign it
as such. You can do this in your Dim statement, as:

Dim strxxx as String = ""

or

Dim strxxx as String = String.Empty

or you could assign it later:

If ckxxx.Checked Then 'Note - why compare it to True?
strxxx = ckxxx.Text
Else
strxxx = ""
End If

So, in summary, there's plenty of ways to do this. Are you
concatenating lots of options together in your objMail.Body line? If
so, you might want to add the vbCrLf during the assignment to strxxx,
rather than during concatenation. Otherwise (say, for instance, there
are 6 options, and options 2 and 5 are selected), your body will be:

-----(Start of Body)

Option 2
Option 5

-----(End of Body)

Whereas if you only add the CR/LF to the string if it contains
something, you'll get:

-----(Start of Body)
Option 2
Option 5
-----(End of Body)

Hope this has helped,

Damien

Jan 19 '06 #2
Joe
Dear Aaron
It should change the following code to
Dim strxxxAs String
to
Dim strxxxAs String = ""

Since you need to pre-define the variable once you declare it

Hope this help you

Regards,
Joe Tsui


"Aaron" wrote:
Hello all,

I have 5 warnings only but I want to know how to "optimize" my code so that
it is "clean."

The warning is this with all 5 the same
Warning 1 Variable 'strxxx' is used before it has been assigned a value. A
null reference exception could result at runtime.

Here is the code

Dim strxxxAs String

.....

.....

If ckxxx.Checked = True Then

strxxx = ckxxx.Text

End If

....

.....

objMail.Body = ....

strxxx& vbCrLf + _

The warnings are in the objMail.Body part of the code.

TIA

Aaron

Jan 19 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Andreas Falck | last post by:
Hi, I ran the code below on two different versions, 4.0.14 and 4.0.16 respectively, both running RH 7.3 on intel. In version mysql server version 4.0.14 the SELECT privelege suffices for...
109
by: Andrew Thompson | last post by:
It seems most people get there JS off web sites, which is entirely logical. But it is also a great pity since most of that code is of such poor quality. I was looking through the JS FAQ for any...
5
by: Dylan | last post by:
Just wondered what you guys do when you need a method that returns a const reference to some data based upon some value. something like this: const CData& MyClass::GetDataWithValue(int...
4
by: Pushkar Pradhan | last post by:
I have some functions which take as i/p a buffer (it can be float, char, or 16 bit, int etc.). The result is another o/p buffer, its type is also flexible (it could be a float, char etc.). I try...
16
by: David Ford | last post by:
I have a macro that I use across the board for freeing ram. I'd like to clean up my code so I don't get these warnings. #define sfree(x) _internal_sfree((void **)&x) #define _internal_sfree(x)...
12
by: Daniel Sjöblom | last post by:
When I compile this (with GCC and optimizations on): void foo(void) { int a; float f = *((float *) &a); } I get the warning : "dereferencing type-punned pointer will break strict-aliasing...
0
by: Daniel | last post by:
Hi there, I recently came across an interesting option when right clicking on a project (Right click on the project -> properties -> Configuration Properties ->Build -> Optimize) There is an...
5
by: Peter Ritchie [C# MVP] | last post by:
I've purposely been ignoring a CA2122 warning in some C++ interop code I've been working on for quite some time. I've just recently had the cycles to investigate the warning. The warning message...
0
by: situ | last post by:
hi, i'm trying to return a result set from a procedure using i series navigator. how ever i am getting the results back but with warning as below. Message: 1 result sets are available from...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.