tags:

views:

169

answers:

6

I have the following code...

Const ToAddress As String = username.Text & "@gmail.com"

which sets to ToAddress to be used in on my Net.Mail.MailMessage that is to be created with the following constructor

Dim mm As New Net.Mail.MailMessage(username.Text, ToAddress)

which takes in a string and a constant string. But I get an error here

Const ToAddress As String = **username.Text** & "@gmail.com"

that says : constant expression required

+2  A: 

how to use mailmessage

username.text is variable, you wont be able to create a const

you can always create a function that would do validation on the username.text

 public function ToAddress(byval _username as string) as MailAddress
      '
      'validation here for _username
      '
      return new MailAddress(_username & "@gmail.com")         
 end function
Fredou
If I change the ToAddress to a string the MailMessage constructor does not take it. I need to set the address dynamically. Is there any work around?
@Kwah, look at my first line for a link to how to use it
Fredou
A: 

That is not how a Const should be used. See the first line here. "a constant is a special kind of variable whose value cannot be altered during program execution"

Edit: See Fredou's answer for the correct syntax.

mxmissile
A: 

VB.NET and C# are not C. Constant modifier is just used for constants whose values are known at the time of compilation.

Mehrdad Afshari
A: 

Remember that a constant is a value that is compiled as a literal into the assembly by the compiler. This is why you cannot define constants as an expression because the compiler cannot evaluate the expression at compilation time.

When you define a constant the compiler takes the value of that constant and replaces all uses of the constant in your source with the literal value. In your example I don't see any reason why you would need to define your string as Const since the method receiving the string will have no way of determining if the string was a Const at compilation time.

Andrew Hare
Can anyone suggest a way to send users a mail on asp.net 2.0.
The phrase "...cannot define constants as an expression..." isn't quite true. You can define a constant as an expression IF all terms of that expression are literals or other constants.
RolandTumble
@RolandTumble: Excellent distinction!
Andrew Hare
A: 

Rather than using a Const, specifically, if you just want the "constant" behavior for the life of an instance of your class, try:

ReadOnly ToAddress As String = username.Text & "@gmail.com"
Dathan
Even though the MailMessage constructor does take ReadOnly instead of Const for the second argument two other issues arrise...1. ReadOnly cannot be used inside a Sub 2. username.txt is not visible outside...requires a sub of the ffg format Sub subname(ByVal sender As Object, ByVal e As System.EventArgs)
Dathan
A: 

Thank you for your assistance. I closed visual studio and restarted it and now it's sending mail works fine. Seems like VS has a bug...