473,569 Members | 2,701 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

the "new" keyword

i'm at the very beginning, and while trying to use the following small
code, i got an error msg thta said use the "new" keyword to create an
object instance.

Actually, since i'm at the very beginning, i don;t really know hot to
change it. This is the code:

string strConnectionSt ring =
ConfigurationMa nager.Connectio nStrings["SqlConnectionS tring"].ConnectionStri ng;

How should i use the "new" keyword in this case?

Thanks

Aug 28 '07 #1
6 3295
Do you have SqlConnectionSt ring properly configured in your configuration
file? I think it's missing.

Try using System.Configur ation.Configura tionManager.Con nectionStrings["LocalSqlServer "].ConnectionStri ng,
LocalSqlServer is configured in machine.config file, so you should be able
to get that.

J.W.

Hello vinnie,
i'm at the very beginning, and while trying to use the following small
code, i got an error msg thta said use the "new" keyword to create an
object instance.

Actually, since i'm at the very beginning, i don;t really know hot to
change it. This is the code:

string strConnectionSt ring =
ConfigurationMa nager.Connectio nStrings["SqlConnectionS tring"].Connecti
onString;
How should i use the "new" keyword in this case?

Thanks

Aug 28 '07 #2
vinnie wrote:
i'm at the very beginning, and while trying to use the following small
code, i got an error msg thta said use the "new" keyword to create an
object instance.
It's lying to you.

:)

Well, actually...it might be that the error includes other information.
But if all it says is to use "new", then at least based on the line of
code you posted it doesn't appear that advice is correct.
Actually, since i'm at the very beginning, i don;t really know hot to
change it. This is the code:

string strConnectionSt ring =
ConfigurationMa nager.Connectio nStrings["SqlConnectionS tring"].ConnectionStri ng;
I haven't used that part of the API, but it should just work fine as
you've posted the code.

The ConnectionStrin gs property returns a
ConnectionStrin gSettingsCollec tion and using the ["SqlConnectionS tring"]
indexer on it should return a ConnectionStrin gSettings instance, which
has a ConnectionStrin g property that is of the type string.

In other words, the line of code should return a string and assign it to
your variable "strConnectionS tring". (All of that assumes the
"SqlConnectionS tring" identifies an actual item in the collection, but
that wouldn't be a compile-time error in any case).

So...

Given that the code posted should work, there must be something else you
didn't describe about the code that's causing a problem. For best
results, you should post a concise-but-complete example of code that
reproduces the compiler error. Emphasis on "concise".

But, before you do that, you should check to make sure that you have
actually added System.Configur ation as a reference for your project
(some namespaces require explicitly being added as a reference, some are
already in the default references). I can see how you'd get a compiler
error that tries to be helpful about how to instantiate a string if the
problem was actually that the compiler wasn't aware of the API you're
trying to use.

If that's not the problem, then along with the example of code I suggest
above, you should also post the _exact_ text of the error. Error
messages have very specific details within them that often can lead
directly to a solution. But we can only offer that solution if you post
those exact details.

Pete
Aug 28 '07 #3
Hello Peter,
vinnie wrote:
>i'm at the very beginning, and while trying to use the following
small code, i got an error msg thta said use the "new" keyword to
create an object instance.
It's lying to you.

:)

Well, actually...it might be that the error includes other
information. But if all it says is to use "new", then at least based
on the line of code you posted it doesn't appear that advice is
correct.
>Actually, since i'm at the very beginning, i don;t really know hot to
change it. This is the code:

string strConnectionSt ring =
ConfigurationM anager.Connecti onStrings["SqlConnectionS tring"].Connect
ionString;
I haven't used that part of the API, but it should just work fine as
you've posted the code.

The ConnectionStrin gs property returns a
ConnectionStrin gSettingsCollec tion and using the
["SqlConnectionS tring"] indexer on it should return a
ConnectionStrin gSettings instance, which has a ConnectionStrin g
property that is of the type string.

