473,513 Members | 2,601 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

use of unassigned variable in C#

4 New Member
Hi,
I am doing the following to split the string 'linestring' but i am getting the error
use of unassigned local variable 'myStrs' (in this case in line number 4 )

1. string[] myStrs;
2. if(linestring.Contains(","))
3. myStrs = linestring.Split(new char[] {','});
4. if (myStrs.Length != 0)
5. {
do something
}

Can u please help.. i am not sure what is wrong

Thanks.
Nit
Apr 13 '07 #1
4 12234
nmsreddi
366 Contributor
Hello

just try like this

string[] myStrs= { };

your problem may be solved
Apr 14 '07 #2
int08h
28 New Member
1. if(linestring.Contains(","))
2. string[] myStrs = linestring.Split(new char[] {','});
3. if (myStrs.Length != 0)
4. {
do something
}

define variable when need and make sure it is initialized
Apr 14 '07 #3
Motoma
3,237 Recognized Expert Specialist
What is happening is that when you get to line 2, linestring.Contains(",") is false, therefor myStrs is never being assigned. Give it an initial value at the time of declaration and this error should go away.

Hi,
I am doing the following to split the string 'linestring' but i am getting the error
use of unassigned local variable 'myStrs' (in this case in line number 4 )

1. string[] myStrs;
2. if(linestring.Contains(","))
3. myStrs = linestring.Split(new char[] {','});
4. if (myStrs.Length != 0)
5. {
do something
}

Can u please help.. i am not sure what is wrong

Thanks.
Nit
Apr 15 '07 #4
nitsjuly
4 New Member
Thank you so much.. it did work
May 26 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
3938
by: Mike P | last post by:
I keep getting the error 'Use of unassigned local variable' in my code, which I have used before and it works fine : SqlTransaction Trans1, Trans2; SqlConnection...
9
70159
by: Ropo | last post by:
Newbie: I am writing code to read an xml file. I want two values from the document so I want to save them as I am reading through the elements. I want to reference them (using Console.WriteLine)...
12
1973
by: Rene | last post by:
I understand that if I don't assign a *local* variable before I use it, the compiler will generate a "Use of unassigned local variable" error. What I don't get is why doesn't the compiler just...
3
6461
by: John Smith | last post by:
In the following (pseudo)code, why is it that C# returns an "unassigned local variable" error for nVar? It seems I have to declare the variable outside foo() for the error to dissapear. void...
2
3383
by: kaiser | last post by:
Hello I am trying to get the last lines value in a text file and display it on screen / read it into a variable. When I run the following code in a console I get the following error "Use...
29
2224
by: Joseph Geretz | last post by:
Use of unassigned local variable 'fs' Please see where I've indicated where the compiler is flagging this error in the method below. fs is initialized in the first line in the try block, so why...
9
6174
by: tshad | last post by:
I am getting an error: Use of unassigned local variable 'postDateRow' But it is assigned. Here is the code: int payDateRow; int postDateRow;
22
8386
by: Laura T. | last post by:
In the following example, c# 2.0 compiler says that a3 and ret are used before assigned. as far as I can see, definite assignment is made. If I add finally { ret = true; a3 = "b3";
8
11134
by: Dom | last post by:
This is a little tricky, but I think the error I'm getting isn't valid. The compiler just doesn't know my logic. MyObject o; switch (IntVariable) { case 1: o = new MyObject() break;
0
7259
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7158
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...
0
7380
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7523
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...
0
5683
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5085
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...
0
4745
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...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1592
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.