Click here to Skip to main content
16,012,116 members
Please Sign up or sign in to vote.
2.41/5 (3 votes)
See more:
Hi Team,

My Question:

How to create existing variable using two string in c# or vb.net?

Example:

My Existing variables are =>

string d1, d2, d3, d4, d5;

d1 = "test1";
d2 = "";
d3 = "test2";
d4 = "test3";
d5 = "";

for (int i = 0; i < 5; i++) 
 {
  if ((d+i) == string.Empty)   // it mean d1, d2, d3, d4, d5.
   {
    (d+i) = "00";
   }
 }



Please give me the solution for my issue.
Posted
Comments
Sergey Alexandrovich Kryukov 23-May-14 10:38am    
Not clear. What does it mean, "create a variable"?
—SA
[no name] 23-May-14 10:47am    
The "solution to your issue"? What issue? What you are trying to do does not make any sense. Use an array or List of strings.
ZurdoDev 23-May-14 11:29am    
As mentioned, use a structure that already supports what you want.

You can't easily do it like that - you can do it under some circumstances, but it gets very, very complicated and isn't something I'd recommend a novice trying (or even looking at how to do it).

Instead, why not use an array?
C#
string[] myStrings = new string[] {"test1", "", "test2", "test3", "");
for (int i = 0; i < myStrings.Length; i++)
   {
   if (myStrings[i] == strings.Empty) 
      {
      myStrings[i] = "00";
      }
   }

Only I'd do this:
C#
string[] myStrings = new string[] {"test1", "", "test2", "test3", "");
for (int i = 0; i < myStrings.Length; i++)
   {
   if (string.IsNullOrWhiteSpace(myStrings[i])) 
      {
      myStrings[i] = "00";
      }
   }
 
Share this answer
 
create a list or array then;
C#
List<string> list = new List<string>(){d1, d2, d3, d4, d5};
for(int i =0; i<list.count;i++)
 {
  if (string.IsNullOrEmpty(list[i]))  
   {
    list[i] = "00";
   }
 }
 
Share this answer
 
v3
Comments
Mohankumar.Engain 26-May-14 1:52am    
Thank you I got a solution from your code, Mr. DamithSL.
DamithSL 26-May-14 2:42am    
you are welcome!

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