[If the SqlConnectionSt ring is not configured properly, it could return null
here, and I think this is
OP's problem.]
In other words, the line of code should return a string and assign it
to your variable "strConnectionS tring". (All of that assumes the
"SqlConnectionS tring" identifies an actual item in the collection, but
that wouldn't be a compile-time error in any case).

So...

Given that the code posted should work, there must be something else
you didn't describe about the code that's causing a problem. For best
results, you should post a concise-but-complete example of code that
reproduces the compiler error. Emphasis on "concise".

But, before you do that, you should check to make sure that you have
actually added System.Configur ation as a reference for your project
(some namespaces require explicitly being added as a reference, some
are already in the default references). I can see how you'd get a
compiler error that tries to be helpful about how to instantiate a
string if the problem was actually that the compiler wasn't aware of
the API you're trying to use.

If that's not the problem, then along with the example of code I
suggest above, you should also post the _exact_ text of the error.
Error messages have very specific details within them that often can
lead directly to a solution. But we can only offer that solution if
you post those exact details.

Pete

Aug 28 '07 #4

"Peter Duniho" <Np*********@Nn OwSlPiAnMk.comw rote in message
news:13******** *****@corp.supe rnews.com...
Jianwei Sun wrote:
>>The ConnectionStrin gs property returns a
ConnectionStr ingSettingsColl ection and using the
["SqlConnectionS tring"] indexer on it should return a
ConnectionStr ingSettings instance, which has a ConnectionStrin g
property that is of the type string.


[If the SqlConnectionSt ring is not configured properly, it could return
null here, and I think this is OP's problem.]

Could be. The OP wasn't clear at all about whether they were getting a
compile error or a run-time error. If the former, then a missing key
isn't the issue. But if the latter, it could very well be.

Hard to say without seeing the exact error. Which of course reinforces
Bet it is a NullReferenceEx ception, which may very well give instructions to
users on how to get an instance of a class.

int i; i.ToString(); // ok
Form f; f.ToString(); // new programmers might not understand why this
doesn't work, NullReferenceEx ception message is meant for them
Aug 28 '07 #5
Ben Voigt [C++ MVP] wrote:
Bet it is a NullReferenceEx ception, which may very well give instructions to
users on how to get an instance of a class.
Sure, that could be a good bet. Even when I started out with C#, that's
not an exception I ever had to deal with, so can't say I'm all that
familiar with the exact text of the runtime error.

Still, we're just betting until the OP clarifies.

Pete
Aug 28 '07 #6

"Peter Duniho" <Np*********@Nn OwSlPiAnMk.comw rote in message
news:13******** *****@corp.supe rnews.com...
Ben Voigt [C++ MVP] wrote:
>Bet it is a NullReferenceEx ception, which may very well give instructions
to users on how to get an instance of a class.

Sure, that could be a good bet. Even when I started out with C#, that's
not an exception I ever had to deal with, so can't say I'm all that
familiar with the exact text of the runtime error.
I cause that error every now and then myself, but a programming on the same
project likes to use try/catch instead of testing the variable beforehand...
>
Still, we're just betting until the OP clarifies.

Pete

Aug 29 '07 #7

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

Similar topics

3
333
by: Paul Auleciems | last post by:
Hi: I'm having trouble using an Object which is created based on the following: Public CarDetail () as Car Where the CLASS "Car" is defined as: Public Class Car
1
2086
by: alanrn | last post by:
I've implemented a number of strongly-typed collections that inherit from CollectionBase and recently noticed something that I don't fully understand. CollectionBase defines method RemoveAt(). However, CollectionBase implements IList which also defines method RemoveAt(). In my collection when I code my own RemoveAt() method, the compiler...
12
1901
by: Ola Johansson | last post by:
Can anyone explain to me a meningfull use of the "new" keyword (Shadows in VB). I think i understand how it works fully but i cant figure out why you would use it. Why would you want to declare a variable in a derived class with the same name as in the base class but with a diffrent meaning? Anyone can think of a scenario for this?
8
13291
by: Dot net work | last post by:
I need VB.NET's "shadows" functionality inside a C# project. I tried the "new" keyword, but it didn't seem to work, because my particular function does in fact differ in signature to the function that is being hidden in the base class. In VB.NET, the shadows keyword hides all the inherited functions, regardless of signature. I need...
0
346
by: Michael Steidl | last post by:
I use XMLspy for handling XML files and started testing its feature to generate C# code from XML Schemas. Now I got stuck in a strange situation: (I have to add being not a C# geek, only currently looking deeper into C# considering making more use of it) I created C# code from a schema with XMLspy 2005 rel 1. This code worked flawlessly. ...
5
347
by: Sam | last post by:
Hi everyone Could anyone help me understand the usage of the "New" keyword I'm new to VB.Net. 1. Why do we use the "New" keyword on some object type variables such as the myPen of the example below and not with the bgColor. Both the Pen and Color are objects Dim myPen As Pen = New Pen(Color.AquaMarine)
4
2621
by: Ben R. | last post by:
I'm curious about the differeng behavior of the "new" keyword when dealing with value versus object types. If I'm correct, when I do: dim x as integer There's no need for "new" to be called since this "value" type is all set to go. Is this because value types are stored on the stack so the memory initialization is already taken care of? ...
37
3912
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as a function
3
2363
by: padhuwork | last post by:
Hi, I m actually making a Windows DLL using VB 6. I have modified the Link.exe, so that, it compiles a Windows DLL. I m now struck with this. I add reference "Microsoft Scripting Runtime" Dim fsr As New FileSystemObject
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8120
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7672
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7968
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5512
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.