473,382 Members | 1,332 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

"Dynamicly" create variables

Hi!

I would like to dim variables using dynamic code, like following:

Dim type as String = "Integer"
Dim variable as [type]

(the issue is in fact about my own types,
but let us isolate the issue using the native types)

Best regards,
Benjamin
Jul 21 '05 #1
4 1282
Dim variable As Integer ?? Generics (2.0), using "Object" (the type is not
supposed to be known at compile time ?), creating code using CodeDom ?

Explaining what you are trying to do may help...

In particular if doing simply :
Dim variable As Integer
You can use Variable.GetType to get the type as a type variable (ie. the
other way round, you can the type name from the variable instead of
creating the variable using the type name).

Patrice
--

"Benjamin" <be***************@nospam.nospam> a écrit dans le message de
news:86**********************************@microsof t.com...
Hi!

I would like to dim variables using dynamic code, like following:

Dim type as String = "Integer"
Dim variable as [type]

(the issue is in fact about my own types,
but let us isolate the issue using the native types)

Best regards,
Benjamin

Jul 21 '05 #2
Benjamin <be***************@nospam.nospam> wrote:
I would like to dim variables using dynamic code, like following:

Dim type as String = "Integer"
Dim variable as [type]

(the issue is in fact about my own types,
but let us isolate the issue using the native types)


If you don't know the type at compile-time, what use do you expect to
get from declaring a variable of that type? You couldn't do anything
more with it (at least with Option Strict On) than you could with
Object.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #3
Hello Benjamin,
I agree with Jon, if you don't know it's type, you won't be able to do much
with it.
But, I managed to do what you're asking for:

using System;
using System.Runtime.Remoting;

statc void Example()
{

int MyInt = 0;

string NameOfType = MyInt.GetType().FullName.ToString();
string NameOfAssembly = MyInt.GetType().Assembly.FullName.ToString();

ObjectHandle hnd = Activator.CreateInstance(NameOfAssembly, NameOfType);

int MyNewlyCreatedInt = (int) hnd.Unwrap();

}

Hi!

I would like to dim variables using dynamic code, like following:

Dim type as String = "Integer"
Dim variable as [type]
(the issue is in fact about my own types,
but let us isolate the issue using the native types)
Best regards,
Benjamin


Jul 21 '05 #4
Brian <fa**@email.com> wrote:
I agree with Jon, if you don't know it's type, you won't be able to do much
with it.
But, I managed to do what you're asking for:
Not quite.
using System;
using System.Runtime.Remoting;

statc void Example()
{

int MyInt = 0;

string NameOfType = MyInt.GetType().FullName.ToString();
string NameOfAssembly = MyInt.GetType().Assembly.FullName.ToString();

ObjectHandle hnd = Activator.CreateInstance(NameOfAssembly, NameOfType);

int MyNewlyCreatedInt = (int) hnd.Unwrap();

}


That's creating a new instance (I'd just use the version which returned
an Object reference personally) but it's not creating a new *variable*
as requested. I suspect the OP doesn't really need to create a new
variable though - it's hard to say without more information.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: bubbakittee | last post by:
I am designing a package of functions to be used by people with very little programming experience (if any) and very little patience. I don't want to scare them with programmerish words like...
1
by: Dominique Vandensteen | last post by:
I want to dynamicly load a type (typename is defined in the database). This type is located in the exe itself or one of the dll's in the directory with the exe file. When creating an instance...
4
by: Benjamin | last post by:
Hi! I would like to dim variables using dynamic code, like following: Dim type as String = "Integer" Dim variable as (the issue is in fact about my own types, but let us isolate the issue...
5
by: han zhiyang | last post by:
Hi. I tried to design a custom web control which can flexibly and dynamicly let the control user ,for example the web page developer, customize its layout codes.This control derives from...
5
by: GS | last post by:
Hi, I have dropdown list bound to a Table, I don't want dropdown list to select first row from a table but rather to display "Choose" and when user clicks it then to display available values. How...
8
by: Bruce | last post by:
I am using VB in Vs2005. Am I missing something or does VB not have the concept of "builds" (release/debug) like in VC? I wrote an assembly and I would like to have a debug version of the DLL...
8
by: bob | last post by:
Hi, we're faced with a horrible scenario whereby we are not able to access exported classes in a DLL due to cross compiler issues. Traditional approaches such as using extern "C" and...
4
by: Apple1 | last post by:
Hello! I would like to switch elements on the fly. Dynamicly turn "A" elenent an "input" tag. This is what I tried: <html> <head> <script>
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.