Creating a new instance based on equals

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 1518 Views - Last Post: 22 December 2013 - 09:38 AM Rate Topic: -----

#1 Oblivion165   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 20-December 13

Creating a new instance based on equals

Posted 20 December 2013 - 08:27 AM

      
IntString s = "";



Hi, I'm creating a new version of a string and I can't for the life of me figure out how to create a new instance of it when I equal it to "". Does anyone know the operator or cast overload to make this work?
Is This A Good Question/Topic? 0
  • +

Replies To: Creating a new instance based on equals

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14175
  • View blog
  • Posts: 56,787
  • Joined: 12-June 08

Re: Creating a new instance based on equals

Posted 20 December 2013 - 08:57 AM

That use of the '=' is really a "get".

Quote

Variable 's' (which is of type intstring) *GETS* and string of length zero.

Was This Post Helpful? 0
  • +
  • -

#3 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5104
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Creating a new instance based on equals

Posted 20 December 2013 - 09:27 AM

Modi's right. Literals have slightly different behaviors. String literals are created and interned separately; the constructors are executed behind the scenes.

However, I do have some advice: look up the implicit operator:

http://msdn.microsof...y/z5z9kes2.aspx

Should be able to give you what you want.

This post has been edited by Curtis Rutland: 21 December 2013 - 01:23 PM
Reason for edit:: typo

Was This Post Helpful? 1
  • +
  • -

#4 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Creating a new instance based on equals

Posted 20 December 2013 - 10:06 AM

What do you mean exactly by "make a new instance" of it? Are you trying to create a whole new variable at run-time that wasn't defined when you coded it?
Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5104
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Creating a new instance based on equals

Posted 20 December 2013 - 10:12 AM

I think he's trying to use string literals to instantiate objects of his new type.
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Creating a new instance based on equals

Posted 20 December 2013 - 06:43 PM

Well... I've done it. But its a pain in the arse.
Was This Post Helpful? 0
  • +
  • -

#7 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: Creating a new instance based on equals

Posted 20 December 2013 - 10:58 PM

Not that much of a pain
using System;

namespace Test {
    class NewString {
        public static implicit operator NewString(String s) {
            NewString result = new NewString();
            // rest of conversion code here
            return result;
        }
    }
}

Was This Post Helpful? 0
  • +
  • -

#8 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7199
  • View blog
  • Posts: 15,008
  • Joined: 16-October 07

Re: Creating a new instance based on equals

Posted 21 December 2013 - 04:21 AM

This looked amusing.

Just like C# string, I've made it immutable and used a simple char array in its guts:
class FooString {
	private readonly char [] chars;
	public FooString() { chars = new char[0]; }
	public FooString(char[] chars) { this.chars = new char[chars.Length]; Array.Copy(chars, this.chars, chars.Length); }
	public FooString(string s) : this(s.ToCharArray()) { }
	public int Length { get { return chars.Length; } }
	public override string ToString() { return new String(chars);  }
	public char this[int idx] { get { return chars[idx]; } }
	public static implicit operator FooString(string s) { return new FooString(s); }
	public static implicit operator FooString(char[] chars) { return new FooString(chars); }
	public static FooString operator + (FooString x, FooString y) {
		var z = new char[x.chars.Length + y.chars.Length];
		Array.Copy(x.chars, z, x.chars.Length);
		Array.Copy(y.chars, 0, z, x.chars.Length, y.chars.Length);
		return new FooString(z);
	}
}

// ...
// test
FooString x = "Hello";
Debug.WriteLine(x.GetType());
Debug.WriteLine(x);
var y = x + " World";
Debug.WriteLine(y.GetType());
Debug.WriteLine(y);



Surprisingly easy. I liked that I got the + string for free, based off the conversion.
Was This Post Helpful? 1
  • +
  • -

#9 Michael26   User is offline

  • Futurama: Insert funny joke here
  • member icon

Reputation: 414
  • View blog
  • Posts: 1,664
  • Joined: 08-April 09

Re: Creating a new instance based on equals

Posted 21 December 2013 - 05:10 AM

So let me get this straight,
var y = x + " World";
now the y is no longer of the type FooString but is of some other type?
Was This Post Helpful? 0
  • +
  • -

#10 andrewsw   User is offline

  • Bouncy!
  • member icon

Reputation: 6563
  • View blog
  • Posts: 26,615
  • Joined: 12-December 12

Re: Creating a new instance based on equals

