Solved

Dynamic Variable C# .Net 4 Windows Form

Posted on 2010-11-16
6
420 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

733 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