Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear All,

I need to pass a string value to const string value.I will attach the code below.While I pass the string variable to const string "constant expression required " error getting.

VB
Dim s As String = "sss"
Const ZSTransID As String = s

Please help me to solve

Regards
Sreejith
Posted
Updated 15-Nov-14 10:12am
v2

1 solution

I am afraid that first you need to understand what Constant is in .Net. Read the documentation is MSDN here:

http://msdn.microsoft.com/en-us/library/zb8fth98.aspx[^]

In you above example, you need to understand that an assignment to a constant can only be a compile time constant. This is what you could still do (it's in C#), but basically means that you have to declare s as constant as well.

I am not sure if in practice that would help you.

C#
const string s = "sss";
const string ZSTransID = s;


Btw, you might want to look at the ReadOnly [^]keyword as well and compare it with Const.

Const and Readonly Keywords[^]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 15-Nov-14 20:35pm    
5ed.
—SA
Manas Bhardwaj 16-Nov-14 4:33am    
thx SA!
SreejithAchutan 16-Nov-14 0:41am    
Thanks for your reply.But actaully I have a value in string format(Random Number).I need to pass this string value to a const string.

I would like to know about that How to convert the string variable to const string.
Manas Bhardwaj 16-Nov-14 4:10am    
If you read the my answer, it states that it wont be possible. And I am wondering why would you pass a variable string to constant?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900