Sign in to follow this  

C# strings

This topic is 4275 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

ok sorry for asking so many questions bout this topic. but im having a real hard time with this. lets say i have a string _ret; and a char x; how can i insert x into _ret;

Share this post


Link to post
Share on other sites
ret.charAt(x) = char_var;
where x is the index you want to access. index being from 0 to size - 1

MSDN is a great resource and if you can't find it there, then just go to Google Romance [smile]. They match everyone up with what they need using great algorithms.

Share this post


Link to post
Share on other sites
ya the problem is that the char x is being assigned in a loop and I could just do
x.ToString then insert that string into _ret.
but since the x is beeing assigned in a loop it says, the use of "unnassigned local variable"
I dont get that. i mean its being assigned a value in the loop.

example.

public string function()
{
string _ret;int i=0;
while(i<1)
{
_ret="hello";
i=1;
}
return _ret;
}

it wont let me do that says _ret is an unassigned local variable.

Share this post


Link to post
Share on other sites
strings if i'm not confusing this with Java are reference variables.
so you have to do this:

public string function()
{
string _ret = new string();
//you may even have to put a size.
//consult MSDN on initializing a string.
//it's not the same as C++.

int i=0;

while(i<1)
{
_ret="hello";
i=1;
}
return _ret;
}

Share this post


Link to post
Share on other sites
don't forget that strings are immutable, they can't be changed. Any operation you do to a string that "changes" it actually creates a whole new string. This can be slow if you are doing a lot of operations (like around 50 in one go). You might want to consider using a StringBuilder.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Alpha_ProgDes
strings if i'm not confusing this with Java are reference variables.
so you have to do this:
*** Source Snippet Removed ***


huh I got an error with that it says no overload for method string.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Quote:
Original post by Alpha_ProgDes
strings if i'm not confusing this with Java are reference variables.
so you have to do this:
*** Source Snippet Removed ***


huh I got an error with that it says no overload for method string.


I think he was referring to the System.String object and not the string object. I have not used System.String though.

Generally in C# it is a good idea to give a string a default value. ie
string _s = "";
or
string _s = string.Empty;

Especially when you are doing operations that add onto the string. In fact most variables will be complained about if you modify them without giving them a value. Mostly because what value you expect the code below to give the integer?


int i;
for(int k = 0; k < 10; k++){
i += k * i + 1;
}


The problem is without initializing i, it could be anything. This is more true in C than in C# I believe... but it proves the point.

Share this post


Link to post
Share on other sites
Quote:
Original post by Krisc
Quote:
Original post by Anonymous Poster
Quote:
Original post by Alpha_ProgDes
strings if i'm not confusing this with Java are reference variables.
so you have to do this:
*** Source Snippet Removed ***


huh I got an error with that it says no overload for method string.


I think he was referring to the System.String object and not the string object.

I didn't know there were 2 types of strings [shocked]

Share this post


Link to post
Share on other sites
Well, I do not believe they are different except for System.String might supply a Constructor where as string, much in the same way as int, double, etc. do not.

Share this post


Link to post
Share on other sites
Basically string is the same as System.String is just a name so we can use it more easily

It's just like a typedef in c++

In c# int stand for System.Int32
long for System.Int64

and yes in C# all the variables need to be initialized

Share this post


Link to post
Share on other sites

This topic is 4275 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this