Posted 21 December 2013 - 05:27 AM

@Michael It works as:

FooString y = x + " World";

@baavgai
It would be interesting to extend this beyond a normal string by implementing, for example, subtraction. The following is a poor first attempt, as it relies too much on String methods (ToString()):

        public static FooString operator -(FooString x, FooString y) {
            if (x.ToString().Contains(y.ToString())) {
                return new FooString(x.ToString().Replace(y.ToString(), ""));
            } else {
                return new FooString(x.ToString());
            }
        }

    // test
    FooString z = y - "ell";     // "Ho World"
    Debug.WriteLine(z);

Could it be written without the String-methods..? ;)

This post has been edited by andrewsw: 21 December 2013 - 05:28 AM

Was This Post Helpful? 0
  • +
  • -

#11 andrewsw   User is offline

  • Bouncy!
  • member icon

Reputation: 6563
  • View blog
  • Posts: 26,615
  • Joined: 12-December 12

Re: Creating a new instance based on equals

Posted 21 December 2013 - 05:39 AM

I can improve it slightly with another constructor:

    public FooString(FooString foo) : this(foo.ToString()) {
    }

    public static FooString operator -(FooString x, FooString y) {
        if (x.ToString().Contains(y.ToString())) {
            return new FooString(x.ToString().Replace(y.ToString(), ""));
        } else {
            return new FooString(x);
        }
    }

Comparing the lengths would tell us if the subtraction was successful.

Added: We could implement StringComparer (somewhere?):
    class FooString : StringComparer {


        public override int Compare(string x, string y) {
            throw new NotImplementedException();
        }

        public override bool Equals(string x, string y) {
            throw new NotImplementedException();
        }

        public override int GetHashCode(string obj) {
            throw new NotImplementedException();
        }

This might eventually(?) account for Contains(), but what about Replace()? Added: Actually, StringComparer would still rely on string methods, so probably not useful.

Of course, if it is just to supplement strings, then there are always Extension Methods.

This post has been edited by andrewsw: 21 December 2013 - 06:08 AM

Was This Post Helpful? 0
  • +
  • -

#12 andrewsw   User is offline

  • Bouncy!
  • member icon

Reputation: 6563
  • View blog
  • Posts: 26,615
  • Joined: 12-December 12

Re: Creating a new instance based on equals

Posted 21 December 2013 - 07:20 AM

In case it is of any interest ;)

    public static FooString operator *(FooString foo, int x) {
        FooString tempFoo = new FooString();
        while (x-- > 0) {
            tempFoo += foo;
        }
        return tempFoo;
    }

    // test
    FooString more = new FooString("XMas ");
    more = more * 4;
    Debug.WriteLine(more);  // XMas XMas XMas XMas

Was This Post Helpful? 0
  • +
  • -

#13 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Creating a new instance based on equals

Posted 21 December 2013 - 07:45 AM

View PostCurtis Rutland, on 20 December 2013 - 11:12 AM, said:

I think he's trying to use string literals to instantiate objects of his new type.

View PosttlhIn`toq, on 20 December 2013 - 07:43 PM, said:

Well... I've done it. But its a pain in the arse.

View PostMomerath, on 20 December 2013 - 11:58 PM, said:

Not that much of a pain
using System;

namespace Test {
    class NewString {
        public static implicit operator NewString(String s) {
            NewString result = new NewString();
            // rest of conversion code here
            return result;
        }
    }
}


I think we're all talking different things. I was thinking the string contained the word "camera" so instantiate a new CameraObject, string contains "person" so instantiate a new PersonObject and so on.
Was This Post Helpful? 0
  • +
  • -

#14 andrewsw   User is offline

  • Bouncy!
  • member icon

Reputation: 6563
  • View blog
  • Posts: 26,615
  • Joined: 12-December 12

Re: Creating a new instance based on equals

Posted 21 December 2013 - 08:32 AM

I know I've gone off on a bit of tangent, soz ;)
Was This Post Helpful? 0
  • +
  • -

#15 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Creating a new instance based on equals

Posted 21 December 2013 - 08:47 AM

Well, this has been fun but it seems everyone other than the OP is part of this conversation. Frankly if the guy that posted the question can't be bothered with participating in this thread then I can "unwatch" it and make a mental note that Oblivion165 is a poster that just wastes my time so ignore their future posts.

This post has been edited by tlhIn`toq: 21 December 2013 - 08:47 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2