String.Substring() Does not exist error

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

Using C# I'm trying to use the substring method of a string variable but it
just generates an "error: 'variable.Substring' does not exist " exception -
how do I fix this ?

code example:

string creationDateStr = string.Empty;
string anotherStr = "";
creationDateStr = "some string or other";
anotherStr = creationDateStr.Substring(0,2);

This compiles but errors at run time.

Thanks,
 
you have to make sure that there is something in your string variable.

if len(creationdatestr) > 2 then
creationdatestr.substring(0,2)
else
messagebox.show("string not long enough")
end if

this is vb syntax... I'm sure you can change it to c# ;-)
 
I have had no problems with your code (although I am still using VS2005 Beta
2). It returs "so" all the time.

Fitim Skenderi
 
Interwander,
if len(creationdatestr) > 2 then
creationdatestr.substring(0,2)
else
messagebox.show("string not long enough")
end if

this is vb syntax... I'm sure you can change it to c# ;-)
You can but is is a little bit without sense

Cor
 
Duncan,

In my opinion should your code run, is it not something around that.

Cor
 
maybe not standard but you can import a namespace that does

in C# it is USE i think?

use Microsoft.VisualBasic

Strings.Left(str, Length)

.....
click yes if this post was helpfull :-D
 
Duncan,

The Left, Right, and Mid are just a part of the Visual Basic namespace the
same as other namespace that you use in C#.

However, if it is only for this it has very few sense. (It has inbuild
checking if by instand the second parameter of a substring is not going
outside the string length and make that second parameter than accoording the
length)

However that is not in this case.

I hope that this gives an idea.

Cor
 
OK, analysed the problem a bit more and basically no string object is being
treated as an object - so maybe I need to re-register something .... ?

Stuff like ...

myArray[0].ToUpper();
or even myString.ToString()

doesn't work either .....
 
Duncan Allen said:
Using C# I'm trying to use the substring method of a string variable but it
just generates an "error: 'variable.Substring' does not exist " exception -
how do I fix this ?

code example:

string creationDateStr = string.Empty;
string anotherStr = "";
creationDateStr = "some string or other";
anotherStr = creationDateStr.Substring(0,2);

This compiles but errors at run time.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
Cor said:
Duncan gave his own answer on the last line to me.

But not in a way which really shows the root of the problem. There's
clearly something wrong in how his variables are being set up in the
first place - and a short but complete program would demonstrate that.

Jon
 
Hi,

Using C# I'm trying to use the substring method of a string variable but it
just generates an "error: 'variable.Substring' does not exist " exception -
how do I fix this ?

code example:

string creationDateStr = string.Empty;
string anotherStr = "";
creationDateStr = "some string or other";
anotherStr = creationDateStr.Substring(0,2);

This compiles but errors at run time.

Thanks,

Check in your references that you haven't removed 'System', as this is
responsible for providing the base object types such as String.
Also, check that 'Imported Namespaces' also has a reference to System.

Cheers,
Jason
 
Check in your references that you haven't removed 'System', as this is
responsible for providing the base object types such as String.
Also, check that 'Imported Namespaces' also has a reference to System.

Slight correction - the System assembly doesn't contain System.String -
mscorlib does.
 
Back
Top