Solved

Dynamic Variable C# .Net 4 Windows Form

Posted on 2010-11-16
6
416 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 2

Author Comment

by:Ranidae
Comment Utility
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
Comment Utility
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
Comment Utility
k.. thanks... unfortunate that this is not available in C#.... almost makes me miss VBScript and PHP... almost...
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 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

11 Experts available now in Live!

Get 1:1 Help Now