Solved

Dynamic Variable C# .Net 4 Windows Form

Posted on 2010-11-16
6
418 Views
Last Modified: 2012-05-10
Hello Experts,

I need to set variables dynamically in a C# windows Form application.  I've done this before in VBScript using "Execute":

Execute("myVar" & i & "=thatVar" & j)

in PHP:

${"myVar".i} = ${"thatVar".j};

Now... how can I do that in C#?

Thanks in advance.
0
Comment
Question by:Ranidae
  • 3
  • 3
6 Comments
 
LVL 12

Expert Comment

by:jagssidurala
ID: 34151952
I think you are using this for loop.....the below code satisfies your needs

"myVar" + i = "thatVar" + j
0
 
LVL 2

Author Comment

by:Ranidae
ID: 34151993
That doesn't work in c#, or if it does, I can't figure it out.
The goal is:

Boolean bTest1 = true;
Boolean bTest2 = false;
Boolean bTest3 = true;

for(int i =1; i<=3 ;i++)
{
   if(bTest + i} { dosomething(); }
}

0
 
LVL 12

Expert Comment

by:jagssidurala
ID: 34152041
As per your above code you are executing same code if any of the boolean variable value to true, so what is need of 3 variables.you can use one boolean variable.

 if you execute different code for every variable you need write if condition for every variable.
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 2

Author Comment

by:Ranidae
ID: 34152075
The three shown are only for demo.

The true code is looking to do:

for(int i = 1; i <=15; i++)
{
     if(properties.settings.Default.extensionAttribute+i)
    {
         dostuff();
    }
}
0
 
LVL 12

Accepted Solution

by:
jagssidurala earned 250 total points
ID: 34152211
you can not get variable name by concatnating the static text and with some number(in your case).

for this you have to write if condition for every variable.

we can write loop for a collection and not for the same type(naming convention like btest1,btest2...) of individual variables.
0
 
LVL 2

Author Comment

by:Ranidae
ID: 34152223
k.. thanks... unfortunate that this is not available in C#.... almost makes me miss VBScript and PHP... almost...
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now