Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 06-21-2004, 06:10 AM   #1 (permalink)
sammy
Code Monkey
 
sammy's Avatar
 
Join Date: Jun 2004
Location: Brooklyn/Rochester
Posts: 53
sammy is on a distinguished road
Send a message via AIM to sammy
Dynamic variables in C#?

Ok, the only way I can be really clear about this question is to put up a PHP example.

lets say i want to call the function func() on a whole bunch of variables, and they all are named in the following fashion: var1, var2, var3... etc

at compile time, i dont know how many of those variables i have. In php, its something like this:

for( $i = 0; $i < $theNumberOfVariables; $i++ ){
func( ${'var' . $i} );
}

is this possible in C#?
__________________
sammy is offline   Reply With Quote
Old 06-21-2004, 06:17 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,695
redhead is on a distinguished road
No, use an array, I can't understand why people would create dynamic variable names.. They are so hard to convert into another programming language..
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 06-21-2004, 06:22 AM   #3 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,470
sde is on a distinguished road
i don't know, but why wouldn't you just make an array?
__________________
Mike
sde is offline   Reply With Quote
Old 06-21-2004, 06:32 AM   #4 (permalink)
sammy
Code Monkey
 
sammy's Avatar
 
Join Date: Jun 2004
Location: Brooklyn/Rochester
Posts: 53
sammy is on a distinguished road
Send a message via AIM to sammy
Doesnt the size of an array have to be determined at compile time as well? Or is my morning coffee playing tricks with me again and im thinkin of another language?
__________________
sammy is offline   Reply With Quote
Old 06-21-2004, 06:43 AM   #5 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,470
sde is on a distinguished road
check out the ArrayList class.

Code:
using System.Collections;
...

ArrayList al = new ArrayList;

for(int i=0;i<20;i++){
  string s = "whatever";
  al.Add(s);
}
i'm not sure exactly what you're trying to do, but ArrayList is a way to make re-sizable arrays.
__________________
Mike
sde is offline   Reply With Quote
Old 06-21-2004, 07:05 AM   #6 (permalink)
sammy
Code Monkey
 
sammy's Avatar
 
Join Date: Jun 2004
Location: Brooklyn/Rochester
Posts: 53
sammy is on a distinguished road
Send a message via AIM to sammy
cool, i'll use arrayList then, since it is resizeable. Thanks a bunch
__________________
sammy is offline   Reply With Quote
Old 06-21-2004, 07:11 AM   #7 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,470
sde is on a distinguished road
sure np

i started with php and moved to c# too, .. i like c# a lot... then i learned some java though and now i understand how much c# took from java .. although there is nothing like developing in visual studio for me. ( well vs compared to netbeans )
__________________
Mike
sde is offline   Reply With Quote
Old 06-21-2004, 07:14 AM   #8 (permalink)
sammy
Code Monkey
 
sammy's Avatar
 
Join Date: Jun 2004
Location: Brooklyn/Rochester
Posts: 53
sammy is on a distinguished road
Send a message via AIM to sammy
I started with Java, but developed it in a UNIX enviornment with emacs. It is nothing like VS. Theres just so much stuff in VS that it gives me a headache. C# is very powerful, i recently learned delegates, and generics (1.2 framework). Havent put them to use yet, so i havent mastered them. Will be nice when i do though.
__________________
sammy is offline   Reply With Quote
Old 06-21-2004, 07:36 AM   #9 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,470
sde is on a distinguished road
generics? hmm .. i don't know of that yet.

I was referring mainly to the text editor and how it works with objects with visual studio.

the intellisense is very fast, and not case sensitive which makes accessing properties much easier .. maybe i'm just lazy lol.
__________________
Mike
sde is offline   Reply With Quote
Old 06-21-2004, 07:42 AM   #10 (permalink)
sammy
Code Monkey
 
sammy's Avatar
 
Join Date: Jun 2004
Location: Brooklyn/Rochester
Posts: 53
sammy is on a distinguished road
Send a message via AIM to sammy
generics are in the new framework 1.2
I got to play with the new Visual Studio (codename Whidbey) which is still in alpha build, and it used 1.2 framework.
If you want to read up on them, you can visit my professors slides.
http://www.cs.rit.edu/~ats/cs-2003-3...s/_d0e516.html
__________________
sammy is offline   Reply With Quote
Old 06-22-2004, 02:22 AM   #11 (permalink)
npa
Code Monkey
 
Join Date: Jul 2003
Location: canada
Posts: 82
npa is on a distinguished road
fwiw, the general system to solve the 'dynamic variable' problem
in non-scripting langauge is a hashmap.

it seems that your design could be better, (i.e. array list
to hold your obj's, instead of strange referencing rules),
but don't forget the hashmap can provide you a nice way to
reference objects attached to strings (i.e. the dynamic variable
illusion) ... so if it is a real requirement, and you intend
to use non-increasing numbers as your variable names, consider
the hashmap, it can be appropriate here.

of course, however, optimise your design first
__________________
direct entry file specification.
npa is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
VB or C#, or something else Apodysophilia MS Technologies ( ASP, VB, C#, .NET ) 6 10-15-2004 09:48 AM
operate overloading member function in C# sureshkumar_kc MS Technologies ( ASP, VB, C#, .NET ) 2 10-15-2004 02:36 AM
For those who have Learned C from "The C Programming Lanuage" DemosthenesB Standard C, C++ 5 07-13-2003 12:22 AM
edit? anon Lounge 10 11-21-2002 03:02 PM


All times are GMT -8. The time now is 02:29 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting