By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,393 Members | 1,405 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,393 IT Pros & Developers. It's quick & easy.

Limited Size String

P: n/a
In some language, it's possible to specify a "predefined" size for string
objects.

Exemple: string[10] which is a string of 10 characters.

Is it possible to do this in C#?

Thank you
Feb 15 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Michel,

No, it is not. As a workaround, you might want to use an array of
characters which is set to 10 characters. When you need a string, you can
pass the array to the constructor of the string class and it will create a
new string based on that array.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Michel Racicot" <mr******@hotmail.com> wrote in message
news:ek*************@tk2msftngp13.phx.gbl...
In some language, it's possible to specify a "predefined" size for string
objects.

Exemple: string[10] which is a string of 10 characters.

Is it possible to do this in C#?

Thank you

Feb 15 '06 #2

P: n/a
A string object is imutable. Once a string is created, it cannot be changed.
string[10] is a vector of strings in C#...
You can create a vector of char, if this is what you want to do...

What do you plan to do?

"Michel Racicot" <mr******@hotmail.com> wrote in message
news:ek*************@tk2msftngp13.phx.gbl...
In some language, it's possible to specify a "predefined" size for string
objects.

Exemple: string[10] which is a string of 10 characters.

Is it possible to do this in C#?

Thank you

Feb 15 '06 #3

P: n/a
That particular datatype will be used to represent a unique plugin ID.
The data is part of the header of a structure that will be sent via Sockets.
The communication protocol have to be sure that the sent header is always
the same fixed size (the body can have variying size as I pass along the
size of the content in the header also).

Of course, I can always pad or truncate the PluginID to 10 characters and
ensure that when the plugins are loaded, that the plugin id value in the XML
definition of the user plugin doesn't contain more that 10 characters...

"Ravi Ambros Wallau" <no****@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
A string object is imutable. Once a string is created, it cannot be
changed.
string[10] is a vector of strings in C#...
You can create a vector of char, if this is what you want to do...

What do you plan to do?

"Michel Racicot" <mr******@hotmail.com> wrote in message
news:ek*************@tk2msftngp13.phx.gbl...
In some language, it's possible to specify a "predefined" size for string
objects.

Exemple: string[10] which is a string of 10 characters.

Is it possible to do this in C#?

Thank you


Feb 15 '06 #4

P: n/a
Just create a new class that extends string. Override the .ToString()
method to pad or truncate as necessary.

Feb 15 '06 #5

P: n/a
<pr*****@gmail.com> wrote:
Just create a new class that extends string. Override the .ToString()
method to pad or truncate as necessary.


Except that String is sealed...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 15 '06 #6

P: n/a
Create a PlugIn class with an Id property of type string. The get
and/or set accessors can ensure the proper length. You could also add
an IsValid property, or Validate(string id) method that the UI or other
id source can call to ensure a proper id is specified.

- Mike

Feb 15 '06 #7

P: n/a
Or....

class Program
{
[STAThread]
static void Main(string[] args)
{

String10 s = "0123456789";

Console.WriteLine("String: " + s);
Console.Read();

}

}

public struct String10
{
string value;

public String10(string value)
{
if (value.Length > 10) throw new ArgumentException("string must be 10
characters or less in length.","value");
this.value = value;
}

public static implicit operator String10(string s)
{
return new String10(s);
}

public override string ToString()
{
return value.ToString();
}

}

--
-Demetri
"mi**********@xquisoft.com" wrote:
Create a PlugIn class with an Id property of type string. The get
and/or set accessors can ensure the proper length. You could also add
an IsValid property, or Validate(string id) method that the UI or other
id source can call to ensure a proper id is specified.

- Mike

Feb 16 '06 #8

P: n/a
A potential problem I have with "String10" is that if you ever change
the rule for the length of a plugin id, is that string10 may not
reflect the actual rule anymore. Maybe you'll have to increase the
length of the Id someday to 12 (or more)? Then either you rename
string10 to string12 and all locations where it is used, or string10
really holds more characters that it leads you to believe.

- Mike

Feb 16 '06 #9

P: n/a
Well then call the class FixedString and modify it to allow for setting the
max length at runtime.

public struct FixedString
{
string value;

public FixedString(int maxLength,string value)
{
if (value.Length > maxLength) throw new ArgumentException("string must be
" + maxLength.ToString() + " characters or less in length.","value");
this.value = value;
}

public override string ToString()
{
return value.ToString();
}

}

--
-Demetri
"mi**********@xquisoft.com" wrote:
A potential problem I have with "String10" is that if you ever change
the rule for the length of a plugin id, is that string10 may not
reflect the actual rule anymore. Maybe you'll have to increase the
length of the Id someday to 12 (or more)? Then either you rename
string10 to string12 and all locations where it is used, or string10
really holds more characters that it leads you to believe.

- Mike

Feb 16 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.