Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / July 2006

Tip: Looking for answers? Try searching our database.

Creating a new object of a GetType() type throws an exception. Why?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Paul Jones - 23 Jul 2006 20:16 GMT
Hi,
I need to create a new object of the same type as another variable is.
Since I do not know the exact type (it varies within the function), I
need to get the original type using GetType(). I've found somewhere on
the internet that the new variable can be created this way:

Object celldata = row.ItemArray.GetValue(i);
Object newobj = Activator.CreateInstance(celldata.GetType());

The second line throws an exception "A constructor without parameters is
not defined for this object." Why? celldata is typed as string at this
moment...

Thanks for any help...

With regards
Paul Jones
Barry Kelly - 23 Jul 2006 21:28 GMT
> I need to create a new object of the same type as another variable is.
> Since I do not know the exact type (it varies within the function), I
[quoted text clipped - 9 lines]
>
> Thanks for any help...

I see your name there 'lazy paul j'. I don't want to encourage laziness,
so I'll point out just two things:

* The exception is saying that there's no constructor without
parameters.

* You say celldata is typed as string - that's equivalent to
System.String.

I suggest you look in the documentation for System.String and examine
its constructors.

-- Barry

Signature

http://barrkel.blogspot.com/

Paul Jones - 23 Jul 2006 21:52 GMT
Hi Barry, I guess I should change the e-mail address... :)

Well, as I found out after I posted the question there really is no
string constructor with no parameters, but I was not able to figure out
how to give those parameters to the constructor... Nevertheless, after a
thorough examination of CreateInstance overloads' parameters it was
quite clear...

Thanks for the advice, I think this approach is sometimes better than
providing a complete solution... :)

With regards
Paul Jones

Barry Kelly napsal(a):

>> I need to create a new object of the same type as another variable is.
>> Since I do not know the exact type (it varies within the function), I
[quoted text clipped - 23 lines]
>
> -- Barry

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.