473,397 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Limited Size String

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
9 10368
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
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
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
Just create a new class that extends string. Override the .ToString()
method to pad or truncate as necessary.

Feb 15 '06 #5
<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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Khuong Dinh Pham | last post by:
I have the contents of an image of type std::string. How can I make a CxImage object with this type. The parameters to CxImage is: CxImage(byte* data, DWORD size) Thx in advance
7
by: arkobose | last post by:
hey everyone! i have this little problem. consider the following declaration: char *array = {"wilson", "string of any size", "etc", "input"}; this is a common data structure used to store...
13
by: cvh | last post by:
Hello, In delphi I could do this : Var Str: String (String of size 2char) How would I do this with VB.NET in CF? Thank you,
9
by: Protoman | last post by:
Why doesn't this display the string? string decrypt(const string& ciphertext,const string& key) { string decrypted; decrypted.resize(ciphertext.size()); // allocates the string for the correct...
0
by: Ken Varn | last post by:
I have a managed C++ assembly in which I need to interact with some 'C' APIs that take fixed size 'C' data blocks. I need to wrap these data blocks into a managed object. It seems like a lot of...
5
by: gopi2ks | last post by:
hi one and all i have so many controls on a form,but the form has a limited size,how to increase the size of a form more than a window size by using scroll bars ,to place all the controls in single...
10
by: John Brown | last post by:
Hi there, Does anyone know how to go about reading/writing a type to a file in a language (culture) independent way. For instance, let's say you're dealing with the native "System.Drawing.Size"...
3
by: mark4asp | last post by:
Stack of limited size containing unique items? Hi guys, How would I implement a stack of limited size containing unique items? For example. Suppose my stack has . I add 2 to it and it is now...
5
by: erictheone | last post by:
so here is my code. My getlines for the strings keyword and phrase at lines 44 and 79 respectively don't work. Please help!!! #include <cstdlib> #include <string> #include <iostream> #include...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
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
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
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
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...
0
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...
0
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,...
0
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...

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.